ZipEntryHandler enumerates and serves files out of static zip files deployed in your web space. The handler is implemented as a simple ASP.NET
IHttpHandler deployed with a code file in the App_Code directory on your website, and configured with a line in the web.config file.
The handler is very useful for several purposes:
- Serving otherwise prohibited files - Code files with extensions that have special meaning to the web application ("cs", "vb", "aspx") can be served statically.
- Storage space - Very compressible content often consumes large amounts of web space. This handler allows content to be stored compressed without special access to the file system on a web server.
- Maintainability - Often, a zip file and the contents of the zip file should both be downloadable and browsable. This pattern allows solely the zip file to be deployed without having to synchronize other downloads.
My company website uses the handler for our Developer Labs area, where samples are available through zip files for users to download, or as single files for users who just browse concepts.
Developers need a basic understanding of ASP.NET to use this component effectively. Knowledge of the
IHttpHandler interface may be useful for any modifications to standard behavior.
Using the code
The handler is configured with a single line in the web.config file:
<add path="ZipEntry.axd" verb="GET" type="Elsinore.Website.ZipEntryHandler" />
Once configured, zip entries are available as URLs. An example URL:
This can be broken down as follows:
These URLs can be hardcoded as above, or methods on the
ZipEntryHandler class can be used to generate the URLs. The following simple ASPX page demonstrates enumerating zip entries programmatically:
<%@ Page Language="C#" %>
protected override void OnLoad(EventArgs e)
<title>Zip Entry Example Page</title>
<asp:Repeater runat="server" ID="zipFileRepeater">
<asp:HyperLink runat="server" NavigateUrl="<%# Container.DataItem %>">
Download <%# Container.DataItem %>
(string)Container.DataItem, this.Context) %>">
View <%# ((Elsinore.Website.ZipEntryInfo)
In the code above, the outer
Repeater is driven by enumerating the zip files in the Content directory. The inner
Repeater uses the
ZipEntryHandler.GetZipEntries method to enumerate over the entries in each zip file. The file is rendered like the following:
The "View" links provide quick-and-easy access to the contents of the zip file.
Points of interest
I hoped to use classes in the new
System.IO.Packaging namespaces to build the handler. Although the
System.IO.Compression streams were sufficient for unpacking the compressed streams, the
System.IO.Packaging.ZipPackage class is not compatible with standard zip files. The class requires a metadata file as an entry in the zip file that contains extended properties about each of the zip entries. I reverted to classes in the SharpZipLib open-source project.