Java compression and decompression string, byte array, string

Source: Internet
Author: User

Compression extracted tool class found on the Web, can compress string string

/*** * Compress gzip * *@paramData *@return  */  Public Static byte[] GZip (byte[] data) {  byte[] B =NULL; Try{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Gzipoutputstream gzip=NewGzipoutputstream (BOS);   Gzip.write (data);   Gzip.finish ();   Gzip.close (); b=Bos.tobytearray ();  Bos.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;}/*** * Unzip gzip * *@paramData *@return  */  Public Static byte[] Ungzip (byte[] data) {  byte[] B =NULL; Try{Bytearrayinputstream bis=Newbytearrayinputstream (data); Gzipinputstream gzip=NewGzipinputstream (bis); byte[] buf =New byte[1024]; intnum =-1; Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();  while(num = gzip.read (buf, 0, buf.length))! =-1) {baos.write (buf,0, num); } b=Baos.tobytearray ();   Baos.flush ();   Baos.close ();   Gzip.close ();  Bis.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;}/*** * Compressed zip * *@paramData *@return  */  Public Static byte[] Zip (byte[] data) {  byte[] B =NULL; Try{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Zipoutputstream Zip=NewZipoutputstream (BOS); ZipEntry entry=NewZipEntry ("Zip");   Entry.setsize (data.length);   Zip.putnextentry (entry);   Zip.write (data);   Zip.closeentry ();   Zip.close (); b=Bos.tobytearray ();  Bos.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;}/*** * Unzip zip * *@paramData *@return  */  Public Static byte[] UnZip (byte[] data) {  byte[] B =NULL; Try{Bytearrayinputstream bis=Newbytearrayinputstream (data); Zipinputstream Zip=NewZipinputstream (bis);  while(Zip.getnextentry ()! =NULL) {    byte[] buf =New byte[1024]; intnum =-1; Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();  while(num = zip.read (buf, 0, buf.length))! =-1) {baos.write (buf,0, num); } b=Baos.tobytearray ();    Baos.flush ();   Baos.close ();   } zip.close ();  Bis.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;}/*** * Compress BZip2 * *@paramData *@return  */  Public Static byte[] BZIP2 (byte[] data) {  byte[] B =NULL; Try{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Cbzip2outputstream bzip2=NewCbzip2outputstream (BOS);   Bzip2.write (data);   Bzip2.flush ();   Bzip2.close (); b=Bos.tobytearray ();  Bos.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;} /*** * Unzip BZIP2 * *@paramData *@return  */  Public Static byte[] UNBZIP2 (byte[] data) {  byte[] B =NULL; Try{Bytearrayinputstream bis=Newbytearrayinputstream (data); Cbzip2inputstream bzip2=NewCbzip2inputstream (bis); byte[] buf =New byte[1024]; intnum =-1; Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();  while(num = bzip2.read (buf, 0, buf.length))! =-1) {baos.write (buf,0, num); } b=Baos.tobytearray ();   Baos.flush ();   Baos.close ();   Bzip2.close ();  Bis.close (); } Catch(Exception ex) {ex.printstacktrace (); }  returnb;}/*** Convert byte array to 16 binary string * *@paramBarray *@return  */  Public StaticString bytestohexstring (byte[] barray) {StringBuffer SB=NewStringBuffer (barray.length);  String stemp;  for(inti = 0; i < barray.length; i++) {stemp= Integer.tohexstring (0xFF &Barray[i]); if(Stemp.length () < 2) Sb.append (0);  Sb.append (Stemp.touppercase ()); }  returnsb.tostring ();}/***jzlib Compressing Data * *@paramObject *@return  * @throwsIOException*/  Public Static byte[] Jzlib (byte[] object) {  byte[] data =NULL; Try{Bytearrayoutputstream out=NewBytearrayoutputstream (); Zoutputstream Zout=NewZoutputstream (out, jzlib.z_default_compression); DataOutputStream Objout=NewDataOutputStream (zout);   Objout.write (object);   Objout.flush ();   Zout.close (); Data=Out.tobytearray ();  Out.close (); } Catch(IOException e) {e.printstacktrace (); }  returndata;} /***jzlib Compressed Data * *@paramObject *@return  * @throwsIOException*/  Public Static byte[] Unjzlib (byte[] object) {  byte[] data =NULL; Try{Bytearrayinputstream in=NewBytearrayinputstream (object); Zinputstream ZIn=NewZinputstream (in); byte[] buf =New byte[1024]; intnum =-1; Bytearrayoutputstream BAOs=NewBytearrayoutputstream ();  while(num = zin.read (buf, 0, buf.length))! =-1) {baos.write (buf,0, num); } Data=Baos.tobytearray ();   Baos.flush ();   Baos.close ();   Zin.close ();     In.close (); } Catch(IOException e) {e.printstacktrace (); }  returndata;}  Public Static voidMain (string[] args) {String s= "This is a test"; byte[] B1 =Zip (s.getbytes ()); System.out.println ("Zip:" +bytestohexstring (B1)); byte[] B2 =UnZip (B1); System.out.println ("UnZip:" +NewString (B2)); byte[] B3 =bZip2 (S.getbytes ()); System.out.println ("BZIP2:" +bytestohexstring (b3)); byte[] B4 =UNBZIP2 (B3); System.out.println ("UNBZIP2:" +NewString (B4)); byte[] B5 =GZip (S.getbytes ()); System.out.println ("BZIP2:" +bytestohexstring (B5)); byte[] B6 =Ungzip (B5); System.out.println ("UNBZIP2:" +NewString (B6)); byte[] B7 =Jzlib (S.getbytes ()); System.out.println ("Jzlib:" +bytestohexstring (B7)); byte[] B8 =Unjzlib (B7); System.out.println ("Unjzlib:" +NewString (B8)); }}

Java compression and decompression string, byte array, string

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.