Click here to Skip to main content
13,505,369 members
Click here to Skip to main content
Add your own
alternative version


45 bookmarked
Posted 8 May 2008

ASP.NET Zip Entry Handler

, 8 May 2008
Rate this:
Please Sign up or sign in to vote.
Deploy zip files to your web application and serve compressed files directly out of the zip file.


The 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:<HandlerPath>?ZipFile=<ZipFileVirutalPath>&ZipEntry=<ZipEntryPath>

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#" %>

<script runat="server">
    protected override void OnLoad(EventArgs e)

        this.zipFileRepeater.DataSource = 
          Elsinore.Website.Utility.GetVirtualFilePaths("~/Content", this.Context);

    <title>Zip Entry Example Page</title>
    <asp:Repeater runat="server" ID="zipFileRepeater">
                <asp:HyperLink runat="server" NavigateUrl="<%# Container.DataItem %>">
                    Download <%# Container.DataItem %>
                <asp:Repeater runat="server" 

                     DataSource="<%# Elsinore.Website.ZipEntryHandler.GetZipEntries(
                                      (string)Container.DataItem, this.Context) %>">
                            <asp:HyperLink runat="server" 

                                   NavigateUrl="<%# ((Elsinore.Website.ZipEntryInfo)
                                                      Container.DataItem).Url %>">
                                View <%# ((Elsinore.Website.ZipEntryInfo)
                                           Container.DataItem).Name %>
            <br />

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.Compression and 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.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jake Morgan
Chief Technology Officer Elsinore Technologies, The Issue Management Expert
United States United States
Jake Morgan
Chief Technology Officer

CTO Jake Morgan brings a diverse background and over 7 years of software development experience to Elsinore, having created successful software applications in both the public and private sector, and founding a wildly popular online community.

Before joining Elsinore in 2005, Jake led the design and development of the NC FAST Online Verification system, used by the NC Department of Health and Human Services to verify eligibility for billions of dollars in benefits. He also spent time at Nortel Networks and founded the, a vibrant online community for NC State students, which supported over 15 million page views a month. At Elsinore he oversees the design and development of IssueNet Issue Management Software.

Jake is an alumnus of NC State University, where he received a BS in Mechanical Engineering.

You may also be interested in...

Comments and Discussions

GeneralFile types served Pin
Dewey27-May-08 14:16
memberDewey27-May-08 14:16 
GeneralRe: File types served Pin
Jake Morgan28-May-08 3:22
memberJake Morgan28-May-08 3:22 
GeneralThis is awesome Pin
Sacha Barber14-May-08 2:41
mvpSacha Barber14-May-08 2:41 
GeneralExcellent! Pin
Antony M Kancidrowski14-May-08 2:13
memberAntony M Kancidrowski14-May-08 2:13 
GeneralTry harder Pin
leppie8-May-08 4:48
member leppie 8-May-08 4:48 
GeneralRe: Try harder Pin
Jake Morgan8-May-08 4:58
memberJake Morgan8-May-08 4:58 
GeneralRe: Try harder Pin
Chris Maunder8-May-08 5:10
adminChris Maunder8-May-08 5:10 
GeneralRe: Try harder Pin
Sacha Barber14-May-08 2:43
mvpSacha Barber14-May-08 2:43 
GeneralRe: Try harder Pin
Chris Maunder14-May-08 3:02
adminChris Maunder14-May-08 3:02 
GeneralRe: Try harder Pin
Sacha Barber14-May-08 3:08
mvpSacha Barber14-May-08 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180417.1 | Last Updated 8 May 2008
Article Copyright 2008 by Jake Morgan
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid