我需要在 android 中解压一个 2.5mb 的 .zip 文件(1087 个文件 - *.html、*.css 和 *.db),我使用过 java.util.zip,它工作正常,但我需要改进性能,解压缩过程持续 1.10 分钟,我需要减少这段时间。 我遵循了一些提高性能的建议,例如:
- 使用 BufferedInputStream、FileOutputStream 和 BufferedOutputStream。
- 分块阅读 zip :
字节数据[] = 新字节[2048]; while ((counter = bisMediaFile.read(data, 0, 2048)) != -1) { bosMediaFile.write(数据,0,计数器);
有什么方法可以改进我的代码吗?。我正在搜索第三方 zip 程序以编程方式使用,例如我尝试了 7ZipJBinding,但它看起来像 android 不支持这个,因为我引用了 sevenzipjbinding.jar 和 sevenzipjbinding-AllPlatforms.jar 但我得到一个错误:“Native在 sevenzipjbinding-AllPlatforms 中检测到的库”。在 7zip 主页上有 MAC、Windows、Linux 版本,但我没有看到任何关于 android 的信息。 您能否推荐任何其他库来解压缩 android 中的文件?
这是我的全部代码:
public static void processZipFile(String strBinaryPath,String strExtractPath, String strDestinationDBPath) throws Exception
{
ZipFile zipInFile = null;
try
{
if (strExtractPath != null)
{
zipInFile = new ZipFile(strBinaryPath);
for (Enumeration<? extends ZipEntry> entries = zipInFile.entries(); entries.hasMoreElements();)
{
ZipEntry zipMediaEntry = entries.nextElement();
if (zipMediaEntry.isDirectory())
{
File mediaDir = new File(String.format("%s\\%s", strExtractPath, zipMediaEntry.getName()));
mediaDir.mkdirs();
}
else
{
BufferedInputStream bisMediaFile = null;
FileOutputStream fosMediaFile = null;
BufferedOutputStream bosMediaFile = null;
try
{
String strFileName = String.format("%s\\%s", strExtractPath, zipMediaEntry.getName());
File uncompressDir = new File(strFileName).getParentFile();
uncompressDir.mkdirs();
//if is a database file, extract to other path : android.movinginteractive.com/databases
if(strFileName.contains(".db"))
strFileName = String.format("%s\\%s", strDestinationDBPath, ExtractDBName(zipMediaEntry.getName()));
bisMediaFile = new BufferedInputStream(zipInFile.getInputStream(zipMediaEntry));
fosMediaFile = new FileOutputStream(strFileName);
bosMediaFile = new BufferedOutputStream(fosMediaFile);
int counter;
byte data[] = new byte[2048];
while ((counter = bisMediaFile.read(data, 0, 2048)) != -1)
{
bosMediaFile.write(data, 0, counter);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (bosMediaFile != null)
{
bosMediaFile.flush();
bosMediaFile.close();
}
if (bisMediaFile != null)
bisMediaFile.close();
}
}
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (zipInFile != null)
zipInFile.close();
File flZipToDelete = new File(strBinaryPath);
if(flZipToDelete.exists())
flZipToDelete.delete();
}
}
最佳答案
我相信您可以找到用于解压缩文件并通过 Android NDK 运行它的 C 或 C++ 代码 fragment 。也就是说,我不确定您可以获得什么性能提升。
关于android - 以比在 Android 中使用 java.util.zip 更快的方式解压缩文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5019990/