I have to download image files off of a another server and zip them up. I have a url for each image and right now I'm using cfhttp to get the image. Any idea how to zip them up without writing them out to a tmp directory?
When I read this question, I got all giddy 'cause I thought I was gonna be able rock some spicy CFImage - CFZip cohabitation. I did go down that road, but in the long run, it didn't work. Originally, I had this plan to use ColdFusion 8's new CFImage tag to grab the images from the given URLs. Then, all I would have to do is grab the image Blob object (ImageGetBlob()) and zip that into the archive.
This had sweet benefits. For starters, it's much less code than a CFHttp call - you put in the URL as the CFImage source attribute and it just downloads and creates the ColdFusion 8 image object. There was also implicit type validation; we were gonna have to use CFTry / CFCatch no matter what, since we are dealing with an external resource, but CFImage had the implicit image type validation since you can't just load any old file into a ColdFusion image object. It would have also put us in a great position if we wanted to do any type of image conversion.
So, with all the goodness, what went wrong with the CFZip / CFImage approach? There were simply too many variables when it comes to dealing with URL-based images. Most importantly, though, we lose file-type information since we cannot see the response header. Sure, all is easy when you are calling .JPG or .GIF files directly, but what happens when you call a ColdFusion page (.CFM) that is serving up image content? The CFImage tag knows how to read that in, but what type of image is it from our perspective? We didn't see the response header so we don't know. This isn't a problem when we are writing the images to the file system, but this causes a huge problem when we need to utilize the Blob data (which is file-type dependent).
It would have been sweet-ass-sweet, but when it comes to in-memory blob usage and zip archiving, CFImage just didn't quite get us there. The next solution is to rely on ColdFusion's standard CFHttp call. Using ColdFusion's CFHttp tag, we can still grab the image files as binary objects, which complements ColdFusion 8's new CFZipParam tag quite nicely, and it still gives us the ability to look the response header and more easily determine the file type.
When it comes to zipping the image data, I am using a Hash() of the URL so that we lower the chances of us overwriting other images we are downloading at the same time. If you KNOW THE FILE NAMES ahead of time and you know how the foreign server is serving up files, then you don't need this; additionally, you could use ColdFusion 8's CFImage tag as well. I tried to build the more flexible solution, but if you know the file naming for fact, then you could have written something a bit shorter and more elegant.
As far as the zip file is concerned, you do have to have the zip file written to the file system. There is no getting around that using ColdFusion 8's CFZip tag as it does not allow variable-based file creation. This is not a bad thing since Zip files might become very large and become a drain on the system RAM. If it is absolutely necessary to do all in-memory stuff, you might want to check out my ZipUtility.cfc which can handle in-memory zipping.
That being said, here is what I came up with:
Launch code in new window » Download code as text file »
Check out the Online Demo if you want to see this in action. Hope that helps a bit.
Download Code Snippet ZIP File
Comments (2) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Understanding Value Binding Within CFThread
Exercise List: Rethinking The Domain Model / Object Oriented Approach
Awesome dude, I was able to make it work but like you said I had to write the zip out to a file. The issue I was having was I was neglecting to use the getasbinary attribute!
Posted by Paul Roe on Nov 7, 2007 at 3:37 PM
@Paul,
If you use the Java ZIP libraries, you can write the zip file directly to a byte array output stream, but then you lose the convenience of the CFZip tag.
Posted by Ben Nadel on Nov 7, 2007 at 4:06 PM