Deflater and inflater compression and decompression

Source: Internet
Author: User
Tags deflater

The original: Deflater and inflater compression and decompression

Package Util;import Java.util.arrays;import;import;import Org.apache.commons.codec.binary.base64;public class Stringzlibutil {/** * compressed with zlib * @param message * @return * @throws EXC Eption */public Static byte[] zlibcompress (String message) throws Exception{string chatacter= "UTF-8"; byte[] input = Message.getbytes (Chatacter);    SYSTEM.OUT.PRINTLN ("Input Length" +input.length);    byte[] Output = new byte[input.length+10+new Double (Math.ceil (input.length*0.25f)). Intvalue ()];    System.out.println (output.length);    Deflater Compresser = new Deflater ();    Compresser.setinput (input);    Compresser.finish ();    int compresseddatalength = compresser.deflate (output);    System.out.println ("Compresseddatalength" +compresseddatalength);        Compresser.end ();    String Ostr=new string (output,0,compresseddatalength,chatacter);    SYSTEM.OUT.PRINTLN ("Out str" +OSTR); return arrays.copyof (output, compresseddatalength);} /** * Unzip * @param Barr need solutionCompressed byte array * @param charater encoding method for compression * @return * @throws Exception */public static byte[] zlibinfcompress (byte[] Barr,str ing Charater) throws exception{byte[] Result=new byte[2014];inflater inf=new inflater (); Inf.setinput (Barr); int infLen= Inf.inflate (result); Inf.end (); String Strogr=new string (result,charater); System.out.println ("str ogr" +strogr); return arrays.copyof (result, Inflen);} public static void Main (string[] args) throws exception{string str= "abcde|qqqqqqqqqqqqqqq|wwwwwwwwwwwwwwwwwwww";// Method one:/*byte[] def=stringzlibutil.zlibcompress (str); byte[] Inf=stringzlibutil.zlibinfcompress (Def, "UTF-8"); String Strogr=new string (INF, "UTF-8"); SYSTEM.OUT.PRINTLN ("str org main" +strogr); *///method Two: The following is the encoding and decoding using the basic base64 byte[] def=stringzlibutil.zlibcompress (str); String strbase=base64.encodebase64string (def); System.out.println ("str base64 string" +strbase); byte[] Decstr=base64.decodebase64 (strbase); byte[] Decode_str= Stringzlibutil.zlibinfcompress (Decstr, "UTF-8"); String Decstrogr=new string (decode_str, "UTF-8"); System.out.println ("Decstrogr" +decstrogr);}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Deflater and inflater compression and decompression

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