2010-06-04 216 views
4

我想使用ZipFile類從多個文件的壓縮文件中使用其名稱解壓縮文件。我怎樣才能得到zip文件名和目錄的字符串傳遞給ZipFile構造函數?使用ZipFile類從多個文件的zip壓縮文件解壓縮文件

+0

的路徑是什麼名字,如果壓縮文件是在資產目錄?我最好將它複製到應用程序文件目錄中嗎? – CalvinS 2010-06-04 14:33:31

+0

APK已經是壓縮的ZIP壓縮文件。在APK中放入ZIP是浪費時間 - 只需將APK的內容放在那裏即可。 – CommonsWare 2010-06-04 16:04:05

回答

4

您可以使用AssetManager和ZipInputStream http://developer.android.com/reference/android/content/res/AssetManager.html

ZipInputStream in = null; 
try { 
    final String zipPath = "data/sample.zip"; 
    // Context.getAssets() 
    in = new ZipInputStream(getAssets().open(zipPath)); 
    for (ZipEntry entry = in.getNextEntry(); entry != null; entry = in.getNextEntry()) { 
     // handle the zip entry 
    } 
} catch (IOException e) { 
    Log.e(TAG, e.getMessage()); 
} finally { 
    try { 
     if (in != null) { 
      in.close(); 
     } 
    } catch (IOException ignored) { 
    } 
    in = null; 
}