Howto compress or decompress byte array in Java?
Posted by Piotr Gabryanczyk on June 8, 2009
Check this class out:
Compressor.java
import java.io.*; import java.util.zip.*; import org.apache.commons.io.IOUtils; public class Compressor{ public static byte[] compress(byte[] content){ ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try{ GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream); gzipOutputStream.write(content); gzipOutputStream.close(); } catch(IOException e){ throw new RuntimeException(e); } System.out.printf("Compression ratio %f\n", (1.0f * content.length/byteArrayOutputStream.size())); return byteArrayOutputStream.toByteArray(); } public static byte[] decompress(byte[] contentBytes){ ByteArrayOutputStream out = new ByteArrayOutputStream(); try{ IOUtils.copy(new GZIPInputStream(new ByteArrayInputStream(contentBytes)), out); } catch(IOException e){ throw new RuntimeException(e); } return out.toByteArray(); } public static boolean notWorthCompressing(String contentType){ return contentType.contains("jpeg") || contentType.contains("pdf") || contentType.contains("zip") || contentType.contains("mpeg") || contentType.contains("avi"); } }
Dependencies: Apache commons-io.jar
כדור בעננים said
כדור בעננים…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
toko cctv said
toko cctv…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
teen comforters said
teen comforters…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
Free Painless Houston tattoo removal said
Free Painless Houston tattoo removal…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
Moshi Monsters said
Moshi Monsters…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
List of no credit check apartments in LA said
List of no credit check apartments in LA…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
trivia answers said
trivia movies…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
Gebze Motorlu kurye said
Gebze Motorlu kurye…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
cloud computing services,cloud computing hosting,clouding computing,cloud computing service,cloud computing platform,cloud computing server,cloud computing security,cloud computing solution,cloud computing technologies,cloud based solution,cloud services, said
cloud computing services,cloud computing hosting,clouding computing,cloud computing service,cloud computing platform,cloud computing server,cloud computing security,cloud computing solution,cloud computing technologies,cloud based solution,cloud serv…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
List of New York City no credit check apartments said
List of New York City no credit check apartments…
[…]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…
iofjeoifjewof said
thanks…..
chiropratique spécifique Sherbrooke said
Howdy just wanted to give you a brief heads up and let you know a few of
the pictures aren’t loading properly. I’m not sure why but I think its a linking issue.
I’ve tried it in two different web browsers and both show the same results.