1. 已byte[]方式处理解压的数据,返回byte[]。



  1. public byte[] getZipData(byte[] sourcesByte) {
  2. // Decompress the bytes // 开始解压,  
  3. // 数组长度不够将导致丢失部分数据
  4. int dataLength = 1024*1024; // 对byte[]进行解压,同时可以要解压的数据包中的某一段数据,就好像从zip中解压出某一个文件一样。  
  5. byte[] result = new byte[dataLength];  
  6. try {
  7. Inflater decompresser = new Inflater();  
  8. decompresser.setInput(sourcesByte0sourcesByte.length);   
  9. int resultLength = decompresser.inflate(result); // 返回的是解压后的的数据包大小,  
  10. decompresser.end();
  11. } catch(Exception ex) {}
  12. return result;
  13. }


2. 以流的方式处理解压的数据


public byte[] getZipData(byte[] sourcesByte) {

	Inflater decompresser = new Inflater();
	decompresser.setInput(sourcesByte, 0, sourcesByte.length);
	byte[] zipPostData = decompress(sourcesByte);
	decompresser.end();
return zipPostData;

}

private byte[] decompress(byte[] compress) throws Exception {
   ByteArrayInputStream bais = new ByteArrayInputStream(compress);
   InflaterInputStream iis = new InflaterInputStream(bais);
   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   int c = 0;
   byte[] buf = new byte[1024];
   while (true) {
      c = iis.read(buf);
      if (c == -1){
         break;
      }
      baos.write(buf, 0, c);
   }

   baos.flush();
       baos.close();
       bais.close();
       iis.close();
   return baos.toByteArray();
}



本文转载:CSDN博客