Android Zip檔案解壓縮代碼

來源:互聯網
上載者:User

標籤: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這樣的類型。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.