用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");
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章