0

NET MVC 4 application and in this app I have to download multiple files represented by a List of byte arrays in a Zip file , so I used the DotNetZip library. when I execute this piece if code :

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

at this line :

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

I got this error :

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:

Haytham
  • 834
  • 1
  • 12
  • 31

1 Answers1

1

You don't need to call zipStream.Write, as you have passed the data in the .AddEntry call already.

This works for me:

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");
}
paul
  • 21,653
  • 1
  • 53
  • 54
  • Thank you for your response , it worked to download the archive but when I open the zip and try to open or extract the files i found out that the files cannot be opened because it is corrupted ... what is the cause of such problem ? – Haytham Apr 23 '18 at 17:52
  • You could check that your byte arrays are valid pdfs, Also check whether the files in the zip have the same lengths as your byte arrays – paul Apr 23 '18 at 18:04
  • Yes when i opened the archive and checked the size of the files i can see that they kept their initial size that means that there is no lost data :/ but the problem still there – Haytham Apr 23 '18 at 20:41