CFZip Doesn’t support gz!?

While working on a personal project dealing with ipblock lists, it came time to cfhttp download a compressed (gz) file, figured I could just decrompress it with cfzip, but lo and behold, ACF doesn’t support it.  (Though, I heard ralio might, not sure)

First I came across this: http://www.fusioncoder.com/coldfusion-gzip-weirdness-unzipping-on-fly-with-cfhttp.htm
Which was good (aside from auto-appending .tar), but I really didn’t want to work from files, since cfhttp.fileContent already constains a byteOutputArray, then I came across this stackoverflow post: http://stackoverflow.com/questions/842896/gzipinputstream-decompression-did-not-work-fine-for-the-compressed-data-with-leng

component {
	public any function uncompress(required array byteArray, string charset='utf8') {
		var r = '';
		var buffer = createObject('java','java.io.ByteArrayOutputStream').init();
		var input = createObject('java','java.io.ByteArrayInputStream').init(arguments.byteArray);

		var inflater = createObject('java','java.util.zip.GZIPInputStream').init(input);
		var bbuf = repeatString(" ",256).getBytes();

		while(true) {
			r = inflater.read(bbuf);
			if(r < 0) {
				break;
			}
			buffer.write(bbuf, 0, r);
		}
		return buffer;
	}

	public array function compress(required string input, string charset='utf8') {
		var buffer = createObject('java','java.io.ByteArrayOutputStream').init();
		var deflater = createObject('java','java.util.zip.GZIPOutputStream').init(buffer);
		deflater.write(arguments.input.getBytes(arguments.charset));
		deflater.close();
		return buffer.toByteArray();
	}
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

WordPress Appliance - Powered by TurnKey Linux