歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android解壓縮ZIP / GZIP數據(基於InflaterInputStream實現)

Android解壓縮ZIP / GZIP數據(基於InflaterInputStream實現)

日期:2017/3/1 9:32:46   编辑:Linux編程

在實際的項目代碼使用過程中,發現如果用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值可以根據自身的項目定制。

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved