Invalid zip file after creating it with System.IO.Compression

后端 未结 4 1697
死守一世寂寞
死守一世寂寞 2021-02-12 15:58

I\'m trying to create a zip file that contains one or more files.
I\'m using the .NET framework 4.5 and more specifically System.IO.Compression namespace.
The objectiv

4条回答
  •  野的像风
    2021-02-12 16:08

    Just return the stream...

    private ActionResult CreateZip(IEnumerable files)
    {
        if (files.Any())
        {
            MemoryStream zipStream = new MemoryStream();
            using (ZipArchive archive = new ZipArchive(zipStream, ZipArchiveMode.Create, false))
            {
                foreach (var f in files)
                {
                   var entry = archive.CreateEntry(f.FileDownloadName, CompressionLevel.Fastest);
                   using (var entryStream = entry.Open())
                   {
                       entryStream.Write(f.FileContents, 0, f.FileContents.Length);
                       entryStream.Close();
                   }
               }
    
            }
    
            zipStream.Position = 0;
            return File(zipStream, MediaTypeNames.Application.Zip, "horta.zip");
        }
    
        return new EmptyResult();
    }

提交回复
热议问题