ColdFusion Sample - Using CFZIP

This post is more than 2 years old.

It's been a while since I did a "ColdFusion Sample", so if you've forgotten, the idea of this is blog series to demonstrate a ColdFusion tag or feature in a complete, if simple, application. In this entry, I'm going to demonstrate CFZIP. This tag allows for creating, reading, and extracting zip files. In my example I'll be making use of reading and extracting.

I've built a small web application that allows you to upload image.

When you upload a file, the logic is pretty simple. Check to see if it's an image first. If it is, we copy the image to the photos directory, then read in the image and scale it for a thumbnail directory. Checking a file to see if it a valid image as easy as using isImageFile. Resizing is also pretty easy with imageScaleToFit. You can also resize with imageResize, but imageScaleToFit allows you to scale images and keep their proportions.

So that portion isn't too complex. But let's ramp it up a notch. What if we allowed folks to upload both images and zips of images. We need to modify our code to check zips for images and extract them as well. Here's the entire template.

<!--- images and thumbs dir are relative ---> <cfset imageDir = expandPath("./photos") & "/"> <cfset thumbDir = expandPath("./thumbs") & "/">

<!--- used to flag if we uploaded crap ---> <cfset successFlag = false>

<cfif structKeyExists(form, "upload") and len(form.upload)> <cfset tempDir = getTempDirectory()> <cffile action="upload" filefield="upload" destination="#tempDir#" nameconflict="overwrite">

&lt;cfset theFile = file.serverdirectory & "/" & file.serverfile&gt;

&lt;cfset images = []&gt;

&lt;cfif file.filewassaved&gt;
	&lt;cfif isImageFile(theFile)&gt;
		&lt;cfset arrayAppend(images,theFile)&gt;

	&lt;!--- check for zip ---&gt;
	&lt;cfif file.serverfileext is "zip"&gt;
			&lt;cfzip action="list" filter="*.jpg,*.png,*.gif" file="#theFile#" name="files"&gt;
			&lt;cfloop query="files"&gt;
				&lt;cfzip action="unzip" entryPath="#name#" destination="#tempDir#" file="#theFile#" overwrite="true"&gt;
				&lt;cfif isImageFile(tempdir & "/" & name)&gt;
					&lt;cfset arrayAppend(images, tempdir & "/" & name)&gt;
				&lt;cfdump var="#cfcatch#"&gt;

&lt;cfif arrayLen(images)&gt;
	&lt;cfloop index="theFile" array="#images#"&gt;
		&lt;!--- create a UUID based name. Helps ensure we don't conflict ---&gt;
		&lt;cfset newName = createUUID() & "." & listLast(theFile, ".")&gt;
		&lt;!--- copy original to image dir ---&gt;
		&lt;cfset fileCopy(theFile, imageDir & newName)&gt;
		&lt;!--- now make a thumb version ---&gt;
		&lt;cfset imgOb = imageRead(theFile)&gt;
		&lt;cfset imageScaleToFit(imgOb, 200,200)&gt;
		&lt;cfset imageWrite(imgOb, thumbDir & newName)&gt;
	&lt;cfset successFlag = true&gt;


<cfset thumbs = directoryList(thumbDir,true,"name",".jpg|.png|*.gif" )>

<!DOCTYPE html> <html> <head> <title>Zip Demo</title> <meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />

