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);
            //SaveStream(zipStream);
            return zipStream;

        }

        public static void SaveStream(Stream stream)
        {
            string path = @"D:\319.zip";
            using (var fileStream = File.Create(path))
            {
                stream.CopyTo(fileStream);
                fileStream.Close();
            }
        }
    }
}
