Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 01:04
Show Gist options
  • Save bennadel/9753362 to your computer and use it in GitHub Desktop.
Save bennadel/9753362 to your computer and use it in GitHub Desktop.
Embedding ZIP Files Inside JPG Files Using ColdFusion
<!---
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment