好库网
package com.vanco.abplayer.util;

import java.util.zip.Deflater;
import java.util.zip.Inflater;
import java.util.zip.DataFormatException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class CompressionTools {

  // Export only static methods
  private CompressionTools() {}

  public static byte[] compress(byte[] value, int offset, int length, int compressionLevel) {  
    ByteArrayOutputStream bos = new ByteArrayOutputStream(length);

    Deflater compressor = new Deflater();

    try {
      compressor.setLevel(compressionLevel);  //灏嗗綋鍓嶅帇缂╃骇鍒缃负鎸囧畾鍊笺€?      compressor.setInput(value, offset, length);
      compressor.finish(); //璋冪敤鏃讹紝鎸囩ず鍘嬬缉搴斿綋浠ヨ緭鍏ョ紦鍐插尯鐨勫綋鍓嶅唴瀹圭粨灏俱€?
      // Compress the data
      final byte[] buf = new byte[1024];
      while (!compressor.finished()) { 
    //濡傛灉宸插埌杈惧帇缂╂暟鎹緭鍑烘祦鐨勭粨灏撅紝鍒欒繑鍥?true銆?        int count = compressor.deflate(buf);
         // 浣跨敤鍘嬬缉鏁版嵁濉厖鎸囧畾缂撳啿鍖恒€?        bos.write(buf, 0, count);
      }
    } finally {
      compressor.end(); //鍏抽棴瑙e帇缂╁櫒骞舵斁寮冩墍鏈夋湭澶勭悊鐨勮緭鍏ャ€?    }

    return bos.toByteArray();
  }
  
  public static byte[] compress(byte[] value, int offset, int length) {
    return compress(value, offset, length, Deflater.BEST_COMPRESSION);
  // 鏈€浣冲帇缂╃殑鍘嬬缉绾у埆
  }
  
   public static byte[] compress(byte[] value) {
    return compress(value, 0, value.length, Deflater.BEST_COMPRESSION);
  }

   public static byte[] decompress(byte[] value) throws DataFormatException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream(value.length);

    Inflater decompressor = new Inflater();

    try {
      decompressor.setInput(value);
   
      final byte[] buf = new byte[1024];
      while (!decompressor.finished()) {
        int count = decompressor.inflate(buf);
        bos.write(buf, 0, count);
      }
    } finally {  
      decompressor.end();
    }
    
    return bos.toByteArray();
  }
   
   public static byte[] decompressXML(byte[] data) throws DataFormatException
   {
    byte[] dest = new byte[data.length+2];
    System.arraycopy(data, 0, dest, 2, data.length);
    dest[0] = 0x78;
    dest[1] = 0x01;
    data = dest;
    Inflater decompresser = new Inflater();
    decompresser.setInput(data);
    
    byte[] bufferArray = new byte[1024];
    ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
    try
    {
     int i = 1;
     while (i != 0)
     {
      i = decompresser.inflate(bufferArray);
      baos.write(bufferArray, 0, i);
     }
     data = baos.toByteArray();
    }
    catch (Exception e)
    {
     e.printStackTrace();
    }
    finally
    {
     try
     {
      baos.flush();
      baos.close();
     }
     catch (IOException e)
     {
      e.printStackTrace();
     }
    }
    
    decompresser.end();
    return data;
   }

}