使用ZipFile类从多个文件的zip归档中解压缩文件

4

我想使用ZipFile类从包含多个文件的归档中解压文件。我该如何获取zip文件名称和目录的字符串以传递给ZipFile构造函数?


如果zip文件在assets目录中,路径名是什么?将其复制到应用程序文件目录是最好的选择吗? - CalvinS
一个APK本身就是一个压缩的ZIP归档文件。将一个ZIP放入APK中是浪费时间,只需将APK的内容放在其中即可。 - CommonsWare
1个回答

4
您可以使用AssetManager和ZipInputStreamhttp://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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接