ZipUtils.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  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. zipStream.Seek(0, SeekOrigin.Begin);
  31. return zipStream;
  32. }
  33. }
  34. }