Description of Simple Zip Demo

There are two 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 "SELECT.ASP".

<form action="makezip.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 takes the form 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 "MAKEZIP.ASP".

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

In both 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 another example which uses Javascript to transfer the user from the form page. Click here for more.