我的應用程序下載約350個文件的zip。 JPG和HTML文件的混合。我寫的功能做得很好,但解壓縮需要永遠。 起初我認爲原因可能是寫入SD卡的速度很慢。但是當我解壓縮與我的手機上的其他應用程序相同的壓縮文件時,它的運行速度更快。有什麼我可以做的優化它嗎?對SD卡的Extrakting Zip速度非常慢。我如何優化性能?
這裏是代碼:
private void extract() {
try {
FileInputStream inStream = new FileInputStream(targetFilePath);
ZipInputStream zipStream = new ZipInputStream(new BufferedInputStream(inStream));
ZipEntry entry;
ZipFile zip = new ZipFile(targetFilePath);
//i know the contents for the zip so i create the dirs i need in advance
new File(targetFolder).mkdirs();
new File(targetFolder + "META-INF").mkdir();
new File(targetFolder + "content").mkdir();
int extracted = 0;
while((entry = zipStream.getNextEntry()) != null) {
if (entry.isDirectory()) {
new File(targetFolder + entry.getName()).mkdirs();
} else {
FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName());
for (int c = zipStream.read(); c != -1; c = zipStream.read()) {
outStream.write(c);
}
zipStream.closeEntry();
outStream.close();
extracted ++;
}
publishProgress(""+(int)extracted*100/zip.size());
}
zipStream.close();
inStream.close();
//
new File(targetFilePath).delete();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
感謝CommonsWare我修改了代碼:
int size;
byte[] buffer = new byte[2048];
FileOutputStream outStream = new FileOutputStream(targetFolder + entry.getName());
BufferedOutputStream bufferOut = new BufferedOutputStream(outStream, buffer.length);
while((size = zipStream.read(buffer, 0, buffer.length)) != -1) {
bufferOut.write(buffer, 0, size);
}
bufferOut.flush();
bufferOut.close();
很大的性能差異。 非常感謝。
謝謝!那實際上做了把戲。 – notme 2010-10-20 13:02:04
此答案中提供的鏈接無效。 – 2012-10-30 08:47:08
@CommonsWare鏈接已關閉,您能否修復該問題? – Scorchio 2013-08-01 10:51:25