在实际的项目代码使用过程中,发现如果用Java类库标准指定的GZIPInputStream读取压缩数据解压不能稳定工作,原因不明。反而使用InflaterInputStream可以替代GZIPInputStream稳定、正常工作,现在把基于InflaterInputStream的zip\gzip解压代码工具方法给出:
public static 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[BUFFER_SIZE];
? while (true) {
? ?c = iis.read(buf);
? ?if (c == EOF)
? ? break;
? ?baos.write(buf, 0, c);
? }
? baos.flush();
? return baos.toByteArray();
?}
其中,EOF = -1,BUFFER_SIZE值可以根据自身的项目定制。