ZipUtils.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Compression;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace MBIRevitBase.Tools
  9. {
  10. public static class ZipUtils
  11. {
  12. /// <summary>
  13. /// 压缩zip字符串流
  14. /// </summary>
  15. /// <param name="data"></param>
  16. /// <param name="fileName"></param>
  17. /// <returns></returns>
  18. public static Stream ZipString(string data, string fileName)
  19. {
  20. MemoryStream zipStream = new MemoryStream();
  21. using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
  22. {
  23. ZipArchiveEntry entry = zipArchive.CreateEntry(fileName);
  24. using (StreamWriter writer = new StreamWriter(entry.Open()))
  25. {
  26. writer.Write(data);
  27. }
  28. //zipArchive释放才会写入相关流
  29. }
  30. ;
  31. zipStream.Seek(0, SeekOrigin.Begin);
  32. //SaveStream(zipStream);
  33. return zipStream;
  34. }
  35. public static void SaveStream(Stream stream)
  36. {
  37. string path = @"D:\319.zip";
  38. using (var fileStream = File.Create(path))
  39. {
  40. stream.CopyTo(fileStream);
  41. fileStream.Close();
  42. }
  43. }
  44. }
  45. }