cfzip

Description

Manipulates ZIP and Java Archive (JAR) files. In addition to the basic zip and unzip functions, use the cfzip tag to delete entries from an archive, filter files, read files in binary format, list the contents of an archive, and specify an entry path used in an executable JAR file.

Categories

History

ColdFusion 8: Added this tag.

Syntax

delete 
<cfzip 
    required 
    action = "delete" 
    file = "absolute pathname" 
    optional 
    entrypath = "full pathname" 
    filter = "file filter" 
    recurse = "yes|no"> 
 
list 
<cfzip 
    required 
    action = "list" 
    file = "absolute pathname" 
    name = "recordset name" 
    optional 
    filter = "file filter" 
    recurse = "yes|no" 
    showDirectory= "yes|no"> 
 
read 
<cfzip 
    required 
    action = "read" 
    entrypath = "full pathname" 
    file = "absolute pathname" 
    variable = "variable name" 
    optional 
    charset = "encoding type"> 
 
readBinary 
<cfzip 
    required 
    action = "readBinary" 
    entrypath = "full pathname" 
    file = "absolute pathname" 
    variable = "variable name"> 
 
unzip 
<cfzip 
    required 
    action = "unzip" 
    destination = "destination directory" 
    file = "absolute pathname" 
    optional 
    entrypath = "full pathname" 
    filter = "file filter" 
    overwrite = "yes|no" 
    recurse = "yes|no" 
    storePath = "yes|no"> 
 
zip 
<cfzip 
    required 
    file = "absolute pathname" 
    One of the following: 
    source = "source directory" 
    <cfzipparam source = "source directory" ...> 
    optional 
    action = "zip" 
    filter = "file filter" 
    overwrite = "yes|no" 
    prefix = "string" 
    recurse = "yes|no" 
    storePath = "yes|no">
Note: You can specify this tag’s attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag’s attribute names as structure keys.

Attributes

AttributeDescriptionRequiredDefault
actionN/A
charsetread
destinationunzip
entryPathdelete read readBinary unzip
filedelete list read readBinary unzip zip
filterdelete list unzip zip
namelist
overwriteunzip zip
prefixzip
recursedelete list unzip zip
showDirectorylist
sourcezip
storePathunzip zip
variableread readBinary

Usage

Use the cfzip tag to zip and unzip files and manipulate existing ZIP or JAR files in ColdFusion. You can use the cfzip tag independently or with one or more cfzipparam tags to manipulate multiple files or directories. The cfzip tag is the parent tag of the cfzipparam tag.
The ZIP format is the standard format for file archiving and compression. The JAR format is based on the ZIP format. JAR files are platform-independent.
Note: The cfzip tag does not create directories. If you specify a directory that does not exist, ColdFusion generates an error.
Use the following syntax to specify an in-memory file or directory in any attribute that takes a path. In-memory files are not written to disk and speed processing of transient data.
ram:///filepath The filepath can include multiple directories, for example ram:///petStore/images/dogImages.zip. You must create the directories in the path before you specify the file. For more information on using in-memory files, see Optimizing transient files in the Developing ColdFusion Applications.

Example

The following example shows how to zip image files chosen from a form and e-mail the ZIP file to the person requesting the images. The first ColdFusion page populates a pop-up menu with the names of artists generated from a database query:
<!--- Create a query to extract artist names from the cfartgallery database. ---> 
<cfquery name="artist" datasource="cfartgallery"> 
    SELECT FIRSTNAME || ' ' || LASTNAME AS FULLNAME,ARTISTS.ARTISTID 
    FROM ARTISTS 
</cfquery> 
 
<!--- Create a form that lists the artists generated by the query. ---> 
<cfform action="zipArt_action.cfm" method="post"> 
<h3>Choose an Artist</h3> 
Please choose an artist:</p> 
<cfselect name="artistName" query="artist" display="FULLNAME" value="ARTISTID" required="yes" multiple="no" size="8"> 
</cfselect> 
<br/><cfinput type="submit" name="submit" value="OK"> 
</cfform>
The first action page displays the images by the selected artist, zips the files, and writes the ZIP file to a temporary directory. Also, it includes a form to e-mail the ZIP file:
<!--- Create a query to extract artwork for the selected artist from the cfartgallery database. ---> 
<cfquery name="artwork" datasource="cfartgallery"> 
    SELECT FIRSTNAME, LASTNAME, LARGEIMAGE 
    FROM ARTISTS, ART 
    WHERE ARTISTS.ARTISTID = ART.ARTISTID 
    AND ARTISTS.ARTISTID=<cfqueryparam value="#form.artistName#"> 
    ORDER BY ARTNAME 
</cfquery> 
 
<cfoutput> 
You have chosen the work of #artwork.FirstName# #artwork.LastName#.</p> 
<cfset thisDir = ExpandPath(".")> 
<cfset imgDir = ExpandPath("..")> 
</cfoutput> 
<cfset xctr = 1> 
<table border="0" cellpadding="15" cellspacing="0" bgcolor="#FFFFFF"> 
<cfoutput query="artwork"> 
    <cfif xctr mod 3 eq 1> 
    <tr> 
        </cfif> 
        <!--- Use the IsImageFile function to verify that the image files  
            extracted from the database are valid. Use the ImageNew function to  
                create a ColdFusion image from valid image files. ---> 
        <cfif IsImageFile("#imgdir#/cfdocs/images/artgallery/ 
                #artwork.largeImage#")> 
        <cfset myImage=ImageNew("#imgdir#/cfdocs/images/artgallery/ 
                #artwork.largeImage#")> 
            <td valign="top" align="center" width="200"> 
            <cfset xctr = xctr + 1> 
        <img src="#imgdir#/cfdocs/images/artgallery/#artwork.largeImage#"/> 
            </td> 
                 
<!---Zip the files by the specified artist. ---> 
        <cfzip source="#imgDir#/cfdocs/images/artgallery/#artwork.LARGEIMAGE#"  
            action="zip" file="#thisDir#/#artwork.lastname#.zip"> 
            </cfif> 
                </cfoutput> 
                </tr> 
                </table> 
<h3>Mail the ZIP File</h3> 
Please enter your e-mail address so we can send you the ZIP file as an attachment.</p> 
<cfform action = "zipArt_action2.cfm" method="post"> 
Your e-mail address: <cfinput type = "Text" name = "MailTo"> 
<!--- Specify the required field. ---> 
    <cfinput type = "hidden" name = "MailTo_required" value = "You must enter  
            your email address"> 
    <cfinput type="hidden" name="zipPath" 
            value="#thisDir#/#artwork.lastname#.zip"> 
    <cfinput type = "Submit" name = "OK" label="Mail"> 
</cfform>
The second action page mails the ZIP file as an attachment:
<h3>Mail the ZIP file</h3> 
Your file has been mailed to you.</p> 
<cfset eMail="#form.MailTo#"> 
<cfset zipPath="#form.zipPath#"> 
<cfmail from="coldfusion@adobe.com" to="#eMail#" subject="see zipped attachment"> 
    The images you requested are enclosed in a ZIP file. 
            <cfmailparam file="#zipPath#"> 
</cfmail>