Embedding ZIP Files Inside JPG Files Using ColdFusion

Posted January 16, 2009 at 4:42 PM by Ben Nadel

Tags: ColdFusion

A while back, I saw a interesting trick where someone used the MS DOS command, "copy," to hide MP3 files inside of images that they were uploading to Flickr. I thought that idea might be something fun to play around with in ColdFusion. After reading the documentation for the MS DOS copy command, it turns out that the trick they were using was simply appending one binary data to another. In ColdFusion and Java, that's pretty easy to do... but, could that possibly be all that it takes?

To test this from end to end, I am gonna do the following:

  1. ZIP several files together.
  2. Embed that ZIP file inside of a JPG image file.
  3. Read image file.
  4. Output image file to screen with new format.
  5. Read ZIP entries out of image and output query.
  6. Extract one of the ZIP entries and output.

I figure this way, we can see if the ZIP file can, in fact, be embedded in an image. And, if so, can we still use the image file successfully as both an image and a ZIP archive.

Here's the test:

  • <!---
  • Store the name of the image file into which we want
  • to embed the zipped data.
  • --->
  • <cfset strImageFile = "girl.jpg" />
  •  
  • <!---
  • This is the name of our new graphic with the embedded
  • zip file.
  • --->
  • <cfset strNewImageFile = "special_girl.jpg" />
  •  
  •  
  • <!---
  • Create the array of files we want to zip up and embed
  • with in the image file.
  • --->
  • <cfset arrFileNames = [
  • "DataFile_1.txt",
  • "DataFile_2.txt",
  • "DataFile_3.txt",
  • "Song.mp3"
  • ] />
  •  
  •  
  • <!---
  • Create our ZIP file based using all of the files above.
  • This is the zip file that will be embedded in the image.
  • --->
  • <cfzip
  • action="zip"
  • file="#ExpandPath( './data.zip' )#"
  • overwrite="true">
  •  
  • <!--- Loop over files to add zip entries. --->
  • <cfloop
  • index="strFileName"
  • array="#arrFileNames#">
  •  
  • <cfzipparam
  • source="#ExpandPath( './#strFileName#' )#"
  • />
  •  
  • </cfloop>
  •  
  • </cfzip>
  •  
  •  
  • <!---
  • Create a binary output stream. This will be used to
  • copy the zip file to the image file.
  • --->
  • <cfset objOutputStream = CreateObject(
  • "java",
  • "java.io.ByteArrayOutputStream"
  • ).Init()
  • />
  •  
  •  
  • <!---
  • Loop over files that we want to join. The first is the
  • image and the second is our zip data file.
  • --->
  • <cfloop
  • index="strFileName"
  • list="#strImageFile#|data.zip"
  • delimiters="|">
  •  
  • <!--- Read in the file binary data (byte array). --->
  • <cffile
  • action="readbinary"
  • file="#ExpandPath( './#strFileName#' )#"
  • variable="binFileData"
  • />
  •  
  • <!--- Append the file to the binary output buffer. --->
  • <cfset objOutputStream.Write(
  • binFileData,
  • JavaCast( "int", 0 ),
  • JavaCast( "int", ArrayLen( binFileData ) )
  • ) />
  •  
  • </cfloop>
  •  
  •  
  • <!--- Write the byte array stream to disk. --->
  • <cffile
  • action="write"
  • file="#ExpandPath( strNewImageFile )#"
  • output="#objOutputStream.ToByteArray()#"
  • />
  •  
  •  
  • <!---
  • Now, read the image file into ColdFusion and write it
  • to the browser. This will test to see if it can be read
  • and manipulated properly as an image.
  • --->
  • <p>
  • <cfimage
  • action="writetobrowser"
  • source="#strNewImageFile#"
  • format="png"
  • />
  • </p>
  •  
  •  
  • <!---
  • Now, read the contents of the "zip" data out of the new
  • image file. This will test to see if the image file can
  • be read and properly manipulated as a ZIP file.
  • --->
  • <cfzip
  • action="list"
  • file="#ExpandPath( strNewImageFile )#"
  • name="qEntry"
  • />
  •  
  • <!--- Trim the query for output. --->
  • <cfquery name="qEntry" dbtype="query">
  • SELECT
  • name,
  • size,
  • type
  • FROM
  • qEntry
  • </cfquery>
  •  
  • <!--- Output the ColdFusion query. --->
  • <cfdump
  • var="#qEntry#"
  • label="#strNewImageFile# w/ Data Zip"
  • />
  •  
  •  
  • <!--- Read the text data for a given entry. --->
  • <cfzip
  • action="unzip"
  • file="#ExpandPath( strNewImageFile )#"
  • destination="#ExpandPath( './output' )#"
  • entrypath="DataFile_1.txt"
  • />
  •  
  • <!--- Include the text file. --->
  • <p>
  • <cfinclude template="./output/DataFile_1.txt" />
  • </p>

