How am I supposed to use ZipArchive with memory streams?

Csaba Toth picture Csaba Toth · Feb 12, 2014 · Viewed 12.6k times · Source

My problem is that as soon as ZipArchive is disposed, it automatically closes and disposes the MemoryStream. If I look at the stream before the disposal of ZipArchive the information is not well formed zip.

using (var compressStream = new MemoryStream())
{
    using (var zipArchive = new ZipArchive(compressStream, ZipArchiveMode.Create))
    {
        // Adding a couple of entries
        string navStackInfo = Navigation.NavState.CurrentStackInfoLines();
        var navStackEntry = zipArchive.CreateEntry("NavStack.txt", CompressionLevel.NoCompression);
        using (StreamWriter writer = new StreamWriter(navStackEntry.Open()))
        {
             writer.Write(navStackInfo);
        }
        var debugInfoEntry = zipArchive.CreateEntry("CallStack.txt", CompressionLevel.Optimal);
        using (StreamWriter writer = new StreamWriter(debugInfoEntry.Open()))
        {
            // debugInfo.Details is a string too
            writer.Write(debugInfo.Details);
        }
        // ...
        // compressStream here is not well formed
    }
    // compressStream here is closed and disposed
}

So how should this work? Maybe the only problem is that it's not well formed? I see "PK" header number within the file (not just at the beginning) at the beginning of each entry part. I'm not sure if that's good or not. Certainly if I save the stream to a file I cannot open it as a zip file, something is wrong. (In the final code I do not want to materialize a file in a crash handling code though.)

Answer

Jeremy picture Jeremy · Mar 2, 2015

I just ran into the same issue, and I noticed that there's an optional parameter for the ZipArchive constructor called leaveOpen. Documentation says: True to leave the stream open after the System.IO.Compression.ZipArchive object is disposed; otherwise, false.

This solved the problem for me.