CFLib.org – Common Function Library Project

gzip(text[, format])

Last updated November 14, 2007

author

Oblio Leitch

Version: 1 | Requires: CF7 | Library: StrLib

Description:
This function will take any string and run it through a gzip compression. By default it will return the binary result, or you can specify an encoding of "base64", "hex", "uu".

Return Values:
Returns a string.

Example:

<cffile action="read" file="#expandPath("./readme.txt")#" variable="input" />
<cfset compressed=gzip(input) />
<cfoutput>
original: #len(input)#<br />
compressed: #len(compressed)#<br />
encoded: #len(gzip(compressed,"base64"))#<br />
</cfoutput>

Parameters:

Name Description Required
text String to compress. Yes
format binary,base64,hex, or uu. Defaults to binary. No

Full UDF Source:

<!---
 Compresses a string using the gzip algorithm; returns binary or a string of (base64|hex|uu).
 
 @param text      String to compress. (Required)
 @param format      binary,base64,hex, or uu. Defaults to binary. (Optional)
 @return Returns a string. 
 @author Oblio Leitch (oleitch@locustcreek.com) 
 @version 1, November 14, 2007 
--->
<cffunction name="gzip"
    returntype="any"
    displayname="gzip"
    hint="compresses a string using the gzip algorithm; returns binary or string(base64|hex|uu)"
    output="no">
    <!---
        Acknowledgements:
        Andrew Scott, original gzip compression routine
         - http://www.andyscott.id.au/index.cfm/2006/9/12/Proof-of-Concept
    --->
    <cfscript>
        var result="";
        var text=createObject("java","java.lang.String").init(arguments[1]);
        var dataStream=createObject("java","java.io.ByteArrayOutputStream").init();
        var compressDataStream=createObject("java","java.util.zip.GZIPOutputStream").init(dataStream);
        compressDataStream.write(text.getBytes());
        compressDataStream.finish();
        compressDataStream.close();

        if(arrayLen(arguments) gt 1){
            result=binaryEncode(dataStream.toByteArray(),arguments[2]);
        }else{
            result=dataStream.toByteArray();
        }
        return result;
    </cfscript>
</cffunction>

Search CFLib.org


Latest Additions

Raymond Camden added
QueryDeleteRows
November 04, 2017

Leigh added
nullPad
May 11, 2016

Raymond Camden added
stripHTML
May 10, 2016

Kevin Cotton added
date2ExcelDate
May 05, 2016

Raymond Camden added
CapFirst
April 25, 2016

Created by Raymond Camden / Design by Justin Johnson