user1431633
user1431633

Reputation: 658

read a zip file without saving it first

I have an API call returning binary compressed data in .zip format. I don't want to first save the file to disk and then unzip. I want to unzip on the fly in memory. And then parse the xml content.

Is there a way to do this with cfzip or via java directly? Basically editing this code I found on cflib.

<cffunction name="ungzip"
    returntype="any"
    displayname="ungzip"
    hint="decompresses a binary|(base64|hex|uu) using the gzip algorithm; returns string"
    output="no">

    <cfscript>
        var bufferSize=8192;
        var byteArray = createObject("java","java.lang.reflect.Array")
             .newInstance(createObject("java","java.lang.Byte").TYPE,bufferSize);
        var decompressOutputStream = createObject("java","java.io.ByteArrayOutputStream").init();
        var input=0;
        var decompressInputStream=0;
        var l=0;

        if(not isBinary(arguments[1]) and arrayLen(arguments) is 1) return;

        if(arrayLen(arguments) gt 1){
            input=binaryDecode(arguments[1],arguments[2]);
        }else{
            input=arguments[1];
        }

        decompressInputStream = createObject("java","java.util.zip.GZIPInputStream")
             .init(createObject("java","java.io.ByteArrayInputStream")
             .init(input));

        l=decompressInputStream.read(byteArray,0,bufferSize);

        while (l gt -1){
            decompressOutputStream.write(byteArray,0,l);
            l=decompressInputStream.read(byteArray,0,bufferSize);
        }

        decompressInputStream.close();
        decompressOutputStream.close();

        return decompressOutputStream.toString();
    </cfscript>

</cffunction>

Upvotes: 2

Views: 404

Answers (0)

Related Questions