By timarcher52
via timarcher.com
Published: Apr 13 2007 / 16:10
Have you ever had the requirement to compress or uncompress files or byte streams from within your java applications? This requirement seems to be a common need for me in all sorts of applications from B2B data feeds to my client applications. Within my client applications I allow users to attach files to records and I compress them to conserve space before storing them on the server. Once I compress the byte stream, I usually do one of two things. I will either store the byte stream representing the compressed file directly into a BLOB column in my database, or I will write it to a file in a common file system location.



Comments
bloid replied ago:
Errr... How is this better or different to ZipInputStream and ZipOutputStream?
http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipInputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/ZipOutputStream.html
Or indeed GZIPInputStream and GZIPOutputStream if you fancy using GZip instead:
http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPInputStream.html
http://java.sun.com/j2se/1.5.0/docs/api/java/util/zip/GZIPOutputStream.html
?
timarcher52 replied ago:
The difference is that it allowed me to pass in a byte array, have it deflated/inflated, and then have the byte array returned to the caller. I could not find out how to do this at all using GZipInput/OutputStream.
bloid replied ago:
Something like:
ByteArrayOutputStream baos = new ByteArrayOutputStream() ;
GZIPOutputStream zos = new GZIPOutputStream( baos ) ;
zos.write( bufferToBeCompressed, 0, bufferToBeCompressed.length ) ;
zos.finish() ;
byte[] compressedBytes = baos.toByteArray() ;
Should do it :-) (I *think* -- not tried it...)
CounterSpell replied ago:
I agree. This seems like a reinvention of the wheel to me.
timarcher52 replied ago:
Hmm, while on the surface that looks easy and should work, there was a reason I didnt use the streams approach. I wrote this a few years ago and my memory is hazy, but I seem to remember something related to not being able to read all the bytes in at once from my inputstreams. Especially because I was pulling the byte array from a BLOB in the database using JDBC. I looped on my read until I had the whole byte array in memory (so I new the total number of bytes I was working with), then passed the whole thing through to be deflated. The downfall is the excessive memory usage in holding multiple copies of the byte array in memory.
In any case, thanks for the input! I'm going to go back to my notes on why I did it this way in that system, and put together a few test cases using your suggested streams approach. If I can replace my framework class with a standard Sun package, it'll be all the better!
bloid replied ago:
You *should* be able to write into Blobs using an OutputStream, so if you wrap that in a GZIPOutputStream, you should be able to write a simple function that reads your data in in small chunks and writes it straight to the db (thereby missing out on having the entire BLOB in memory at once) via this GZIP wrapped output stream
Glad I could help with another possible route to solving this problem
Voters For This Link (9)
Voters Against This Link (3)