123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- /* ==============================================================================
- * 功能描述:Compress
- * 创 建 者:Garrett
- * 创建日期:2019/2/14 15:04:11
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using ICSharpCode.SharpZipLib.Zip;
- namespace PackageUploader.Compress
- {
- /// <summary>
- /// Compress
- /// </summary>
- class CompressT
- {
- /// <summary>
- /// 压缩
- /// </summary>
- /// <param name="source">源目录</param>
- /// <param name="s">ZipOutputStream对象</param>
- public static void Compress(string basePath, string source, ZipOutputStream s, CompressArgs args)
- {
- string[] filenames = Directory.GetFileSystemEntries(source);
- foreach (string file in filenames)
- {
- if (Directory.Exists(file))
- {
- Compress(basePath, file, s, args); //递归压缩子文件夹
- }
- else
- {
- using (FileStream fs = File.OpenRead(file))
- {
- byte[] buffer = new byte[4 * 1024];
- ZipEntry entry = new ZipEntry(file.Replace(basePath, "")); //此处去掉盘符,如D:\123\1.txt 去掉D:
- entry.DateTime = DateTime.Now;
- s.PutNextEntry(entry);
- int sourceBytes;
- do
- {
- sourceBytes = fs.Read(buffer, 0, buffer.Length);
- s.Write(buffer, 0, sourceBytes);
- args.IncrementTransferred = sourceBytes;
- } while (sourceBytes > 0);
- }
- }
- }
- }
- }
- }
|