閱讀270 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Android Zip文件解壓縮代碼

 在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這樣的類型。

最後更新:2017-04-02 06:51:46

  上一篇:go Android裁剪圖片最簡單方法
  下一篇:go android圖片類型之間的互轉