在Java平台上有很多官方的和非官方、第三方的壓縮公用程式包,它們各有各的長處,比如Oracle官方的java.util.zip 類庫,Apache網站上的Apache Commons Compress 類庫,或者Chilkat
Java Zip 類庫,但總體說來,這些類庫提供都是低層級的API,操作起來都不是很方便,而今天推薦給大家的這個叫做ZeroTurnaround(簡稱zt-zip)的壓縮類庫的特點就是方便、簡易,我們可以比較一下,如果用標準的Java類庫壓縮一個目錄裡的所有檔案,你需要寫出的代碼大概是這樣:
File dir = new File("demo");ZipOutputStream out = new ZipOutputStream(new FileOutputStream("demo.zip"));try { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; ZipEntry entry = new ZipEntry(file.getName()); entry.setSize(file.length()); entry.setTime(file.lastModified()); out.putNextEntry(entry); FileInputStream in = new FileInputStream(file); try { IOUtils.copy(in, out); } finally { IOUtils.closeQuietly(in); } out.closeEntry(); } } finally { IOUtils.closeQuietly(out);}
而使用 zt-zip 工具包,你的代碼就變成了只有一行:
ZipUtil.pack(new File("demo"), new File("demo.zip"));
你不需要自己去關閉檔案的資料流,這個類庫的介面自動替你你做了這些。
可能經常做Java壓縮編程的人會提到另外一個壓縮類庫:TrueZIP,這也是一個非常好的類庫,而zt-zip跟它比起來的一個優勢是:消耗記憶體很少,這是因為TrueZIP大量的使用了虛擬機器的堆記憶體,而zt-zip卻是只是以資料流的形式進行操作,當然這也是zt-zip的API提供的功能很有針對性、不是TrueZIP
API那樣通用的原因。
你可以在Github上下載這個類庫。
Examples
//Unpacking//Check if an entry exists in a ZIP archiveboolean exists = ZipUtil.containsEntry(new File("/tmp/demo"), "foo.txt");//Extract an entry from a ZIP archive into a byte arraybyte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt");//Extract an entry from a ZIP archive into file systemZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt"));//Extract a ZIP archiveZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"));//Extract a ZIP archive which becomes a directoryZipUtil.explode(new File("/tmp/demo.zip"));//Extract a directory from a ZIP archive including the directory nameZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() { public String map(String name) { return name.startsWith("doc/") ? name : null; }});//Extract a directory from a ZIP archive excluding the directory namefinal String prefix = "doc/"; ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() { public String map(String name) { return name.startsWith(prefix) ? name.substring(prefix.length()) : name; }});//Print .class entry names in a ZIP archiveZipUtil.iterate(new File("/tmp/demo.zip"), new ZipInfoCallback() { public void process(ZipEntry zipEntry) throws IOException { if (zipEntry.getName().endsWith(".class")) System.out.println("Found " + zipEntry.getName()); }});//Print .txt entries in a ZIP archive (uses IoUtils from Commons IO)ZipUtil.iterate(new File("/tmp/demo.zip"), new ZipEntryCallback() { public void process(InputStream in, ZipEntry zipEntry) throws IOException { if (zipEntry.getName().endsWith(".txt")) { System.out.println("Found " + zipEntry.getName()); IOUtils.copy(in, System.out); } }});
//Packing//Compress a directory into a ZIP archiveZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"));//Compress a directory which becomes a ZIP archiveZipUtil.unexplode(new File("/tmp/demo.zip"));//Compress a directory into a ZIP archive with a parent directoryZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"), new NameMapper() { public String map(String name) { return "foo/" + name; }});//Add an entry from file to a ZIP archiveZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("f/tmp/oo.txt"), new File("/tmp/new.zip"));//Add an entry from byte array to a ZIP archiveZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));//Add an entry from file and from byte array to a ZIP archiveZipEntrySource[] entries = new ZipEntrySource[] { new FileSource("doc/readme.txt", new File("foo.txt")), new ByteSource("sample.txt", "bar".getBytes())};ZipUtil.addEntries(new File("/tmp/demo.zip"), entries, new File("/tmp/new.zip"));//Replace a ZIP archive entry from fileboolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("/tmp/foo.txt"), new File("/tmp/new.zip"));//Replace a ZIP archive entry from byte arrayboolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));//Replace a ZIP archive entry from file and byte arrayZipEntrySource[] entries = new ZipEntrySource[] { new FileSource("doc/readme.txt", new File("foo.txt")), new ByteSource("sample.txt", "bar".getBytes())};boolean replaced = ZipUtil.replaceEntries(new File("/tmp/demo.zip"), entries, new File("/tmp/new.zip"));
//Comparison//Compare two ZIP archives (ignoring timestamps of the entries)boolean equals = ZipUtil.archiveEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"));//Compare two ZIP archive entries with same name (ignoring timestamps of the entries)boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo.txt");//Compare two ZIP archive entries with different names (ignoring timestamps of the entries)boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo1.txt", "foo2.txt");
註:本文轉載自:
http://www.aqee.net/development-tools-zt-zip/
https://github.com/zeroturnaround/zt-zip