DotNetZip异常坏状态(无效的文字/长度代码)

时间:2018-04-23 16:34:43

标签: c# asp.net-mvc dotnetzip

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);

我收到了这个错误:

  

状态不佳(文字/长度代码无效)

     

Ionic.Zlib.ZlibException未被用户代码
处理   HResult = -2146233088消息=错误状态(无效的文字/长度代码)   Source = DotNetZip StackTrace:          在Ionic.Zlib.InflateManager.Inflate(FlushType flush)          在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)          在Ionic.Crc.CrcCalculatorStream.Write(Byte []缓冲区,Int32偏移量,Int32计数)          在Host.Web.Areas.FranceArea.Controllers.FranceController.downloadMultiplefile()   在   C:\项目\ Oddo.Web.OBAM_Icn \主机\ Host.Web \区\ FranceArea \ \控制器FranceController.cs:行   503          在lambda_method(Closure,ControllerBase,Object [])          在System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase控制器,Object []参数)          在System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext   controllerContext,IDictionary 2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary 2   参数)          在System.Web.Mvc.Async.AsyncControllerActionInvoker。&lt;&gt; c__DisplayClass42.b__41()          在System.Web.Mvc.Async.AsyncResultWrapper。&lt;&gt; c__DisplayClass8 1.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult 1.End()          在System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult)   asyncResult)          在System.Web.Mvc.Async.AsyncControllerActionInvoker。&lt;&gt; c__DisplayClass37。&lt;&gt; c__DisplayClass39.b__33()          在System.Web.Mvc.Async.AsyncControllerActionInvoker。&lt;&gt; c__DisplayClass4f.b__49()   的InnerException:

1 个答案:

答案 0 :(得分:1)

您无需致电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");
}