在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 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