コマンドラインからファイルやディレクトリを指定してzipファイルを作るサンプルです。
commons-compressを使っています。
前回はテキストファイル1個だけのzipファイルを作ったので、今度はファイルやディレクトリを圧縮するサンプルを作ってみました。
前回のも、ファイルが無くてもメモリ上のデータからzipファイルを作れるのがわかって面白いかなと思いました。
サンプルソース
// Zipファイル圧縮サンプル2
// 指定されたファイルを圧縮するサンプル
// 第一パラメータにzipファイルの名前、第二パラメータ以降に圧縮するファイルを指定します
// それ以外のオプションはありません
import java.io.*;
import org.apache.commons.compress.archivers.zip.*;
public class ZipCompSample2 {
public static void main(String[] args) throws Exception {
if (args.length < 2) {
// オプションがない場合、sampleディレクトリ(ファイル)を圧縮してsampleComp2.zipを作成します。
comp(new String[] { "sampleComp2.zip", "sample" });
} else {
comp(args);
}
}
/**
* mainから呼ばれる圧縮メソッド
*
* @param args
* [0]=zipファイル名 [1]以降 圧縮するファイル
* @throws Exception
*/
public static void comp(String[] args) throws Exception {
// 1 zipに書き込むZipArchiveOutputStreamを作成します
ZipArchiveOutputStream os = new ZipArchiveOutputStream(new File(args[0]));
// 2.中の圧縮ファイルに適用するエンコードを指定します。システム標準にしてみたのでWindowsならMS932になります。
os.setEncoding(System.getProperty("file.encoding"));
// 第二パラメータ以降を圧縮対象としてぐるぐる回します
for (int i = 1; i < args.length; i++) {
comp(os, args[i]);
}
// 7.最後にアウトプットストリームをクローズします。
os.close();
}
/**
* mainから呼ばない圧縮メソッド
*
* @param os
* Zipのアウトプットストリーム
* @param path
* 圧縮するファイルのパス
* @throws Exception
*/
public static void comp(ZipArchiveOutputStream os, String path) throws Exception {
comp(os, path, null);
}
/**
* mainから呼ばない圧縮メソッドその2
*
* @param os
* Zipのアウトプットストリーム
* @param path
* 圧縮するファイルのパス
* @param basePath
* ベースディレクトリ(圧縮する際、除外するディレクトリ名)
* @throws Exception
*/
public static void comp(ZipArchiveOutputStream os, String path, String basePath) throws Exception {
// ファイルオブジェクト取得
File f = new File(path);
// ベースディレクトリがnullの場合
if (basePath == null) {
// 圧縮するファイルのパスからベースディレクトリを取得します
// 絶対パスを取得
f = f.getAbsoluteFile();
// 親ディレクトリを取得
File p = f.getParentFile();
// 親ディレクトリのパスをベースディレクトリとします
basePath = p.getAbsolutePath();
}
// ディレクトリの場合
if (f.isDirectory()) {
// ディレクトリの中身を取得します
File[] childs = f.listFiles();
// ディレクトリの中身を圧縮するファイルとして圧縮メソッドを呼びます
for (File file : childs) {
comp(os, file.getPath(), basePath);
}
} else {
// ディレクトリでない場合
// 圧縮する際のファイル名を求めます
String compPath = f.getAbsolutePath();
if (compPath.indexOf(basePath) == 0) {
// 先頭がベースディレクトリから始まる場合、ベースディレクトリ+1文字をカットします
compPath = compPath.substring(basePath.length() + 1);
}
// コンソールにファイル名出力
System.out.println("compless " + compPath);
// 3.ZipArchiveEntry(Zipに入れるファイルのような物)を作成
ZipArchiveEntry zae = new ZipArchiveEntry(f, compPath);
// 4.ZipArchiveOutputStreamにエントリを入れます。
os.putArchiveEntry(zae);
// 5.エントリの中身を書き込みます。
FileInputStream is = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(is);
byte[] buff = new byte[500];
int size;
while ((size = bis.read(buff)) > 0) {
os.write(buff, 0, size);
}
bis.close();
// 6.エントリをクローズします
os.closeArchiveEntry();
}
}
}
使い方
- java -cp ..\lib\commons-compress-1.4.1.jar; ZipCompSample2 sample.zip ..\src
- こんな感じで、ZipCompSample2の後ろにzipファイル名と圧縮するファイル・ディレクトリを指定します。
- 上のコマンドは、一つ上のディレクトリの「src」ディレクトリ配下を圧縮して「smaple.zip」を作成します。
- -cpはクラスパスにCommons Compressのjarファイルを追加しています。
実行結果
C:\swing\workspace\Zip\bin>java -cp ..\lib\commons-compress-1.4.1.jar; ZipCompSample2 sample.zip ..\src
compless src\ZipCompSample.java
compless src\ZipCompSample2.java
compless src\ZipKaitouSample.java
サンプルダウンロード
コメント
最終更新:2013年07月31日 00:09