This Java tip illustrates an example of compressing a Byte Array. Developer may compress a byte array with the help of Deflater class.

  byte[] input = "compression string".getBytes();
    // Compressor with highest level of compression
    Deflater compressor = new Deflater();
    // Give the compressor the data to compress
    // Create an expandable byte array to hold the compressed data.
    // It is not necessary that the compressed data will be smaller than
    // the uncompressed data.
    ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
    // Compress the data
    byte[] buf = new byte[1024];
    while (!compressor.finished()) {
        int count = compressor.deflate(buf);
        bos.write(buf, 0, count);
    try {
    } catch (IOException e) {
    // Get the compressed data
    byte[] compressedData = bos.toByteArray();