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; }
|
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; }
|
原文:
The Best Way of Working with Zip Files in Java
[该贴被banq于2009-09-05 09:49修改过]