5,117,952 members and growing! (14,629 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate License: The Code Project Open License (CPOL)

ASP.NET Zip Entry Handler

By Jake Morgan

Deploy zip files to your web application and serve compressed files directly out of the zip file.
C# (C# 1.0, C# 2.0, C# 3.0, C#), HTML, .NET (.NET, .NET 3.5, .NET 3.0, .NET 2.0), IIS (IIS 5, IIS 5.1, IIS 6, IIS 7, IIS), ASP.NET, Dev, Design

Posted: 8 May 2008
Updated: 8 May 2008
Views: 839
Announcements



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 3.15 Rating: 4.50 out of 5
0 votes, 0.0%
1
0 votes, 0.0%
2
1 vote, 20.0%
3
0 votes, 0.0%
4
4 votes, 80.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

Download ZipHandlerSite.zip - 98.42 KB

Introduction

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.

Background

Developers need a basic understanding of ASP.NET to use this component effectively. Knowledge of the IHttpHandler interface may be useful to any modifications to standard behavior.

Using the code

The handler is configured with a single line in the web.config file:

<configuration>
    <system.web>
        <httpHandlers>
            <add path="ZipEntry.axd" verb="GET" type="Elsinore.Website.ZipEntryHandler" />
        </httpHandlers>
    </system.web>
</configuration>

Once configured, zip entries are available as URLs. An example URL:

http://www.mysite.com/ZipEntry.axd?ZipFile=test.zip&ZipEntry=test.cs

This can be broken down as follows:

http://www.mysite.com/<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)
    {
        base.OnLoad(e);

        this.zipFileRepeater.DataSource = Elsinore.Website.Utility.GetVirtualFilePaths("~/Content", this.Context);
        this.zipFileRepeater.DataBind();
    }
    
</script>

<html>
<head>
    <title>Zip Entry Example Page</title>
</head>
<body>
    <asp:Repeater runat="server" ID="zipFileRepeater">
        <ItemTemplate>
            <p>
                <asp:HyperLink runat="server" NavigateUrl="<%# Container.DataItem %>">
                    Download <%# Container.DataItem %>
                </asp:HyperLink>
            </p>
            <p>
                Contents:</p>
            <ul>
                <asp:Repeater runat="server" DataSource="<%# Elsinore.Website.ZipEntryHandler.GetZipEntries((string)Container.DataItem, this.Context) %>">
                    <ItemTemplate>
                        <li>
                            <asp:HyperLink runat="server" NavigateUrl="<%# ((Elsinore.Website.ZipEntryInfo)Container.DataItem).Url %>">
                                View <%# ((Elsinore.Website.ZipEntryInfo)Container.DataItem).Name %>
                            </asp:HyperLink>
                        </li>
                    </ItemTemplate>
                </asp:Repeater>
            </ul>
            <br />
        </ItemTemplate>
    </asp:Repeater>
</body>
</html>

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:

ZipEntryHandlerScreenshot.png

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.

License

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

About the Author

Jake Morgan


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 TheWolfWeb.com, 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.

Occupation: Chief Technology Officer
Company: Elsinore Technologies, The Issue Management Expert
Location: United States United States

Other popular ASP.NET articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralTry hardermember leppie 5:48 8 May '08  
GeneralRe: Try hardermemberJake Morgan5:58 8 May '08  
GeneralRe: Try harderadminChris Maunder6:10 8 May '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 8 May 2008
Editor:
Copyright 2008 by Jake Morgan
Everything else Copyright © CodeProject, 1999-2008
Web11 | Advertise on the Code Project