1234567891011121314151617181920212223242526272829303132333435 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.IO.Compression;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace MBIRevitBase.Tools
- {
- public static class ZipUtils
- {
- /// <summary>
- /// 压缩zip字符串流
- /// </summary>
- /// <param name="data"></param>
- /// <param name="fileName"></param>
- /// <returns></returns>
- public static Stream ZipString(string data,string fileName)
- {
- MemoryStream zipStream = new MemoryStream();
- using (ZipArchive zipArchive = new ZipArchive(zipStream, ZipArchiveMode.Create, true))
- {
- ZipArchiveEntry entry = zipArchive.CreateEntry(fileName);
- using (StreamWriter writer = new StreamWriter(entry.Open()))
- {
- writer.Write(data);
- }
- //zipArchive释放才会写入相关流
- }
- zipStream.Seek(0, SeekOrigin.Begin);
- return zipStream;
- }
- }
- }
|