標籤:des android style class blog java
2011-04-01 17:58:52| 分類: Android |舉報 |字型大小 訂閱
在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這樣的類型。