The original: Deflater and inflater compression and decompression
Package Util;import Java.util.arrays;import Java.util.zip.deflater;import java.util.zip.inflater;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