在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
-
-
-
- boolean exists = ZipUtil.containsEntry(new File("/tmp/demo"), "foo.txt");
-
- byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt");
-
- ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt"));
-
- ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"));
-
- ZipUtil.explode(new File("/tmp/demo.zip"));
-
- ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {
- public String map(String name) {
- return name.startsWith("doc/") ? name : null;
- }
- });
-
- 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;
- }
- });
-
- 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());
- }
- });
-
- 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);
- }
- }
- });
-
-
-
- ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"));
-
- ZipUtil.unexplode(new File("/tmp/demo.zip"));
-
- ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"), new NameMapper() {
- public String map(String name) {
- return "foo/" + name;
- }
- });
-
- ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("f/tmp/oo.txt"), new File("/tmp/new.zip"));
-
- ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));
-
- 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"));
-
- boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("/tmp/foo.txt"), new File("/tmp/new.zip"));
-
- boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));
-
- 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"));
-
-
-
- boolean equals = ZipUtil.archiveEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"));
-
- boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo.txt");
-
- 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
http://blog.csdn.net/davidhsing/article/details/7071581