我需要在android中解壓縮2.5mb(1087個文件 - * .html,* .css和* .db)的.zip文件,我有使用java.util.zip,它工作正常,但我需要提高性能,解壓縮過程最後1.10分鐘,我需要減少這個時間。 我按照一些recomendations爲提高性能,例如:以比使用Android中的java.util.zip更快的方式解壓縮文件
- 使用的BufferedInputStream,FileOutputStream中和的BufferedOutputStream。
- 閱讀塊的zip:
字節數據[] =新的字節[2048]; ((counter = bisMediaFile.read(data,0,2048))!= -1) { bosMediaFile.write(data,0,counter); }
有什麼辦法可以改善我的代碼嗎?我正在搜索第三方zip程序使用編程方式,例如我嘗試了7ZipJBinding,但它看起來像android不支持這一點,因爲我引用sevenzipjbinding.jar和sevenzipjbinding-AllPlatforms.jar,但我得到一個錯誤:「本機在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();
}
}
您應該剖析/計時您的代碼,以查看時間花在哪裏。您可以使用7zip或Info-ZIP的解壓縮作爲參考,查看應該以多快的速度執行合理的實施。 – 2011-02-16 17:47:06