Create Zip archive from multiple in memory files in C#

后端 未结 8 1901
情话喂你
情话喂你 2020-12-13 02:06

Is there a way to create a Zip archive that contains multiple files, when the files are currently in memory? The files I want to save are really just text only and are stor

8条回答
  •  轮回少年
    2020-12-13 03:07

    I was utilizing Cheeso's answer by adding MemoryStreams as the source of the different Excel files. When I downloaded the zip, the files had nothing in them. This could be the way we were getting around trying to create and download a file over AJAX.

    To get the contents of the different Excel files to be included in the Zip, I had to add each of the files as a byte[].

    using (var memoryStream = new MemoryStream())
    using (var zip = new ZipFile())
    {
        zip.AddEntry("Excel File 1.xlsx", excelFileStream1.ToArray());
        zip.AddEntry("Excel File 2.xlsx", excelFileStream2.ToArray());
    
        // Keep the file off of disk, and in memory.
        zip.Save(memoryStream);
    }
    

提交回复
热议问题