-1

I'm using Ionic.Zip library to zip all files into a folder, so I do:

using (ZipFile zip = new ZipFile())
{
     foreach(var i in consultaPDF)
     {
         var fileRoute = carpeta + i.vRutaArchivo;
         zip.AddFile(fileRoute, "Document");
     }

     zip.Save(Response.OutputStream);
}

That I want to do is just download zip file. I don't want to save it. How can I just download it? Regards

I change it to:

  using (ZipFile zip = new ZipFile())
            {
                    foreach(var i in consultaPDF)
                    {
                        var fileRoute = carpeta + i.vRutaArchivo;
                        zip.AddFile(fileRoute);
                    }
                    MemoryStream output = new MemoryStream();
                    zip.Save(output);
                    return File(output, "application/zip", "sample.zip");

            }

Zip is downloaded correctly but when I try to open it:

the file has an unknown format or is damaged

David
  • 1,035
  • 1
  • 7
  • 11

1 Answers1

1

You need to reset stream position to start after saving zip to it

using (ZipFile zip = new ZipFile())
{
    foreach(var i in consultaPDF)
    {
        var fileRoute = carpeta + i.vRutaArchivo;
        zip.AddFile(fileRoute);
    }
    MemoryStream output = new MemoryStream();
    zip.Save(output);
    output.Seek(0, SeekOrigin.Begin);
    return File(output, "application/zip", "sample.zip");
}
witalego
  • 551
  • 3
  • 7