LoginSignup
6
7

More than 5 years have passed since last update.

【Java】ZIPファイルが超簡単に扱えるユーティリティ「ZT Zip」の紹介

Posted at

ZIPファイルがたった1行のコードで操作できるライブラリを見つけましたので、紹介いたします。

ライブラリ導入(Maven or Gradle)

Javaに慣れてる人であれば知ってると思うので、詳しくは説明しません。

pom.xml
<!-- https://mvnrepository.com/artifact/org.zeroturnaround/zt-zip -->
<dependency>
    <groupId>org.zeroturnaround</groupId>
    <artifactId>zt-zip</artifactId>
    <version>1.13</version>
</dependency>
build.gradle
// https://mvnrepository.com/artifact/org.zeroturnaround/zt-zip
compile group: 'org.zeroturnaround', name: 'zt-zip', version: '1.13'

サンプルコード

よく使うだろうなというコードをいくつか紹介します。
staticメソッドで実装されているので、どれも1行だけで書けてしまっていますね。

//解凍します
ZipUtil.unpack(new File("C:/work/file.zip"), new File("C:/work/unzip/"));

//圧縮します
ZipUtil.pack(new File("C:/work/unzip/"), new File("C:/work/file.zip"));

//ZIPのなかにあるファイルを入れ替える
boolean successFlg = ZipUtil.replaceEntry(new File("C:/work/file.zip"),
        "/config/production.properties", new File("C:/work/production.properties"));

//ZIPのなかへファイルを追加する
ZipUtil.addEntry(new File("C:/work/file.zip"), "/config/develop.properties", new File("C:/work/develop.properties"), new File("C:/work/file_dst.zip"));

もっと詳しく知りたい場合は…

公式ドキュメントを見ましょう…。簡単な英語で書かれています。
zeroturnaround/zt-zip: ZeroTurnaround ZIP Library

6
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
7