2013-08-31 27 views
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; 
} 
+0

將您的字節緩衝區更改爲大小爲8192。 –

+0

@SotiriosDelimanolis YEAH,但請告訴我這樣做的好處,如果我將它的大小設置爲8192 –

+0

如果您每次閱讀更多內容,您將減少IO OS調用磁盤的次數。 –

回答

0

在代碼中沒有太多可以做的事情。此外,大部分時間都是在實際使用拉鍊時花費的。

所以,即使您將零件花費的時間減少到0,整體增益也會非常小。