Java compression and decompression method with list object as an example

Source: Internet
Author: User

 /**
  * Feature Description: Convert list object compression to byte[]
  * @param data
  * @return
  * @throws IOException
  */
 public static byte[] Compresslist (List data) throws IOException {
   Byte[] Result=null;
  //The output stream used by serialization
  bytearrayoutputstream o = new Bytearrayoutputstream ();
 &NBSP
  gzipoutputstream gzout=new gzipoutputstream (o);
 &NBSP
  //Create object serialization output stream
  objectoutputstream out = new ObjectOutputStream (gzout);
  out.writeobject (data);
  out.flush ();
  out.close ();
  gzout.close ();
  result=o.tobytearray ();
  o.close ();
  return result;

 }
 /**
  * Feature Description: Extract byte[] data into List object on top process reverse
  * @param data
  * @return
  * @ Throws IOException
  */
 public static List uncompresslist (byte[) data throws IOException {
 & nbsp List Result=null;
  
  bytearrayinputstream i = new Bytearrayinputstream (data);
 &NBSP
  gzipinputstream gzin=new gzipinputstream (i);
 &NBSP
  objectinputstream in = new ObjectInputStream (Gzin);
  
  try {
   result= (List) in.readobject ();
 &NBSP} catch (ClassNotFoundException e) {
   //TODO auto-generated catch block
    e.printstacktrace ();
  }
  
  i.close ();
  gzin.close ();
  in.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.