DotNetZip 异常错误状态(无效 literal/length 代码)

DotNetZip Exception Bad state (invalid literal/length code)

NET MVC 4 应用程序,在此应用程序中,我必须下载 Zip 文件中由字节数组列表表示的多个文件,因此我使用了 DotNetZip 库。当我执行这段代码时:

 using (MemoryStream ms = new MemoryStream())
       {
        using (var archive = new ZipFile())
        {
         foreach (var item in byteArrays)
         {
          var entry = archive.AddEntry("file" + i + ".pdf", item);
          i++;
          archive.Save("All.zip");
           using (var zipStream = entry.OpenReader())
           {
           zipStream.Write(item, 0, item.Length);
          }
         }
        }
        return File(ms.ToArray(), "application/zip", "all.zip");
        }

在这一行:

zipStream.Write(item, 0, item.Length);

我遇到了这个错误:

Bad state (invalid literal/length code)

Ionic.Zlib.ZlibException was unhandled by user code
HResult=-2146233088 Message=Bad state (invalid literal/length code) Source=DotNetZip StackTrace: at Ionic.Zlib.InflateManager.Inflate(FlushType flush) at Ionic.Zlib.ZlibCodec.Inflate(FlushType flush) at Ionic.Zlib.ZlibBaseStream.Write(Byte[] buffer, Int32 offset, Int32 count) at Ionic.Zlib.DeflateStream.Write(Byte[] buffer, Int32 offset, Int32 count) at Ionic.Crc.CrcCalculatorStream.Write(Byte[] buffer, Int32 offset, Int32 count) at Host.Web.Areas.FranceArea.Controllers.FranceController.downloadMultiplefile() in C:\Projects\Oddo.Web.OBAM_Icn\Host\Host.Web\Areas\FranceArea\Controllers\FranceController.cs:line 503 at lambda_method(Closure , ControllerBase , Object[] ) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.b__41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.b__33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.b__49() InnerException:

您不需要调用 zipStream.Write,因为您已经在 .AddEntry 调用中传递了数据。

这对我有用:

var i = 1;

using (var outStream = new MemoryStream())
{
    using (var archive = new ZipFile())
    {
        foreach (var item in byteArrays)
        {
            using (MemoryStream ms = new MemoryStream(item))
            {
                var entry = archive.AddEntry($"file{i++}.pdf", ms);
                archive.Save(outStream);
            }
        }
    }
    outStream.Position = 0;
    return File(outStream.ToArray(), "application/zip", "all.zip");
}