使用 DotNetZip 或 SharpZipLib 在经典 ASP 中创建 Zip 文件

发布于 2024-09-15 01:35:39 字数 303 浏览 5 评论 0原文

在 ASP.Net 中,制作 zip 文件的两种可能方法是

Sharp Zip Library

< a href="http://dotnetzip.codeplex.com/" rel="nofollow noreferrer">Dot Net Zip Library

我如何在经典 ASP 中使用其中任何一个来制作 zip 文件?哪一个会更好?

In ASP.Net two of the possible ways to make the zip files are

Sharp Zip Library

Dot Net Zip Library

How can i use any of these in Classic ASP to make the zip files ? And which one will be better ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鱼窥荷 2024-09-22 01:35:39

您不能在经典 asp 中使用任何这些库。一种方法是尝试使用 regasm.exe 实用程序,尽管我不确定这些程序集是否标有 COMVisible 属性。

You can't use any of those libraries in classic asp. One way would be to try to register them as COM components using the regasm.exe utility, although I am not sure whether those assemblies are marked with the COMVisible attribute.

蓝天白云 2024-09-22 01:35:39

您需要为 .NET 库创建一个 COM 包装选择。

然后,您将能够在经典 ASP 中引用该包装器并使用它。

You will need to create a COM wrapper for the .NET library of choice.

You will then be able to reference the wrapper in classic ASP and use it.

断舍离 2024-09-22 01:35:39

与某些看法相反,在 COM 环境(包括 ASP)中使用 DotNetZip 很容易。

DotNetZip 包含展示如何从 VBScript 和 Javascript 创建 zip 文件的示例。
您可以轻松地改编此代码以使用 ASP classic 中的代码。

源代码中还有一个示例,用于从 ASP 经典页面中读取 ZIP 文件。它看起来像这样:

<%@ LANGUAGE = VBScript %>
<%  Option Explicit %>
<%

' -------------------------------------------------------
' ASP DotNetZip Example
' -------------------------------------------------------
' This example ASP page uses DotNetZip (Ionic.Zip.dll) via COM
' interop.  The page opens a zip file, then allows the user
' to download any individual file within the zip file. 
' 
'' To get this to work, you must be sure to register DotNetZip for COM
'' interop (regasm).  Also you need to be sure that IIS/ASP has the correct
'' permissions to instantiate the ZipFile object.  In my experience I Was
'' able to do this by copying Ionic.Zip.dll to the
'' c:\windows\system32\inetsrv directory, then calling "regasm /codebbase
'' Ionic.Zip.dll" from within that directory.

'' This example assumes that the ASP page is deployed into a directory,
'' that contains a subdirectory called "fodder".  Fodder must be readable,
'' and should contain one or more zip files.  This page allows the user to
'' select a zip file, then select a file within the zip file, and download
'' that file.
''

