Created
May 5, 2017 22:26
-
-
Save navopw/2270700a551cd0b3c5ec36a93d8a1da1 to your computer and use it in GitHub Desktop.
GzipCompression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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