软件开发工具推荐:ZeroTurnaround ZIP 类库

在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"));人生难免有挫折,但你是逃避不了的,一定要去面对它

软件开发工具推荐:ZeroTurnaround ZIP 类库

相关文章:

你感兴趣的文章:

标签云: