我应该如何将ZipArchive与内存流一起使用?

Csa*_*oth 9 c# memorystream ziparchive

我的问题是,一旦ZipArchive处理完毕,它就会自动关闭并处理掉MemoryStream.如果我在处理ZipArchive信息之前看一下流不完整的拉链.

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
}
Run Code Online (Sandbox Code Playgroud)

那应该怎么做呢?也许唯一的问题是它不是很好吗?我看"PK"头号码的文件(不只是开头)在每个入口部分的开头.我不确定这是好还是不好.当然,如果我将流保存到文件中,我无法将其作为zip文件打开,那就错了.(在最终的代码中,我不想在崩溃处理代码中实现文件.)

Jer*_*emy 14

我刚遇到同样的问题,我注意到有一个ZipArchive名为的构造函数的可选参数leaveOpen.文件说:True to leave the stream open after the System.IO.Compression.ZipArchive object is disposed; otherwise, false.

这解决了我的问题.