How to dynamically create a ZIP archive in ASP.NET without temp file on disk?

0 votes
asked Sep 17, 2010 by Rebex KB (8,430 points)
edited Dec 6, 2011

I need to dynamically create a ZIP archive, add files to it (into different folders). This file will be sent to user's browser. Ideally without any temporary disk file.

Applies to: Rebex ZIP

1 Answer

+1 vote
answered Sep 17, 2010 by Rebex KB (8,430 points)
edited Nov 14, 2011
Best answer

Creating ZIP file "on the fly" is simple using Rebex ZIP component. Check out the following sample code:

// prepare MemoryStream to create ZIP archive within
using (MemoryStream ms = new MemoryStream())
    // create new ZIP archive within prepared MemoryStream
    using (ZipArchive zip = new ZipArchive(ms))
         // add some files to ZIP archive
         zip.Add(@"c:\temp\innerfile.txt", @"\subfolder");

         // clear response stream and set the response header and content type
         Response.ContentType = "application/zip";
         Response.AddHeader("content-disposition", "");

         // write content of the MemoryStream (created ZIP archive) 
         // to the response stream

// close the current HTTP response and stop executing this page