Adding "virtual" data to a Zip with ColdFusion

I wrapped up the CFZIP portion of my work on CFWACK last night and I came across a cool feature. Did you know that you can add fake/virtual/you get the idea files to a zip? If you use CFZIP to create a zip you can specify a file or folder. This data has to exist on the file system. However - if you make use of the child cfzipparam tag, you can add a file directly from a variable. Consider the following simple example:

<cfset zipFile = expandPath("./")>

<cfzip file="#zipFile#" action="zip"> <cfzipparam content="#repeatString('Simple Text',999)#" entrypath="simple.txt"> <cfzipparam source="#expandPath('./unzip.cfm')#" entrypath="/sub/unzipfile.cfm"> </cfzip>

This code sample adds two entries to a zip file. The first is a string. I used repeatString with a count of 999 so I could test how well it compressed. The second zipparam tag uses a real file name - but note how I change both the name and folder for the file.

The only thing missing is the ability to store zip data itself into a variable. When making a zip, the file attribute is required. It would be nice if we could specify "name" and have the binary data stored there. Then we could serve up 100% completely virtual zips to users. (Obviously that could be a bit intensive so you would need to use it with care.)

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless 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