Java zip compression (converted from Jeet)

Source: Internet
Author: User
Public   Static String compress (string s) throws ioexception {

Bytearrayinputstream Input
=   New Bytearrayinputstream (S. getbytes ( " UTF-8 " ));

Bytearrayoutputstream output
=   New Bytearrayoutputstream ( 1024 );

Gzipoutputstream gzout
=   New Gzipoutputstream (output );

 

Byte [] Buf = New   Byte [ 1024 ];

Int Number;

 



While (Number = Input. Read (BUF )) ! =   - 1 ) {

Gzout. Write (BUF,
0, Number );

}
 



Gzout. Close ();

Input. Close ();



String result
= New Base64encoder (). encode (output. tobytearray ());



Output. Close ();



Return Result;

}
 



Public   Static String decompress (string data) throws ioexception {

Bytearrayoutputstream output
=   New Bytearrayoutputstream ( 1024 );

Bytearrayinputstream Input
=   New Bytearrayinputstream ( New Base64decoder (). decodebuffer (data ));

Gzipinputstream gzinpt
=   New Gzipinputstream (input );

Byte [] Buf =   New   Byte [ 1024 ];

Int Number =   0 ;



While (Number = Gzinpt. Read (BUF )) ! =   - 1 ) {

Output. Write (BUF,
0, Number );

}
 



Gzinpt. Close ();

Input. Close ();



String result
=   New String (output. tostring ( " UTF-8 " ));



Output. Close ();



Return Result;



}
 

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.