6

In my application I have a class manipulating with zip archive using System.IO.Compression.ZipArchive. Now I want to save the entries of the archive to file. How can I do that? I know I can associate a Stream with archive but in my case the archive creates w/o streams and I can't change the code:

var archive = ZipFile.Open(fileName, ZipArchiveMode.Create);
// add entries
// how can I save it to file now?
folibis
  • 12,048
  • 6
  • 54
  • 97

1 Answers1

4

You are already 'saving it to a file', the one indicated by fileName.

To make sure everything is written and flushed, use a using :

using (var archive = ZipFile.Open(fileName, ZipArchiveMode.Create))
{
   // add entries
   ....

}  // here it is saved and closed
H H
  • 263,252
  • 30
  • 330
  • 514
  • Thanks, @Henk Holterman, you're right, I've noticed that this code creates a file. Unfortunately it's empty although I add several `ZipArchiveEntry` items. – folibis Feb 28 '17 at 06:53
  • 1
    It's ok, I've found a way to do that. In my code I didn't use `using` so it looks that file just remained opened. I've added `archive.Dispose()` in my class destructor and now it work fine, archive created as expected. – folibis Feb 28 '17 at 08:21