Using ColdFusion to unzip a Gzip Base64 string

Here is a quick code snippet to allow us unzip a base64 string of gzipped data:

<cfset data = toBinary("H4sIAKXWCU4A/7MJyEmsTC0qDkotS80rTS1W0LcDAJmtcVQTAAAA") />


<cfset buffReader = createObject("java", "java.io.BufferedReader").init(
	createObject("java", "java.io.InputStreamReader").init(
		createObject("java", "java.util.zip.GZIPInputStream").init(
			createObject("java", "java.io.ByteArrayInputStream").init(data)
		) 
	)
) />

<!--- Read our unzipped data, line by line --->
<cfset str = "" />
<cfset line = buffReader.readLine() />

<cfloop condition="isDefined('line')">

	<cfset str &= line />
	<cfset line = buffReader.readLine() />
	
</cfloop>

<cfdump var="#str#" />

Notice how I nested the calls, otherwise you end up with a bunch of use-once variables knocking around that you are just passing to the next call, like so:

<cfset byteArr = createObject("java", "java.io.ByteArrayInputStream").init(data) />
<cfset gzip = createObject("java", "java.util.zip.GZIPInputStream").init(byteArr) />
<cfset inputStream = createObject("java", "java.io.InputStreamReader").init(gzip) />
<cfset buffReader =  createObject("java", "java.io.BufferedReader").init(inputStream) />

Anyway, others might find this useful. Most of the info I needed I found in this Java example.

Advertisements
This entry was posted in Uncategorized and tagged , , , . Bookmark the permalink.

One Response to Using ColdFusion to unzip a Gzip Base64 string

  1. Al S. says:

    You should SEO this a bit better. I’ve been looking for this code for a few days. Which drove me to start working on my own. However, not being a JAVA developer made me do a bit more research to eventually end here…. Thanks for putting this up!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s