Skip to content

Instantly share code, notes, and snippets.

@elevenetc
Created October 18, 2015 12:50
Show Gist options
  • Save elevenetc/fa1645a2c4468136bc72 to your computer and use it in GitHub Desktop.
Save elevenetc/fa1645a2c4468136bc72 to your computer and use it in GitHub Desktop.
Compress.java
private void compress(byte[] input) {
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
Deflater compressor = new Deflater();
compressor.setLevel(Deflater.BEST_COMPRESSION);
compressor.setInput(input);
compressor.finish();
byte[] buf = new byte[1024];
while (!compressor.finished()) {
int count = compressor.deflate(buf);
bos.write(buf, 0, count);
}
try {
bos.close();
} catch (IOException e) {
}
// Get the compressed data
byte[] compressedData = bos.toByteArray();
int compressedLength = compressedData.length;
int inputLengthlength = input.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment