2

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>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
user1431633
  • 658
  • 2
  • 15
  • 34
  • Take a look at [`ZipInputStream`](http://docs.oracle.com/javase/7/docs/api/java/util/zip/ZipInputStream.html), but it will not contain any entries, it wills simply be a compressed stream... – MadProgrammer Jun 13 '13 at 00:13
  • You can compress data into a stream, but I don't believe you can decompress zip data from a stream without first reading the whole data stream as zip metadata is stored at the end of the file. See the `Structure` section of: http://en.wikipedia.org/wiki/Zip_%28file_format%29 – Shadow Man Jun 13 '13 at 00:24
  • A single blank line of white space is always enough. And don't make 'every other line' blank! – Andrew Thompson Jun 13 '13 at 00:55
  • 2
    Why not save it to the RAM disk first? Then it's not on your spinning disk, and you can just read it directly out of ram:// – Dan Short Jun 13 '13 at 02:23

0 Answers0