0
我已經開發了一種壓縮文件的方法,該文件將文件路徑和文件名作爲參數,並將壓縮文件,如下所示,請你告訴我如何修改此方法以提高效率並且更快,因爲我是優化的忠實粉絲。使壓縮過程更優化一個
public File generateZipForAFile(String folderPath, String reportFileName)
throws FileNotFoundException, IOException {
File inputFile = new File(folderPath + reportFileName);
FileInputStream in = new FileInputStream(inputFile);
File outputZipFile = new File(folderPath, reportFileName + ".zip");
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(outputZipFile));
// Add ZIP entry to output stream.
out.putNextEntry(new ZipEntry(reportFileName));
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
out.close();
in.close();
return outputZipFile;
}
將您的字節緩衝區更改爲大小爲8192。 –
@SotiriosDelimanolis YEAH,但請告訴我這樣做的好處,如果我將它的大小設置爲8192 –
如果您每次閱讀更多內容,您將減少IO OS調用磁盤的次數。 –