Java uses gzip for compression and decompression (Gzipoutputstream,gzipinputstream) __java

Source: Internet
Author: User

To use Gzipoutputstream for gzip compression:

public static byte[] Compress (String str, string encoding) {
        if (str = NULL | | str.length () = 0) {return
            null;< c2/>}
        Bytearrayoutputstream out = new Bytearrayoutputstream ();
        Gzipoutputstream gzip;
        try {
            gzip = new Gzipoutputstream (out);
            Gzip.write (str.getbytes (encoding));
            Gzip.close ();
        } catch (IOException e) {
            apilogger.error ("gzip compress error.", e);
        }
        return Out.tobytearray ();
    }



To use Gzipinputstream for gzip decompression:

public static byte[] uncompress (byte[] bytes) {
        if (bytes = NULL | | bytes.length = 0) {return
            null;
        }
        Bytearrayoutputstream out = new Bytearrayoutputstream ();
        Bytearrayinputstream in = new Bytearrayinputstream (bytes);
        try {
            Gzipinputstream ungzip = new Gzipinputstream (in);
            byte[] buffer = new byte[256];
            int n;
            while ((n = ungzip.read (buffer)) >= 0) {
                out.write (buffer, 0, N);
            }
        catch (IOException e) {
            Apilo Gger.error ("gzip uncompress error.", e);
        }

        return Out.tobytearray ();
    }



Complete code:

Package com.weibo.api.proxy.server.util;
Import Java.io.ByteArrayInputStream;
Import Java.io.ByteArrayOutputStream;
Import java.io.IOException;
Import Java.util.zip.GZIPInputStream;

Import Java.util.zip.GZIPOutputStream;

Import Org.apache.commons.codec.binary.StringUtils;

Import Cn.sina.api.commons.util.ApiLogger;

    /** * * * @author WENQI5 * */public class Gziputils {public static final String gzip_encode_utf_8 = "UTF-8";

    public static final String gzip_encode_iso_8859_1 = "Iso-8859-1"; 
        /** * String compressed to gzip byte array * * @param str * @return/public static byte[] Compress (string str) {
    Return compress (str, gzip_encode_utf_8);  /** * String compressed to gzip byte array * * @param str * @param encoding * @return * * public static
        Byte[] Compress (String str, string encoding) {if (str = NULL | | str.length () = 0) {return null; } Bytearrayoutputstream out = new BytearrayoutputStream ();
        Gzipoutputstream gzip;
            try {gzip = new Gzipoutputstream (out);
            Gzip.write (str.getbytes (encoding));
        Gzip.close ();
        catch (IOException e) {apilogger.error ("gzip compress error.", e);
    return Out.tobytearray (); /** * gzip Uncompressed * * @param bytes * @return/public static byte[] Uncompress (byte[) by
        tes {if (bytes = NULL | | | bytes.length = = 0) {return null;
        } Bytearrayoutputstream out = new Bytearrayoutputstream ();
        Bytearrayinputstream in = new Bytearrayinputstream (bytes);
            try {gzipinputstream ungzip = new Gzipinputstream (in);
            byte[] buffer = new BYTE[256];
            int n;
            while ((n = ungzip.read (buffer)) >= 0) {out.write (buffer, 0, N);
The catch (IOException e) {apilogger.error ("gzip uncompress error.", e);        return Out.tobytearray ();
        }/** * * @param bytes * @return/public static String uncompresstostring (byte[] bytes) {
    Return uncompresstostring (bytes, gzip_encode_utf_8); /** * * @param bytes * @param encoding * @return/public static String Uncompressto
        String (byte[] bytes, string encoding) {if (bytes = = NULL | | bytes.length = 0) {return null;
        } Bytearrayoutputstream out = new Bytearrayoutputstream ();
        Bytearrayinputstream in = new Bytearrayinputstream (bytes);
            try {gzipinputstream ungzip = new Gzipinputstream (in);
            byte[] buffer = new BYTE[256];
            int n;
            while ((n = ungzip.read (buffer)) >= 0) {out.write (buffer, 0, N);
        return out.tostring (encoding); catch (IOException e) {apilogger.error ("gzip uncompress to String ErroR. ", E);
    return null; public static void Main (string[] args) {String str = '%5b%7b%22lastupdatetime%22%3
        a%222011-10-28+9%3a39%3a41%22%2c%22smslist%22%3a%5b%7b%22livestate%22%3a%221 ";
        System.out.println ("Original length:" + str.length ());
        System.out.println ("Compressed string:" + gziputils.compress (str). toString (). Length ());
        System.out.println ("Uncompressed string:" + Stringutils.newstringutf8 (gziputils.uncompress (gziputils.compress (str)));
    System.out.println ("Uncompressed string:" + gziputils.uncompresstostring (gziputils.compress (str));
 }
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.