Ziparchive:如何从 ziparchive 关闭创建的条目

Md *_*lam 1 c# memorystream ziparchive

我编写了如下方法来将多个 Memorystream 绑定到 ziparchive。该代码适用于一个流,但如果我通过迭代添加多个流,则它会在 for 循环的第二行中显示以下错误。

 System.IO.IOException: 'Entries cannot be created 
 while previously created entries are still open.' 
Run Code Online (Sandbox Code Playgroud)

我的代码,

 using (var zip = new ZipArchive(outputStream, ZipArchiveMode.Create, 
 leaveOpen: false))
  {

        for (int i = 0; i < msList.Count; i++)
        {
          msList[i].Position = 0;
         var createenter = zip.CreateEntry("123"+i+".jpg", 
         CompressionLevel.Optimal);
         msList[i].CopyTo(createenter.Open());

         }
   }
Run Code Online (Sandbox Code Playgroud)

stu*_*bax 8

可能错过using了打开Stream

 using (var zip = new ZipArchive(outputStream, ZipArchiveMode.Create, leaveOpen: false))
 {
    for (int i = 0; i < msList.Count; i++)
    {
        msList[i].Position = 0;
        var createenter = zip.CreateEntry("123"+i+".jpg", 
        CompressionLevel.Optimal);
        using (var s = createenter.Open())
        {
            msList[i].CopyTo(s);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)