7-Zip-JBinding Java压缩开源软件

09-09-05 banq
    

7-Zip-JBinding是使用7-Zip C++压缩算法的JNI库包,它和java.util.zip使用方法几乎一样,但是性能完全不同:

以包含110 jpg 的104MB zip文件为例:

java.util.zip:12203

7-Zip-JBinding:409

7-Zip-JBinding的性能要远远超过java.util.zip近30倍。

两者使用方式几乎一样:

java.util.zip:

static public int getSize(File file) {
    ZipFile zf = null;
    int total = -1;

    try {
        zf = new ZipFile(file);
        total = 0;
        for (Enumeration e = zf.entries(); e.hasMoreElements();) {
            e.nextElement();
            total++;
        }
        zf.close();
    } catch (ZipException ex) {
        Logger.getLogger(Zip.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(Zip.class.getName()).log(Level.SEVERE, null, ex);
    }

    return total;
}
<p>

7-Zip-JBinding

public int getSize (File file) {
     RandomAccessFile randomAccessFile = null;
     ISevenZipInArchive inArchive = null;
     int total = -1;
     try {
         randomAccessFile = new RandomAccessFile(file.getAbsolutePath(), "r");
         inArchive = SevenZip.openInArchive(null, // autodetect archive type
                 new RandomAccessFileInStream(randomAccessFile));

         total = inArchive.getNumberOfItems();

     } catch (Exception e) {
         System.err.println("Error occurs: " + e);
         System.exit(1);
     } finally {
         if (inArchive != null) {
             try {
                 inArchive.close();
             } catch (SevenZipException e) {
                 System.err.println("Error closing archive: " + e);
             }
         }
         if (randomAccessFile != null) {
             try {
                 randomAccessFile.close();
             } catch (IOException e) {
                 System.err.println("Error closing file: " + e);
             }
         }
     }

     return total;
 }
<p>

原文:

The Best Way of Working with Zip Files in Java

[该贴被banq于2009-09-05 09:49修改过]

    

xysniper
2009-09-24 15:08

对167M的ZIP文件执行getSize()方法,测试如下:

java.util.zip:至少60毫秒

7-zip-jbinding:至少320毫秒

从测试结果上看,java的比7-zip的明显快的多呀,我个人认为java的永远都是最快的,如果有比java的还快的工具,那sun确实不好混了

[该贴被thinkjava于2009-09-24 15:11修改过]