Android上实现zip解压缩

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();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值