If Request.Form("Submit") = "Download" Then 
   dim pathForZipFile, fileToDownload
   pathForZipFile= Request.Form("zipFile")
   if pathForZipFile <> "" Then
     fileToDownload = Request.Form("fileToDownload")
     Response.Clear
     Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload  
     Response.ContentType = "application/octet-stream"  

     pathForZipFile = Server.MapPath("fodder\" & pathForZipFile)

     dim zip, ms
     set zip = Server.CreateObject("Ionic.Zip.ZipFile")
     zip.Initialize(pathForZipFile)

     set ms = Server.CreateObject("System.IO.MemoryStream")

     dim selectedEntry, entry
     For Each entry in zip
         If entry.FileName = fileToDownload  Then 
           set selectedEntry = entry 
         End If 
     Next

     selectedEntry.Extract_3(ms)
     zip.Dispose

     dim fred
     fred = ms.ToArray

     Response.BinaryWrite(fred)
     ms.Dispose
   End If

Else

%>

<html>
   <HEAD>
<TITLE>Simple DotNetZip Example</TITLE>
<style>
BODY { font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10pt;}
TD {  font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 8pt;}
TH {  font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 10pt;}
H2 { font-size: 16pt; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color:Navy;}
H1 { font-size: 20pt; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color:Blue;}
</style>


   <script language="Javascript">

    function Download(file, zipFile)
    {
        document.form1.fileToDownload.value = file;
        document.form1.zipFile.value = zipFile;
        document.form1.submit();
    }

   </script>



   <script RUNAT=Server language="VBScript">

   '-------------------------------------
   ' This reads the given zip file. 
   '-------------------------------------
   Sub DisplayContentsOfZip
    dim pathForZipFile
    pathForZipFile= Request.Form("selectedZip")
    if pathForZipFile <> "" Then
        pathForZipFile = Server.MapPath("fodder\" & pathForZipFile)

        dim zip
        set zip = Server.CreateObject("Ionic.Zip.ZipFile")
        zip.Initialize(pathForZipFile)

        response.write "<table border=1><tr><th>Name</th><th>last modified</th></tr>"

        dim entry, fN
        For Each entry in zip
         If Right(entry.FileName,1) <> "/" Then 
             Response.Write "<tr><TD><input type='submit' name='Submit' value='Download' onClick=" & chr(34) & "Download('" & _
             entry.FileName & "', '" & Request.Form("selectedZip")  & "');" & chr(34) & " ></TD><td>" & _
             entry.FileName & "</td><td>" & entry.LastModified & "</td></tr>"
         End If
        Next
        response.write "</table>"
        zip.Dispose()
    End If
   End Sub


   '-------------------------------------
   ' This function builds and returns the 
   ' option list for the form. eg:
   '    <OPTION value="file1.zip">file1.zip</OPTION>
   '    <OPTION value="file2.zip">file2.zip</OPTION>
   '    <OPTION value="file3.zip">file3.zip</OPTION>
   '-------------------------------------
   Function FileList()
     Dim fso, folder, ext, item, result
     result = ""
     Set fso= Server.CreateObject("Scripting.FileSystemObject")
     Set folder = FSO.GetFolder(Server.MapPath("fodder"))
     For Each item In folder.Files
         ext = Right(item.Name,4)
         If (ext = ".zip") Then
           result = result & "<OPTION value='" & item.Name & "'>" & item.Name & "</OPTION>"
         End If
     Next 
     Set fso = Nothing
     Set folder = Nothing
     FileList = result
   End Function

   </script>

   </HEAD>

<body>
<h1>ASP DotNetZip</h1>

<p> This page shows how to use <a
href="http://DotNetZip.codeplex.com">DotNetZip</a> from an ASP (Classic)
page.  This page reads zip files and allows the browser to download
items from the zip files.  </p>

<form METHOD="POST" id='form1' name='form1'>

   <TABLE style="border:1; cellspacing:1; cellpadding:1;">
<TR> <TD>Select a Zip file:</TD>
  <TD><SELECT id='select1' name='selectedZip'>
    <%= fileList %>
    </SELECT>
  </TD>
</TR>
<TR> <TD/><TD><input type='submit' name="Submit" Value="Read Zip"/></TD> </TR>
   </TABLE>

   <input type="hidden" name="fileToDownload" value="">
   <input type="hidden" name="zipFile" value="">

<%
DisplayContentsOfZip
%>

</form>

</body>
</html>

<%
End If
%> 

要查看此示例的最新版本,请转到 http://dotnetzip.codeplex .com/SourceControl/list/changesets 并单击“浏览”,然后展开名为“Examples”的目录。

Contrary to some perception, it's easy to use DotNetZip from COM environments, including ASP.

DotNetZip includes examples that show creating zip files from VBScript and Javascript.
You can easily adapt this code to use it from ASP classic.

There is also an example in the source code for reading a ZIP file from within an ASP classic page. It looks like this:

<%@ LANGUAGE = VBScript %>
<%  Option Explicit %>
<%

' -------------------------------------------------------
' ASP DotNetZip Example
' -------------------------------------------------------
' This example ASP page uses DotNetZip (Ionic.Zip.dll) via COM
' interop.  The page opens a zip file, then allows the user
' to download any individual file within the zip file. 
' 
'' To get this to work, you must be sure to register DotNetZip for COM
'' interop (regasm).  Also you need to be sure that IIS/ASP has the correct
'' permissions to instantiate the ZipFile object.  In my experience I Was
'' able to do this by copying Ionic.Zip.dll to the
'' c:\windows\system32\inetsrv directory, then calling "regasm /codebbase
'' Ionic.Zip.dll" from within that directory.

'' This example assumes that the ASP page is deployed into a directory,
'' that contains a subdirectory called "fodder".  Fodder must be readable,
'' and should contain one or more zip files.  This page allows the user to
'' select a zip file, then select a file within the zip file, and download
'' that file.
''

If Request.Form("Submit") = "Download" Then 
   dim pathForZipFile, fileToDownload
   pathForZipFile= Request.Form("zipFile")
   if pathForZipFile <> "" Then
     fileToDownload = Request.Form("fileToDownload")
     Response.Clear
     Response.AddHeader "Content-Disposition", "attachment; filename=" & fileToDownload  
     Response.ContentType = "application/octet-stream"  

     pathForZipFile = Server.MapPath("fodder\" & pathForZipFile)

     dim zip, ms
     set zip = Server.CreateObject("Ionic.Zip.ZipFile")
     zip.Initialize(pathForZipFile)

     set ms = Server.CreateObject("System.IO.MemoryStream")

     dim selectedEntry, entry
     For Each entry in zip
         If entry.FileName = fileToDownload  Then 
           set selectedEntry = entry 
         End If 
     Next

     selectedEntry.Extract_3(ms)
     zip.Dispose

     dim fred
     fred = ms.ToArray

     Response.BinaryWrite(fred)
     ms.Dispose
   End If

Else

%>

<html>
   <HEAD>
<TITLE>Simple DotNetZip Example</TITLE>
<style>
BODY { font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 10pt;}
TD {  font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 8pt;}
TH {  font-family: Verdana, Arial, Helvetica, sans-serif;  font-size: 10pt;}
H2 { font-size: 16pt; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color:Navy;}
H1 { font-size: 20pt; font-weight: bold; font-family: Verdana, Arial, Helvetica, sans-serif; color:Blue;}
</style>


   <script language="Javascript">

    function Download(file, zipFile)
    {
        document.form1.fileToDownload.value = file;
        document.form1.zipFile.value = zipFile;
        document.form1.submit();
    }

   </script>



   <script RUNAT=Server language="VBScript">

   '-------------------------------------
   ' This reads the given zip file. 
   '-------------------------------------
   Sub DisplayContentsOfZip
    dim pathForZipFile
    pathForZipFile= Request.Form("selectedZip")
    if pathForZipFile <> "" Then
        pathForZipFile = Server.MapPath("fodder\" & pathForZipFile)

        dim zip
        set zip = Server.CreateObject("Ionic.Zip.ZipFile")
        zip.Initialize(pathForZipFile)

        response.write "<table border=1><tr><th>Name</th><th>last modified</th></tr>"

        dim entry, fN
        For Each entry in zip
         If Right(entry.FileName,1) <> "/" Then 
             Response.Write "<tr><TD><input type='submit' name='Submit' value='Download' onClick=" & chr(34) & "Download('" & _
             entry.FileName & "', '" & Request.Form("selectedZip")  & "');" & chr(34) & " ></TD><td>" & _
             entry.FileName & "</td><td>" & entry.LastModified & "</td></tr>"
         End If
        Next
        response.write "</table>"
        zip.Dispose()
    End If
   End Sub


   '-------------------------------------
   ' This function builds and returns the 
   ' option list for the form. eg:
   '    <OPTION value="file1.zip">file1.zip</OPTION>
   '    <OPTION value="file2.zip">file2.zip</OPTION>
   '    <OPTION value="file3.zip">file3.zip</OPTION>
   '-------------------------------------
   Function FileList()
     Dim fso, folder, ext, item, result
     result = ""
     Set fso= Server.CreateObject("Scripting.FileSystemObject")
     Set folder = FSO.GetFolder(Server.MapPath("fodder"))
     For Each item In folder.Files
         ext = Right(item.Name,4)
         If (ext = ".zip") Then
           result = result & "<OPTION value='" & item.Name & "'>" & item.Name & "</OPTION>"
         End If
     Next 
     Set fso = Nothing
     Set folder = Nothing
     FileList = result
   End Function

   </script>

   </HEAD>

<body>
<h1>ASP DotNetZip</h1>

<p> This page shows how to use <a
href="http://DotNetZip.codeplex.com">DotNetZip</a> from an ASP (Classic)
page.  This page reads zip files and allows the browser to download
items from the zip files.  </p>

<form METHOD="POST" id='form1' name='form1'>

   <TABLE style="border:1; cellspacing:1; cellpadding:1;">
<TR> <TD>Select a Zip file:</TD>
  <TD><SELECT id='select1' name='selectedZip'>
    <%= fileList %>
    </SELECT>
  </TD>
</TR>
<TR> <TD/><TD><input type='submit' name="Submit" Value="Read Zip"/></TD> </TR>
   </TABLE>

   <input type="hidden" name="fileToDownload" value="">
   <input type="hidden" name="zipFile" value="">

<%
DisplayContentsOfZip
%>

</form>

</body>
</html>

<%
End If
%> 

To see the latest version of this example, go to http://dotnetzip.codeplex.com/SourceControl/list/changesets and click on "Browse", then expand the directory called "Examples".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文