Created
March 28, 2013 00:19
-
-
Save seraphy/5259419 to your computer and use it in GitHub Desktop.
Zip4Jを使って日本語ファイル名とWindowsで使えるZIP暗号化のパスワードの設定例.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package zip4jtest; | |
import java.io.File; | |
import java.io.FileOutputStream; | |
import net.lingala.zip4j.io.ZipOutputStream; | |
import net.lingala.zip4j.model.ZipModel; | |
import net.lingala.zip4j.model.ZipParameters; | |
import net.lingala.zip4j.util.Zip4jConstants; | |
/** | |
* Zip4Jを使って日本語ファイル名とWindowsで使えるZIP暗号化のパスワードの設定例.<br> | |
* Zip4Jは、以下より入手する.<br> | |
* http://www.lingala.net/zip4j/ | |
* @author seraphy | |
*/ | |
public class Zip4jTest { | |
/** | |
* メインエントリ | |
* @param args the command line arguments | |
*/ | |
public static void main(String[] args) throws Exception { | |
// Zipの特性 | |
ZipModel zipModel = new ZipModel(); | |
zipModel.setFileNameCharset("csWindows31J"); // ファイル名の文字コード(日本語) | |
// ZIPエントリの設定 | |
ZipParameters zipParam = new ZipParameters(); | |
// 圧縮率 | |
zipParam.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); | |
zipParam.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); | |
// 暗号化 | |
zipParam.setEncryptFiles(true); | |
zipParam.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); // 標準の暗号化 | |
zipParam.setPassword("password123"); // パスワード | |
try (FileOutputStream fos = new FileOutputStream("c:\\temp\\test.zip")) { | |
try (ZipOutputStream zos = new ZipOutputStream(fos, zipModel)) { | |
for (int idx = 0; idx < 10; idx++) { | |
String fileName = String.format("日本語フォルダ/日本語ファイル名%02d.txt", idx); | |
zipParam.setSourceExternalStream(true); // ファイルを使わない | |
zipParam.setFileNameInZip(fileName); // 格納するファイル名 | |
// ファイルエントリ作成 | |
zos.putNextEntry(null, zipParam); | |
// ファイルの中身の出力 | |
String text = String.format("こんにちは, 世界! No.%03d", idx); | |
zos.write(text.getBytes("csWindows31J")); | |
zos.closeEntry(); | |
} | |
// Zipを閉じる | |
zos.finish(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment