1. 已byte[]方式处理解压的数据,返回byte[]。
- public byte[] getZipData(byte[] sourcesByte) {
- // Decompress the bytes // 开始解压,
- // 数组长度不够将导致丢失部分数据
- int dataLength = 1024*1024; // 对byte[]进行解压,同时可以要解压的数据包中的某一段数据,就好像从zip中解压出某一个文件一样。
- byte[] result = new byte[dataLength];
- try {
- Inflater decompresser = new Inflater();
- decompresser.setInput(sourcesByte, 0, sourcesByte.length);
- int resultLength = decompresser.inflate(result); // 返回的是解压后的的数据包大小,
- decompresser.end();
- } catch(Exception ex) {}
- return result;
- }
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(); }