在android平台中如何實作zip檔案的解壓縮功能呢? 因為android内部已經內建了zlib庫,對于英文和非密碼的zip檔案解壓縮還是比較簡單的,下面android123給大家一個解壓縮zip的java代碼,可以在android上任何版本中使用,unzip這個靜态方法比較簡單,參數一為源zip檔案的完整路徑,參數二為解壓縮後存放的檔案夾。
private static void unzip(string zipfile, string targetdir) {
int buffer = 4096; //這裡緩沖區我們使用4kb,
string strentry; //儲存每個zip的條目名稱
try {
bufferedoutputstream dest = null; //緩沖輸出流
fileinputstream fis = new fileinputstream(zipfile);
zipinputstream zis = new zipinputstream(new bufferedinputstream(fis));
zipentry entry; //每個zip條目的執行個體
while ((entry = zis.getnextentry()) != null) {
try {
log.i("unzip: ","="+ entry);
int count;
byte data[] = new byte[buffer];
strentry = entry.getname();
file entryfile = new file(targetdir + strentry);
file entrydir = new file(entryfile.getparent());
if (!entrydir.exists()) {
entrydir.mkdirs();
}
fileoutputstream fos = new fileoutputstream(entryfile);
dest = new bufferedoutputstream(fos, buffer);
while ((count = zis.read(data, 0, buffer)) != -1) {
dest.write(data, 0, count);
dest.flush();
dest.close();
} catch (exception ex) {
ex.printstacktrace();
}
}
zis.close();
} catch (exception cwj) {
cwj.printstacktrace();
}
}
上面是android開發網總結的zip檔案解壓縮代碼,希望你大家有用,需要注意的是參數均填寫完整的路徑,比如/mnt/sdcard/xxx.zip這樣的類型。