为了解压缩 zip 文件,我使用了包 java.util.zip* 中的类,引用 this它可以正常工作,但是解压缩 40MB 的文件需要 59 秒。当我在 iPhone 项目上尝试相同的 zip 文件时(我们正在为两个平台开发应用程序 - Android 和 iPone 并且具有解压缩 zip 文件并将解压缩的内容保存到 SDCARD-Android 或文档目录 - iPhone 的功能) , 仅需 14 秒。 iphone 应用程序使用 ziparchive .
所以我的问题是:
1.从上面的实验可以看出,与 iPhone 应用程序相比,在 Java 中对 SDCARD 的解压缩和文件写入操作会消耗更多时间,因此我决定使用 NDK 使用 C/C++ 级别的解压缩和文件写入操作。 这是正确的选择吗?
2.我在谷歌、stackoverflow 上搜索过,有些人建议使用 minizip但是关于如何在 android 中使用 minizip 没有足够的帮助。有人试过 minizip for android 吗?
3.我还尝试了 NDK development for libz 来实现我的目标,因为 NDK 中添加了 Libz 但不知道如何使用它。有人在 NDK 中尝试过 libz 吗?
4.是否有任何其他 Java 或 C/C++ 框架可以解压缩大型 zip 文件并在更短的时间内将它们写入 SDCARD?
请帮帮我。
这是我的 Java 解压代码:
public String unzip() {
String result;
try {
FileInputStream fin = new FileInputStream(this.filePath);
ZipInputStream zin = new ZipInputStream(fin);
ZipEntry ze = null;
while ((ze = zin.getNextEntry()) != null) {
Log.v("Unzip", "Unzipping " + ze.getName());
if (ze.isDirectory()) {
_dirChecker(ze.getName());
} else {
// Read 16 k at a time
byte[] buffer = new byte[16*1024];
int read;
FileOutputStream fout = new FileOutputStream(this.location+ "/" + ze.getName());
while ((read = zin.read(buffer)) != -1)
{
fout.write(buffer, 0, read);
}
zin.closeEntry();
fout.close();
}
}
zin.close();
result = "success";
} catch (Exception e) {
Log.e("unzip", "unzip", e);
result = "failure";
}
return result;
}
最佳答案
你为什么不试试这个代码。它工作得很棒
String zipname = "data.zip";
FileInputStream fis = new FileInputStream(zipname);
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
System.out.println("Unzipping: " + entry.getName());
int size;
byte[] buffer = new byte[2048];
FileOutputStream fos = new FileOutputStream(entry.getName());
BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);
while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
bos.write(buffer, 0, size);
}
bos.flush();
bos.close();
}
zis.close();
fis.close();
关于android - 如何解压缩大型 zip 文件并在更短的时间内写入 SDCARD,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14218178/