public static byte[] compressData(byte[] data) {
    Deflater deflater = new Deflater();
    deflater.setLevel(Deflater.BEST_COMPRESSION);

    deflater.setInput(data);
    deflater.finish();

    ByteArrayOutputStream stream = new ByteArrayOutputStream(data.length);
    // Compress the data
    byte[] buffer = new byte[1024];
    while (!deflater.finished()) {
        int size = deflater.deflate(buffer);
        stream.write(buffer, 0, size);
    }
    try {
      stream.close();
    } catch (IOException e) {
    }
    // Get the compressed data
    return stream.toByteArray();
  }