软件开发工具推荐:ZeroTurnaround ZIP 类库
2011-12-14 19:34
513 查看
在Java平台上有很多官方的和非官方、第三方的压缩工具包,它们各有各的长处,比如Oracle官方的java.util.zip 类库,Apache网站上的Apache Commons Compress 类库,或者Chilkat
Java Zip 类库,但总体说来,这些类库提供都是低级别的API,操作起来都不是很方便,而今天推荐给大家的这个叫做ZeroTurnaround(简称zt-zip)的压缩类库的特点就是方便、简易,我们可以比较一下,如果用标准的Java类库压缩一个目录里的所有文件,你需要写出的代码大概是这样:
而使用 zt-zip 工具包,你的代码就变成了只有一行:
你不需要自己去关闭文件的数据流,这个类库的接口自动替你你做了这些。
可能经常做Java压缩编程的人会提到另外一个压缩类库:TrueZIP,这也是一个非常好的类库,而zt-zip跟它比起来的一个优势是:消耗内存很少,这是因为TrueZIP大量的使用了虚拟机的堆内存,而zt-zip却是只是以数据流的形式进行操作,当然这也是zt-zip的API提供的功能很有针对性、不是TrueZIP
API那样通用的原因。
你可以在Github上下载这个类库。
Examples
注:本文转载自:
http://www.aqee.net/development-tools-zt-zip/
https://github.com/zeroturnaround/zt-zip
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 archive boolean exists = ZipUtil.containsEntry(new File("/tmp/demo"), "foo.txt"); //Extract an entry from a ZIP archive into a byte array byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt"); //Extract an entry from a ZIP archive into file system ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt")); //Extract a ZIP archive ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo")); //Extract a ZIP archive which becomes a directory ZipUtil.explode(new File("/tmp/demo.zip")); //Extract a directory from a ZIP archive including the directory name ZipUtil.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 name final 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 archive ZipUtil.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 archive ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip")); //Compress a directory which becomes a ZIP archive ZipUtil.unexplode(new File("/tmp/demo.zip")); //Compress a directory into a ZIP archive with a parent directory ZipUtil.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 archive ZipUtil.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 archive ZipUtil.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 archive ZipEntrySource[] 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 file boolean 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 array boolean 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 array ZipEntrySource[] 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
相关文章推荐
- 软件开发工具推荐:ZeroTurnaround ZIP 类库(java zip/unzip)
- ZeroTurnaround ZIP类库
- Swing,RPC,SWT,GWT,AWT 软件开发工具推荐网址及其安装方法
- 软件开发与测试工具推荐
- 软件开发工具推荐 :Gow
- 软件开发与测试工具推荐(持续更新)
- ZeroTurnaround JRebel(一):热部署工具简单介绍
- 强烈推荐大家使用cpp-ut-project作为C/C++的TDD开发工具套件
- SUP - Sybase提供的移动软件快速开发工具,很好的支持BlackBerry
- 高性能WEB开发:web性能测试工具推荐
- 微信开发工具SDK推荐
- Android开发中常用的工具软件
- 【工具推荐】好用的.NET编译和开发工具 Snippet Compiler
- 推荐10款非常优秀的 HTML5 开发工具
- 计算机专业及软件开发推荐书籍
- 前端开发工具webStorm快捷键和主题推荐
- HTML5规范尘埃落定,5个开发工具推荐
- C#软件开发实例.私人订制自己的屏幕截图工具(七)加入放大镜的功能
- 项目经理的工具箱---走出软件作坊:三五个人十来条枪 如何成为开发正规军(三) 推荐
- 最全控件,开发工具,国外软件