用zt-zip Java库进行zip文件处理

Java 标准库本身自带java.util.zip包,利用该包可以解决zip文件的处理问题。但是该包提供的功能相对底层,想要实现zip文件的处理,需要写一 些代码,该包并没有封装API到调用一个方法就实现了压缩或者解压功能的层次。zt-zip库提供了这种上层的封装,只需要调用一个方法,可以很方便的实 现zip压缩与解压,zt-zip在底层也利用了java.util.zip包。

maven中央仓库里已经有这个库

<dependency>
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-zip</artifactId>
    <version>1.8</version>
    <type>jar</type>
</dependency>

解压

检查zip压缩包中是否具有某个文件

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

从zip压缩包中提取一个文件,然后放入byte数组

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

从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压缩包中提取的文件

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

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

提取zip压缩包中的一个目录,不保留目录名

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

从zip压缩包中提取符合文件名模式的文件

ZipUtil.unpack(new File("/tmp/demo.zip"), new File("/tmp/demo"), new NameMapper() {
  public String map(String name) {
    if (name.contains("/doc")) {
      return name;
    }
    else {
      // returning null from the map method will disregard the entry
      return null;
    }
  }
});

打印在zip压缩包中的.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压缩包中的.txt文件 (IoUtils为Apache 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);
    }
  }
});

压缩

把目录里的内容压缩到zip包中

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

把目录本身压缩成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;
  }
});

向zip压缩包里新增一个文件

ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("f/tmp/oo.txt"), new File("/tmp/new.zip"));

以byte数组的形式向zip压缩包里新增一个文件

ZipUtil.addEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));

同时以文件形式和byte数组形式向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"));

同时以文件形式和byte数组形式向zip压缩包里新增文件,并且以OutputStream的形式给出结果

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

覆盖zip压缩包中的文件

boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", new File("/tmp/foo.txt"), new File("/tmp/new.zip"));

以byte数组的形式覆盖zip压缩包中的文件

boolean replaced = ZipUtil.replaceEntry(new File("/tmp/demo.zip"), "doc/readme.txt", "bar".getBytes(), new File("/tmp/new.zip"));

同时以文件形式和byte数组的形式覆盖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"));

新增或覆盖zip压缩包中的文件

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

转换

将zip压缩包中的文件的名称转为大写

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

比较

比较两个zip压缩包是否一致(忽略其中文件的时间差异)

boolean equals = ZipUtil.archiveEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"));

比较两个zip压缩包中有相同名字的文件 (忽略其中文件的时间差异)

boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo.txt");

比较两个zip压缩包中有不同名字的文件 (忽略其中文件的时间差异)

boolean equals = ZipUtil.entryEquals(new File("/tmp/demo1.zip"), new File("/tmp/demo2.zip"), "foo1.txt", "foo2.txt");
发布了106 篇原创文章 · 获赞 15 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/skyupward/article/details/104895327