2013-08-31 28 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,整体增益也会非常小。