Rachel Sanchez
Rachel Sanchez

Reputation: 21

Creating a ZIP Archive in Memory in C#

I'm trying to create a ZIP archive with multiple pdf files as follows:

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", "attachment;filename=ExpedienteProyecto.zip");
using (var memoryStream = new MemoryStream())
{
    using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
    {
        var demoFile = archive.CreateEntry(nombreReporte + "_" + DateTime.Now.ToShortDateString() + formatoReporte["ext"].ToString());
        using (var originalFileStream = new MemoryStream(renderedBytes))
        using (var ZipEntryStream = demoFile.Open())
        {
            originalFileStream.CopyTo(ZipEntryStream);
        }
    }
    using (var fileStream = Response.OutputStream)
    {
        memoryStream.Seek(0, SeekOrigin.Begin);
        memoryStream.CopyTo(fileStream);
    }
}

Response.End();

However, the ZIP is created only with the last or the first text file, what happens?

Upvotes: 0

Views: 207

Answers (0)

Related Questions