首页 / 操作系统 / Linux / Android解压缩ZIP / GZIP数据(基于InflaterInputStream实现)
在实际的项目代码使用过程中,发现如果用Java类库标准指定的GZIPInputStream读取压缩数据解压不能稳定工作,原因不明。反而使用InflaterInputStream可以替代GZIPInputStream稳定、正常工作,现在把基于InflaterInputStream的zipgzip解压代码工具方法给出: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值可以根据自身的项目定制。更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11本文永久更新链接地址:http://www.linuxidc.com/Linux/2015-02/113795.htm