Skip to content

Instantly share code, notes, and snippets.

@navopw
Created May 5, 2017 22:26
Show Gist options
  • Save navopw/2270700a551cd0b3c5ec36a93d8a1da1 to your computer and use it in GitHub Desktop.
Save navopw/2270700a551cd0b3c5ec36a93d8a1da1 to your computer and use it in GitHub Desktop.
GzipCompression
package pw.navo.compression;
import java.io.*;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class GzipCompression {
public static byte[] compress(byte[] bytes) throws IOException {
ByteArrayOutputStream byteStream = new ByteArrayOutputStream(bytes.length);
try {
GZIPOutputStream gzipInputStream = new GZIPOutputStream(byteStream);
try {
gzipInputStream.write(bytes);
} finally {
gzipInputStream.close();
}
} finally {
byteStream.close();
}
return byteStream.toByteArray();
}
public static byte[] deflate(byte[] bytes) throws IOException {
GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes));
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int length = 0;
while ((length = gzipInputStream.read(buffer)) != -1) {
byteBuffer.write(buffer, 0, length);
}
return byteBuffer.toByteArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment