Description of Advanced Zip Demo

There are three scripts in this demo. The first lists all the files in the source directory and displays them in a form with a checkbox next to each file so they can be selected by the user. The csASPZipFile component is used to obtain the directory listing although the File System Object could be used instead.

In the downloadable example this script is called "SELECTPLUS.ASP".

<form action="finished.asp" method="post">
<%
Set FileObj = Server.CreateObject("csASPZipFile.MakeZip")
FileObj.DirName = Server.MapPath("datafiles") & "\"
If FileObj.FileList.Count > 1 Then
  For I = 0 to FileObj.FileList.Count - 1
    Response.Write "<input type="checkbox" value="true" name=" & I & ">" & _
    FileObj.FileList(I) & "<br>"
  Next
End If
%>
<input type="hidden" name="directory" value="<%= Server.HTMLEncode(FileObj.DirName) %>">
<input type="reset" value="Clear"><input type="submit" value="Download">
</form>

The source directory is called "datafiles" and is immediately below the directory containing the scripts. This is specified in only one place and the value is passed to the next script as a hidden form variable. If a different directory is used it must be a full physical path and it must end with a backslash.

The second script collects the form data and uses Javascript to load the download script. The form data is passed to the third script in the query string. Here are the relevant parts.

In the downloadable example this script is called "FINISHED.ASP".

<%
Set Zip = Server.CreateObject("csASPZipFile.MakeZip")
Zip.DirName = Request.Form("Directory")
URL = "makezipplus.asp?Directory=" & Server.URLEncode(Request.Form("Directory"))
For I = 0 to Zip.FileList.Count - 1
  If Request.Form(CStr(I)) = "true" Then
    URL = URL & "&" & CStr(I) & "=true"
  End If
Next
%>

<script language="JavaScript">
<!-- Begin
  function send()
  {
    document.location = '<%= URL %>';
  }
// End -->
</script>

<body onLoad="send();">

The URL is constructed in ASP using the form variables. Then the Javascript onLoad event is used to call a redirection to the script that generates and streams the zip file. This current script is the one that will remain in the browser window after the download is completed.

The third script takes the query string data which specifies the required files, it creates the zip file and streams it to the browser.

In the downloadable example this script is called "MAKEZIPPLUS.ASP".

<%
Response.Expires = 0
Response.Buffer = true
Response.Clear

Set Zip = Server.CreateObject("csASPZipFile.MakeZip")
Zip.DirName = Request.QueryString("Directory")
For I = 0 to Zip.FileList.Count - 1
  If Request.QueryString(CStr(I)) = "true" Then
    Zip.ZipAdd(Zip.DirName & Zip.FileList(I))
  End If
Next
Zip.StreamZip "sample.zip"
%>

In the scripts the files are listed in the same order so the index in that list is used for identification. There are other ways of doing this such as passing the name of the file. The zip file is built by using the ZipAdd method to add each file. The full physical path of the file on the server is needed.

This example does not preserve the directory structure although the csASPZipFile component can do this. The paths on the server can be used or new paths can be specified that are not related to the location of the files on the server.

Back to the example.

There is a simpler example which does not have the intermediate page. Click here for more.