Java.util.zip.Deflater Compression Inflater Decompression Instance

Source: Internet
Author: User
Tags deflater

Source: Java Compression decompression class instance [go]

 PackageCom.example.helloworld;ImportJava.io.ByteArrayOutputStream;Importjava.io.IOException;ImportJava.util.zip.Deflater;ImportJava.util.zip.Inflater;/*** zlib Compression tool * *@authorpresent *@version1.0 *@since1.0*/ Public Abstract classUtils {/*** Compression * *@paramdata to be compressed *@returnbyte[] Compressed data*/     Public Static byte[] Compress (byte[] data) {        byte[] Output =New byte[0]; Deflater Compresser=NewDeflater ();        Compresser.reset ();        Compresser.setinput (data);        Compresser.finish (); Bytearrayoutputstream Bos=NewBytearrayoutputstream (data.length); Try {            byte[] buf =New byte[1024];  while(!compresser.finished ()) {                inti =compresser.deflate (BUF); Bos.write (BUF,0, i); } Output=Bos.tobytearray (); } Catch(Exception e) {output=data;        E.printstacktrace (); } finally {            Try{bos.close (); } Catch(IOException e) {e.printstacktrace ();        }} compresser.end (); returnoutput; }    /*** Unzip * *@paramdata to be compressed *@returnbyte[] decompressed data*/     Public Static byte[] Decompress (byte[] data) {        byte[] Output =New byte[0]; Inflater Decompresser=NewInflater ();        Decompresser.reset ();        Decompresser.setinput (data); Bytearrayoutputstream o=NewBytearrayoutputstream (data.length); Try {            byte[] buf =New byte[1024];  while(!decompresser.finished ()) {                inti =decompresser.inflate (BUF); O.write (BUF,0, i); } Output=O.tobytearray (); } Catch(Exception e) {output=data;        E.printstacktrace (); } finally {            Try{o.close (); } Catch(IOException e) {e.printstacktrace ();        }} decompresser.end (); returnoutput; }     Public Static voidMain (string[] args) {String inputstr= "[email protected]; [Email protected]; [Email protected] "; System.err.println ("Input string: \ t" +inputstr); byte[] input =inputstr.getbytes (); System.err.println ("Input byte length: \ t" +input.length); byte[] data =utils.compress (input); System.err.println ("Byte length after compression: \ t" +data.length); byte[] Output =utils.decompress (data); System.err.println ("Uncompressed byte length: \ t" +output.length); String Outputstr=NewString (output); System.err.println ("Output string: \ t" +outputstr); }}

Java.util.zip.Deflater Compressed Inflater Decompression instance

Related Article

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.