Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I convert an uncompressed InputStream into a gzip'ed InputStream efficiently?

A user uploads a large file to my website and I want to gzip the file and store it in a blob. So I have an uncompressed InputStream and the blob wants an InputStream. I know how to compress an InputStream to an Outputstream using GZIPOutputStream, but how do I go from the gzip'ed OutputStream back to the InputStream needed by the blob.

The only way I could find involves using ByteArrayOutputStream and then creating a new InputStream using toByteArray. But that will mean I have an entire copy of the file in memory. And it wouldn't surprise me if the JDBC driver implementation converted the stream to a byte[] also so I'd have two copies in memory.

like image 916
Brian Deterling Avatar asked Feb 02 '10 18:02

Brian Deterling


1 Answers

If you are on java 1.6 you can use java.util.zip.DeflaterInputStream. As far as I can tell, this does exactly what you want. If you can't use 1.6 you should be able to reimplement DeflaterInputStream using java.util.zip.Deflater. When reading the data back from the BLOB use a InflaterInputStream as a filter to get the original data back.

like image 107
Geoff Reedy Avatar answered Oct 23 '22 22:10

Geoff Reedy