65.9K
CodeProject 正在变化。 阅读更多。
Home

ASP.NET Zip Entry Handler

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.94/5 (16投票s)

2008年5月8日

CPOL

2分钟阅读

viewsIcon

61639

downloadIcon

453

将 zip 文件部署到您的 Web 应用程序,并直接从 zip 文件中提供压缩文件。

引言

ZipEntryHandler 枚举并提供部署在您的 Web 空间中的静态 zip 文件中的文件。 该处理程序被实现为简单的 ASP.NET IHttpHandler,与代码文件一起部署在您网站的 App_Code 目录中,并在 web.config 文件中配置了一行。

该处理程序对于多种用途非常有用

  • 提供其他禁止的文件 - 扩展名对 Web 应用程序有特殊含义的文件(“cs”、“vb”、“aspx”)可以静态提供。
  • 存储空间 - 非常可压缩的内容通常会消耗大量 Web 空间。 此处理程序允许存储压缩内容,而无需特殊访问 Web 服务器上的文件系统。
  • 可维护性 - 通常,zip 文件和 zip 文件的内容都应该可下载和浏览。 这种模式允许仅部署 zip 文件,而无需同步其他下载。

我的公司网站将该处理程序用于我们的 Developer Labs 区域,用户可以通过 zip 文件下载示例,或者作为单个文件供仅浏览概念的用户使用。

背景

开发人员需要基本了解 ASP.NET 才能有效使用此组件。 了解 IHttpHandler 接口可能对任何修改标准行为都很有用。

使用代码

该处理程序通过在 web.config 文件中添加一行来配置

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

配置完成后,zip 条目可用作 URL。 一个示例 URL

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

这可以分解如下

http://www.mysite.com/<HandlerPath>?ZipFile=<ZipFileVirutalPath>&ZipEntry=<ZipEntryPath>

这些 URL 可以像上面那样进行硬编码,也可以使用 ZipEntryHandler 类上的方法来生成 URL。 以下简单的 ASPX 页面演示了以编程方式枚举 zip 条目

<%@ 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>

在上面的代码中,外部 Repeater 通过枚举 Content 目录中的 zip 文件来驱动。 内部 Repeater 使用 ZipEntryHandler.GetZipEntries 方法来枚举每个 zip 文件中的条目。 文件呈现如下

ZipEntryHandlerScreenshot.png

“查看”链接提供对 zip 文件内容的快速简便的访问。

关注点

我希望使用新的 System.IO.CompressionSystem.IO.Packaging 命名空间中的类来构建处理程序。 尽管 System.IO.Compression 流足以解压缩压缩流,但 System.IO.Packaging.ZipPackage 类与标准 zip 文件不兼容。 该类需要一个元数据文件作为 zip 文件中的一个条目,其中包含有关每个 zip 条目的扩展属性。 我回到了 SharpZipLib 开源项目中的类。

© . All rights reserved.