Piotr Gabryanczyk’s Blog

Java, Refactoring, AOP, Spring, DDD, TDD, etc.

  • Blogroll

    • I have joined Anti-IF Campaign

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

Advertisements

12 Responses to “Howto compress or decompress byte array in Java?”

  1. כדור בעננים…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  2. toko cctv said

    toko cctv…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  3. teen comforters…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  4. Free Painless Houston tattoo removal…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  5. Moshi Monsters…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  6. List of no credit check apartments in LA…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  7. trivia movies…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  8. Gebze Motorlu kurye…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  9. 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[…]…

  10. List of New York City no credit check apartments…

    […]Howto compress or decompress byte array in Java? « Piotr Gabryanczyk’s Blog[…]…

  11. iofjeoifjewof said

    thanks…..

  12. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: