Java compression and decompression string __java compressed string

Source: Internet
Author: User
Tags stringbuffer
Reprint Address: https://www.cnblogs.com/dongzhongwei/p/5964758.html
LZ: Ding Dong ^v^
/*** * Compressed gzip * * @param data * @return/public static byte[] GZip (byte[] data) {byte[] b = null;
   try {bytearrayoutputstream bos = new Bytearrayoutputstream ();
   Gzipoutputstream gzip = new Gzipoutputstream (BOS);
   Gzip.write (data);
   Gzip.finish ();
   Gzip.close ();
   b = Bos.tobytearray ();
  Bos.close ();
  catch (Exception ex) {ex.printstacktrace ();
 return b;
  /*** * Extract gzip * * @param data * @return/public static byte[] Ungzip (byte[] data) {byte[] b = null;
   try {Bytearrayinputstream bis = new Bytearrayinputstream (data);
   Gzipinputstream gzip = new Gzipinputstream (bis);
   byte[] buf = new byte[1024];
   int num =-1;
   Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
   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 (); } rEturn b;
  /*** * Compressed Zip * * @param data * @return/public static byte[] Zip (byte[] data) {byte[] b = null;
   try {bytearrayoutputstream bos = new Bytearrayoutputstream ();
   Zipoutputstream zip = new Zipoutputstream (BOS);
   ZipEntry entry = new ZipEntry ("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 ();
 return b;
  /*** * Extract Zip * * @param data * @return/public static byte[] UnZip (byte[] data) {byte[] b = null;
   try {Bytearrayinputstream bis = new Bytearrayinputstream (data);
   Zipinputstream zip = new Zipinputstream (bis);
    while (zip.getnextentry ()!= null) {byte[] buf = new byte[1024];
    int num =-1;
    Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
    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 ();
 return b;
  /*** * Compressed BZIP2 * * @param data * @return/public static byte[] BZIP2 (byte[] data) {byte[] b = null;
   try {bytearrayoutputstream bos = new Bytearrayoutputstream ();
   Cbzip2outputstream bzip2 = new Cbzip2outputstream (BOS);
   Bzip2.write (data);
   Bzip2.flush ();
   Bzip2.close ();
   b = Bos.tobytearray ();
  Bos.close ();
  catch (Exception ex) {ex.printstacktrace ();
 return b; 
  /*** * Extract BZIP2 * * @param data * @return/public static byte[] UNBZIP2 (byte[] data) {byte[] b = null;
   try {Bytearrayinputstream bis = new Bytearrayinputstream (data);
   Cbzip2inputstream bzip2 = new Cbzip2inputstream (bis);
   byte[] buf = new byte[1024];
   int num =-1;
   Bytearrayoutputstream BAOs = new Bytearrayoutputstream (); 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 ();
 return b; 
  /** * Converts a byte array to a 16-character string * * @param barray * @return/public static string Bytestohexstring (byte[] barray) {
  StringBuffer sb = new StringBuffer (barray.length);
  String stemp;
   for (int i = 0; i < barray.length i++) {stemp = integer.tohexstring (0xFF & Barray[i]);
   if (Stemp.length () < 2) sb.append (0);
  Sb.append (Stemp.touppercase ());
 return sb.tostring (); /** *jzlib Compressed Data * * @param object * @return * @throws ioexception/public static byte[] Jzlib (byte[) o
  bject) {byte[] data = null;
   try {bytearrayoutputstream out = new Bytearrayoutputstream ();
   Zoutputstream zout = new Zoutputstream (out, jzlib.z_default_compression);
   DataOutputStream objout = new DataOutputStream (zout);
   Objout.write (object); Objout.Flush ();
   Zout.close ();
   data = Out.tobytearray ();
  Out.close ();
  catch (IOException e) {e.printstacktrace ();
 } return data; /** *jzlib Compressed Data * * @param object * @return * @throws ioexception/public static byte[] Unjzlib (byte[)
  Object) {byte[] data = null;
   try {bytearrayinputstream in = new Bytearrayinputstream (object);
   Zinputstream zIn = new Zinputstream (in);
   byte[] buf = new byte[1024];
   int num =-1;
   Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();
   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 ();
 } return data;
  
  public static void Main (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:"+ New String (B2));
  byte[] B3 = bZip2 (S.getbytes ());
  System.out.println ("BZIP2:" + bytestohexstring (b3));
  byte[] B4 = unBZip2 (b3);
  System.out.println ("UNBZIP2:" + new String (B4));
  Byte[] B5 = GZip (S.getbytes ());
  System.out.println ("BZIP2:" + bytestohexstring (B5));
  Byte[] B6 = Ungzip (B5);
  System.out.println ("UNBZIP2:" + new String (B6));
  byte[] B7 = Jzlib (S.getbytes ());
  System.out.println ("Jzlib:" + bytestohexstring (B7));
  byte[] B8 = Unjzlib (b7);
 System.out.println ("Unjzlib:" + new String (B8)); }
}
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.