I previously gave a glimpse of how to zip into an HttpResponse's OutputStream, but it wasn't explaining all aspects of zipping from ASP.NET. So I'll get in more details here.
First, I have used my fantastic talent in UI designs to create this web page:
Yup, three checkboxes and a button is enough gadgets for me!
The first piece of code involves Application_Start. Since I know I won't be zipping gazillions of bytes, I want my web page to use memory as a temporary location for compressed data. How you do this with Xceed Zip for .NET is simple: You create a RAM drive! Oh the good old days of RAM drives...
protected void Application_Start(Object sender, EventArgs e)
Xceed.Zip.Licenser.LicenseKey = "ZIN23-#####-#####-####";
ZipArchive.DefaultTempFolder = new MemoryFolder();
This new MemoryFolder is acting exactly like a per-process RAM drive. It's an AbstractFolder like any other AbstractFolder. The TempFolder of all new ZipArchive instances will be initialized to that value. Application_Start is also a great place where to set your license key, before anything else.
We're now ready for the button's click event. Again, I want to avoid write access on the hard drive, and wish to zip directly in the response stream. But the idea behind the Xceed FileSystem is to copy source files and folders to destination files and folders. How can I zip into a Stream? The StreamFile class comes to the rescue. It lets you expose a Stream as if it were an AbstractFile. Then, you can pass this StreamFile to the ZipArchive's constructor, to tell that new instance to write into that Stream. The rest is glue code for my wonderful ASP.NET application to zip the correct files.
private void Button1_Click(object sender, System.EventArgs e)
if( !CheckBox1.Checked && !CheckBox2.Checked && !CheckBox3.Checked )
// Redirect to error page...
// The "MACHINE\ASP_NET" user must have read access to that folder.
DiskFolder source = new DiskFolder( @"d:\" );
// We want the client-side to recognize the upcoming file as a zip file.
this.Response.ContentType = "application/zip";
this.Response.AddHeader( "Content-Disposition", "attachment; filename=YourFiles.zip" );
// We will zip directly in the response stream. The temporary compressed
// data will be written to the ZipArchive's TempFolder, thus the MemoryFolder
// we set in Application_Start.
ZipArchive destination = new ZipArchive( new StreamFile( this.Response.OutputStream ) );
// And finally we zip in a single operation. If we had to zip more than
// one source, we could have used ZipArchive.BeginUpdate/EndUpdate.
ArrayList nameFilters = new ArrayList();
if( CheckBox1.Checked )
nameFilters.Add( new NameFilter( "*.txt" ) );
if( CheckBox2.Checked )
nameFilters.Add( new NameFilter( "*.jpg" ) );
if( CheckBox3.Checked )
nameFilters.Add( new NameFilter( "*.exe|*.dll" ) );
// Passing more than one filter to CopyFilesTo does an "AndFilter"
// by default.
Filter mainFilter = ( nameFilters.Count == 1 )
? nameFilters[ 0 ] as Filter
: new OrFilter( nameFilters.ToArray( typeof( NameFilter ) ) );
source.CopyFilesTo( destination, false, true, mainFilter );
We now have an ASP.NET application which only requires read access to the source files and folders to zip. Everything else is done in memory, without drifting away from the logic of the Xceed FileSystem; manipulating files and folders.