As you can see, I am following the exact outline as above. And, when we run the above code, we get the following screen output:

 
 
 
 
 
 
Embedding ZIP Data Files Inside Of JPG Images. 
 
 
 

Holy cow! I can't believe that actually works - and, that it is done so easily. ColdFusion successfully uses the new merged file as a JPG (converting it to a PNG for output) using CFImage; and, ColdFusion's CFZIP tag is able to read all the data entries and extract a given one to disk.




Reader Comments

Jan 16, 2009 at 4:55 PM // reply »
73 Comments

It's either really cold or...


Jan 16, 2009 at 4:56 PM // reply »
10,640 Comments

... or this ColdFusion post is just really really exciting :)


Jan 17, 2009 at 5:40 PM // reply »
15 Comments

Very clever. :-) Can this be put to a real world use? For example, when I try to email zip files or other non-Office files to certain organisations/clients it's rejected by their filewall, so sending an innocent looking file could be slipped through. (Wouldn't that be like a tojan, something scanners are supposed to detect?)

Even if it got through how would the person at the other end unmerge the files from the email attachment keeping in mind that even if an "unmerge" web app was available on the Internet the resulting downloads would be blocked too. Hmm. AIR can't run CFML on a desktop yet can it?


Jan 17, 2009 at 8:55 PM // reply »
110 Comments

Well, I guess if the local computer was running CF, then it could call localhost, but it can't run a self-contained instance of CF.


Jan 18, 2009 at 4:21 PM // reply »
10,640 Comments

@Gary,

You can actually just rename the file and it works. So, if you saved the special_girl.jpg and renamed it to be "special_girl.zip" and double-clicked it - it would open up as a ZIP file. No need to do anything programmatically.


Jan 19, 2009 at 10:07 PM // reply »
15 Comments

"The compressed folder is invalid or corrupt". :-( WinRar doesn't like it either.


Jan 19, 2009 at 10:20 PM // reply »
10,640 Comments

@Gary,

Did you rename it as .ZIP or a .RAR file? I'll put together a demo video tomorrow, just to demonstrate it, to make sure we're all on the same page.


Jan 20, 2009 at 5:51 AM // reply »
15 Comments

Renamed to .zip. A demo video? Cool, I like them. :-)


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
InVision App - Prototyping Made Beautiful With Prototyping Tools Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Feb 12, 2012 at 3:37 AM
Learning ColdFusion 8: CFImage Part III - Watermarks And Transparency
Hi Ben, Just to ask currently it is placed bottom right corner, if i need to replace the same rendered image on the bottom left side or in the bottom center, how that can be calculated. bottom ce ... read »
Feb 11, 2012 at 9:29 PM
Use jQuery's SlideDown() With Fixed-Width Elements To Prevent Jumping
I can't say how glad I am that I found your post. Thank you very much. ... read »
Feb 10, 2012 at 7:21 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
Update! Instead of $(eval(options.insertAfter)).after(data['insertData']); I now use: var ajaxNode = document.createElement('span'); var parent = $(eval(options.insertAfter))[0].parentNode; ... read »
Feb 10, 2012 at 6:18 PM
jQuery AJAX Strips Script Tags And Inserts Them After Parent-Most Elements
encountered this same, what I consider, jQuery bug last week. I'm building a site in which I load some content via AJAX. This content contains Linkedin share button placeholders which Linkedin API ne ... read »
Feb 10, 2012 at 11:30 AM
Cross-Origin Resource Sharing (CORS) AJAX Requests Between jQuery And Node.js
After you understand the concepts here, this is an awesome cheatsheet for enabling CORS in just about anything http://enable-cors.org/ ... read »
JM
Feb 10, 2012 at 9:10 AM
My Safari Browser SQLite Database Hello World Example
@Amy, Here is a very good tutorial on how to use JOIN: http://www.sqltutorial.org/sqljoin-innerjoin.aspx ... read »
Feb 10, 2012 at 4:42 AM
Building A Twitter-Inspired RESTful API Architecture In ColdFusion
This is great, very useful Ben. I spotted a small typo in the api.cgm listing: <cfthrow type="Unauthroized" /> Cheers Stefan ... read »
Feb 9, 2012 at 10:35 PM
CFDirectory Filtering Uses Pipe Character For Multiple Filters (Thanks Steve Withington)
I was wondering if there would be a filter you could apply so that you got everything but what you included in the filter. As in show me all docs that are not a .pdf. ... read »