Using ColdFusion to Zip Individual Files

I ran into an interesting situation this morning that didn't have an obvious answer. I needed to create a zip with a few specific files out of a directory with lots of possible files. The problem is that <cfzip /> is designed to work against whole directories, so at first I kept thinking that I'd have to make a temporary directory, move the files to it, and then zip the directory, but a little more persistence found the right answer.

The problem is that the <cfzip /> tag requires a source directory attribute, even if you want a single file, so you have to break the file path up and pass in the directory, and then pass the file name into the filter attribute, so that it only matches that specific file.

<cfzip action="zip" file="#ExpandPath(".")#/" source="#ExpandPath(".")#" filter="test.cfc" />

Handling multiple files in the same folder is easy too, because you can pass in a comma-delimited list to the filter attribute.

<cfzip action="zip" file="#ExpandPath(".")#/" source="#ExpandPath(".")#" filter="test.cfc,test2.cfc,test3.cfc" />

If you have multiple files from different directories though, you have to go over each of them and break up the path yourself. If that sounds a little tedious, I went ahead and made a Utility Function that wraps up the implimentation nicely.

<cffunction name="ZipFile" output="false" returntype="void">
    <cfargument name="zippath" type="string" required="true" />
    <cfargument name="filePath" type="string" required="true" />
    <cfargument name="overwrite" type="boolean" required="false" default="false" />
    <cfzip action="zip" file="#arguments.zipPath#" source="#GetDirectoryFromPath(arguments.filePath)#" filter="#GetFileFromPath(arguments.filePath)#" overwrite="#YesNoFormat(arguments.overwrite)#" />
    <cfreturn />

And it can be used like this:

<cfset zip = "#ExpandPath(".")#\" />

<cfset ZipFile(zip, "#ExpandPath(".")#\utilities.cfc") />
<cfset ZipFile(zip, "#ExpandPath("./new")#\utilities2.cfc") />

Something to remember when working with zips is that every zip command updates the zip if it exists, rather than over writing it, so the example code creates a single zip with 2 files in it.



Jon Hartmann, July 2011

I'm Jon Hartmann and I'm a Javascript fanatic, UX/UI evangelist and former ColdFusion master. I blog about mysterious error messages, user interface design questions, and all things baffling and irksome about programming for the web.

Learn more about me.

Post a job. Find one.

Interested in becoming a sponsor? Contact me.