See Also: Deflater Members
This class compresses data using the DEFLATE algorithm (see ).
It is usually more convenient to use Java.Util.Zip.DeflaterOutputStream.
To compress an in-memory byte[] to another in-memory byte[] manually:
java Example
byte[] originalBytes = ... Deflater deflater = new Deflater(); deflater.setInput(originalBytes); deflater.finish(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buf = new byte[8192]; while (!deflater.finished()) { int byteCount = deflater.deflate(buf); baos.write(buf, 0, byteCount); } deflater.end(); byte[] compressedBytes = baos.toByteArray();
In situations where you don't have all the input in one array (or have so much input that you want to feed it to the deflater in chunks), it's possible to call Deflater.SetInput(Byte[]) repeatedly, but you're much better off using Java.Util.Zip.DeflaterOutputStream to handle all this for you. Java.Util.Zip.DeflaterOutputStream also helps minimize memory requirements — the sample code above is very expensive.
A compression level must be Deflater.DefaultCompression to compromise between speed and compression (currently equivalent to level 6), or between 0 (Deflater.NoCompression, where the input is simply copied) and 9 (Deflater.BestCompression). Level 1 (Deflater.BestSpeed) performs some compression, but with minimal speed overhead.