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 <cfdirectory> tag is used to list the files.

In the downloadable example this script is called "SELECT.CFM".

<form action="makezip.cfm" method="post">
<cfset dir = ExpandPath(".") & "\datafiles\">
<cfdirectory directory="#dir#" name="datafiles">
<cfset I = 0>
<cfoutput query="datafiles">
<cfif (#name# NEQ ".") and (#name# NEQ "..")>
<input type=checkbox value=true name=#I#>#name#<br>
</cfif>
<cfset I = I + 1>
</cfoutput>
<input type=hidden name=directory value="<cfoutput>#HTMLEditFormat(dir)#</cfoutput>">
<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.CFM".

<cfcache action="flush">
<cfset dir=form["directory"]>
<cfset filename=ExpandPath(".") & "\" & CreateUUID() & ".zip">
<cfobject action="create" name="zip" class="csASPZipFileTrial.MakeZip">
<cfset I=0>
<cfdirectory directory=#dir# name="datafiles">
<cfoutput query="datafiles">
<cfparam name="form[#I#]" default="false">
<cfif form["#I#"] EQ "true">
  <cfset zip.ZipAdd(dir & #name#)>
</cfif>
<cfset I = I + 1>
</cfoutput>
<cfset zip.SaveZip(#filename#)>
<cfcontent type="application/x-zip-compressed" deletefile="yes" file=#filename#>

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.

The file selection is chosen using checkboxes and this can lead to difficulties in Cold Fusion because if the box is unchecked there is no form variable to read and an error is generated. The work around for this is to use the <cfparam> tag to define the form variable.

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.

We have another example which uses Javascript to transfer the user from the form page. Click here for more.

There is an alternative method available in Cold Fusion MX which uses the PageContext Java Object and this can stream images directly to the browser without saving to a temporary file first. The is more information in the Cold Fusion section in the online manual.