Compressing data

231 views Asked by At

For some reason I can't find a solid example online on how to do the following, so if anyone here can help that would great

I am transferring files over a socket in Java 6 and I would like to compress the data. I have looked into the ZIP Input Stream and although it seems good, I have had nothing but trouble getting it working. What I would rather do is:

  1. Read the uncompressed (normal) file into a byte[] array
  2. In memory, compress the byte[] into another byte[] that is zip-compressed
  3. transfer that new zipped byte[] over the normal OutputStream (no fancy deflater/inflater classes to deal with)
  4. On the other end, decompress the zip back to its original byte[] and write it to a new file.

I'd appreciate any examples on how to do those 4 steps :)

Here is why I'd rather not use the Zip IO Stream: Problems with using ZipOutputStream and ObjectOutputStream

2

There are 2 answers

1
TheCoder On

You can use ZipInputStream available in Java. Here are the following classes you can use to accomplish what you want

ZipInputStream ZipOutputStream

If you have worked earlier on IO Streams, it is a cake walk. Let me know if you still cannot get it to work.

0
flanglet On

There are many compression packages available online. You can try to look at this one: https://code.google.com/p/kanzi/. Data can be compressed as a stream or a buffer: E.G. create a CompressedOutputStream and write data to it: cos.write(array, 0, len); Different common compression methods can be specified to reach your speed/compression ratio target. There is a full example here: https://code.google.com/p/kanzi/source/browse/java/src/kanzi/test/TestBlockCoder.java