推荐一个比较好用的zip库

添加下面依赖到你的项目pom文件中。

<dependency>  
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-zip</artifactId>
    <version>1.11</version>
</dependency>  

使用列子

解压

检查一个文件是否存在一个zip中

boolean exists = ZipUtil.containsEntry(new File("/tmp/demo.zip"), "foo.txt");  

解压压缩包的文件中的文件到二进制流中(系统编码)

byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt");  

解压压缩包的文件中的文件到二进制流中(包含编码)

byte[] bytes = ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", Charset.forName("IBM437"));  

将一个指定的文件从zip中解压出来

ZipUtil.unpackEntry(new File("/tmp/demo.zip"), "foo.txt", new File("/tmp/bar.txt"));  

将一个zip解压到指定文件夹中

ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"));  

解压一个文件到名字跟这个zip一样的文件夹中,当前zip文件会被删除

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;
  }
});

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;
  }
});

打印出一个压缩文件中所有.class文件

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());
  }
});

压缩

压缩一个文件夹到一个压缩zip中

ZipUtil.pack(new File("/tmp/demo"), new File("/tmp/demo.zip"));  

将一个带后缀的文件夹压缩成zip文件

ZipUtil.unexplode(new File("/tmp/demo.zip"));  

生成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"));  

将多个流添加到压缩文件中,然后导出到输出流

ZipEntrySource[] entries = new ZipEntrySource[] {  
    new FileSource("doc/readme.txt", new File("foo.txt")),
    new ByteSource("sample.txt", "bar".getBytes())
};
OutputStream out = null;  
try {  
  out = new BufferedOutputStream(new FileOutputStream(new File("/tmp/new.zip")));
  ZipUtil.addEntries(new File("/tmp/demo.zip"), entries, out);
}
finally {  
  IOUtils.closeQuietly(out);
}

替换压缩文件中的某个文件

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"));  

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"));  

Add or replace entries in a ZIP archive

ZipEntrySource[] addedEntries = new ZipEntrySource[] {  
        new FileSource("/path/in/zip/File1.txt", new File("/tmp/file1.txt")),
        new FileSource("/path/in/zip/File2.txt", new File("/tmp/file2.txt")),
        new FileSource("/path/in/zip/File3.txt", new File("/tmp/file2.txt")),
    };
ZipUtil.addOrReplaceEntries(new File("/tmp/demo.zip"), addedEntries);  

转化

转化压缩文件中的文本格式

boolean transformed = ZipUtil.transformEntry(new File("/tmp/demo"), "sample.txt", new StringZipEntryTransformer() {  
    protected String transform(ZipEntry zipEntry, String input) throws IOException {
        return input.toUpperCase();
    }
}, new File("/tmp/demo.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");  

尤鹏飞

继续阅读此作者的更多文章