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

[Originally Posted From] :

Thanks to 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))

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

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.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s