码迷,mamicode.com
首页 > 其他好文 > 详细

Creating a ZIP Archive in Memory Using System.IO.Compression

时间:2015-11-22 00:08:11      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

Thanks to http://stackoverflow.com/a/12350106/222748 I got:

using (var memoryStream = new MemoryStream())
{
   using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
   {
      var demoFile = archive.CreateEntry("foo.txt");

      using (var entryStream = demoFile.Open())
      using (var streamWriter = new StreamWriter(entryStream))
      {
         streamWriter.Write("Bar!");
      }
   }

   using (var fileStream = new FileStream(@"C:\Temp\test.zip", FileMode.Create))
   {
      memoryStream.Seek(0, SeekOrigin.Begin);
      memoryStream.CopyTo(fileStream);
   }
}

So we need to call dispose on ZipArchive before we can use it, which means passing ‘true‘ as the third parameter to the ZipArchive so we can still access the stream after disposing it.

Creating a ZIP Archive in Memory Using System.IO.Compression

标签:

原文地址:http://www.cnblogs.com/shiningrise/p/4984944.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!