CompressT.cs 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* ==============================================================================
  2. * 功能描述:Compress
  3. * 创 建 者:Garrett
  4. * 创建日期:2019/2/14 15:04:11
  5. * ==============================================================================*/
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using ICSharpCode.SharpZipLib.Zip;
  13. namespace PackageUploader.Compress
  14. {
  15. /// <summary>
  16. /// Compress
  17. /// </summary>
  18. class CompressT
  19. {
  20. /// <summary>
  21. /// 压缩
  22. /// </summary>
  23. /// <param name="source">源目录</param>
  24. /// <param name="s">ZipOutputStream对象</param>
  25. public static void Compress(string basePath, string source, ZipOutputStream s, CompressArgs args)
  26. {
  27. string[] filenames = Directory.GetFileSystemEntries(source);
  28. foreach (string file in filenames)
  29. {
  30. if (Directory.Exists(file))
  31. {
  32. Compress(basePath, file, s, args); //递归压缩子文件夹
  33. }
  34. else
  35. {
  36. using (FileStream fs = File.OpenRead(file))
  37. {
  38. byte[] buffer = new byte[4 * 1024];
  39. ZipEntry entry = new ZipEntry(file.Replace(basePath, "")); //此处去掉盘符,如D:\123\1.txt 去掉D:
  40. entry.DateTime = DateTime.Now;
  41. s.PutNextEntry(entry);
  42. int sourceBytes;
  43. do
  44. {
  45. sourceBytes = fs.Read(buffer, 0, buffer.Length);
  46. s.Write(buffer, 0, sourceBytes);
  47. args.IncrementTransferred = sourceBytes;
  48. } while (sourceBytes > 0);
  49. }
  50. }
  51. }
  52. }
  53. }
  54. }