&lt;link rel="stylesheet" href=""&gt;
&lt;link rel="stylesheet" href="jquery.lightbox-0.5.css" type="text/css" /&gt;
&lt;script type="text/javascript" src=""&gt;&lt;/script&gt;
&lt;script type="text/javascript" src="jquery.lightbox-0.5.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
	$(function() {

</head> <body>

&lt;div class="container"&gt;

	&lt;ul class="media-grid"&gt;
		&lt;cfloop index="image" array="#thumbs#"&gt;
			&lt;a href="photos/#getFileFromPath(image)#" class="imageList"&gt;
			&lt;img class="thumbnail" src="thumbs/#getFileFromPath(image)#"&gt;

	&lt;h2&gt;Upload New Image&lt;/h2&gt;
	&lt;form enctype="multipart/form-data" method="post"&gt;
	&lt;cfif successFlag&gt;
		Image(s) have been uploaded. Thanks!
	&lt;cfif structKeyExists(variables, "errors")&gt;
	Select image, or zip file of images: 
	&lt;input type="file" name="upload"&gt;
	&lt;input type="submit" value="Upload" class="btn primary"&gt;


</body> </html>

Consider the template as two halves. The bottom half simply handles outputting the images and providing a form. This is just regular HTML although I made use of a jQuery LightBox plugin to make it sexier.

Let's focus more on the top portion. First, we wrap our main handling code in a check for an actual file upload. If the form was submitted and nothing was uploaded, we could provide an error. (In fact, you can see where I made use of an error display in the bottom half, but I ended up not bothering creating any errors.) I send the upload to a temp directory outside of web root. Hopefully we all know why.

I've created an array, images, that will store all the files I'll be copying and creating thumbs with. My code then branches into two sections. If the file was an image, I just add it to the array. If the file was a zip, and note we check the extension, there is no "isZipFile" in ColdFusion, I use the list operation of cfzip to get all the images contained within it. For each one, I extract it, check it again to ensure it really is an image, and then add it to array.

At this point I've got an array of images in a temporary directory. I can then simply loop over it and perform my copy/scale operations. Note the use of createUUID(). This provides a new name for the image and allows me to not worry about overwriting an existing image.

That's it. I'm not going to post a demo for this as I know some jerk will abuse the upload. I did include a zip of the code base though. You should be able to extract this locally under your web root and just play with it.

Download attached file.

Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA

Archived Comments

Comment 1 by Shawn Bowman posted on 11/29/2011 at 12:29 AM

Nice. The one thing I think I haven't really nailed down is uploading of images. I made it work for the most part using <cffileupload> but I know it is far from perfect.

I was tasked with creating a photo gallery of sorts and what I did was use <cffileupload> to allow the administrator to upload all the photos needed via the flash uploader which I know isn't ideal, but it worked.

I have been trying to figure out a very user-friendly way of allowing 10-20 images to be uploaded at once and also allowing the ability to put captions in for each photo. I broke down and just let them upload all the images first then come back and show a page with the images and allow them to put captions after the upload.

I think Photo Galleries are one thing that programmers always tend to have an issue with or at least from the standpoint of a very user-friendly one on the backend. CF made it easier to an extent with image resizing and the cffileupload.

Comment 2 by Shawn Bowman posted on 11/29/2011 at 12:30 AM

Not to mention the picture names were uploaded to a DB, then the captions were placed into the DB after the upload.

Comment 3 by Raymond Camden posted on 11/29/2011 at 12:32 AM

I think your approach (upload first, then edit captions) makes sense. You don't want too much going on at once probably anyway.

Comment 4 by Henry posted on 11/29/2011 at 6:32 PM

I've been playing around with this a bit and noticing on page refresh the files are re-written. I guess they are staying in memory. If my suspicion is true, what do you suggest to remove from memory?

Comment 5 by Raymond Camden posted on 11/29/2011 at 6:59 PM

Are you reloading the page after you did a form submit? If so, it's resubmitting, which is normal behavior. You can prevent that if you did a cflocation after the form uploaded.

Comment 6 by Henry posted on 11/29/2011 at 7:17 PM

Yep, good to know. Thanks.

Comment 7 by Sanoop posted on 12/22/2011 at 1:45 PM

Great Work Ray as usual.

Why Coldfusion does not alert developers in development about the deprecated tag or attributes? :(

Comment 8 by Raymond Camden posted on 12/22/2011 at 6:33 PM

You mean like via a console message? I don't know - we just don't do that.

Comment 9 by Sanoop posted on 12/23/2011 at 9:46 AM

Yes, some message (as Java does) when CF compiles the CFML into .CLASS, I believe it can make the developer to use or interact more with the new functions, tags or attributes.


Comment 10 by zonPluto posted on 3/15/2013 at 6:57 PM

<cfset thumbs = directoryList(thumbDir,true,"name","*.jpg|*.png|*.gif" )>
hi,,,when i run this application.i got an error variable directorylist is undefined.....pls help me...

Comment 11 by Raymond Camden posted on 3/15/2013 at 8:25 PM

directoryList was added to ColdFusion 9. It is a function version of <cfdirectory action="list">. You can replace it with that.