CentOSサーバー上で、JAVAを使って開発をしています。
DBから顧客情報を抽出し、CSVへ出力する機能があるのですが、
個人情報の保護に、少しでも役に立てばと考え、
CSVは、パスワード付きZIPの形で、
極特定ユーザー端末がダウンロードできるようにします。
そこで、サーバー側で、DB⇒CSV⇒パスワード付きZIPにしたいのですが、
下記標準?ライブラリを使うと、パスワードなしZIPができることは、
わかりました。
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;
パスワード付きZIPを作るには、どのようにすればよいでしょうか?
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
回答1件
0
ベストアンサー
zip4jを使ったら、すぐにできました。
他にもライブラリがあるようですが、これが一番ソースが短くシンプルのようです。
ダウンロード先
http://www.lingala.net/zip4j/download.php
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
package ごにょごにょ;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import java.io.File;
import java.io.IOException;
public class ZIP_Packege {
private int compressionMethod = Zip4jConstants.COMP_DEFLATE; private int compressionLevel = Zip4jConstants.DEFLATE_LEVEL_NORMAL; private int encryptionMethod = Zip4jConstants.ENC_METHOD_STANDARD; private int aesKeyStrength = Zip4jConstants.AES_STRENGTH_256; public ZIP_Packege(){ } public ZIP_Packege(int encryptionMethod, int aesKeyStrength) { this.encryptionMethod = encryptionMethod; this.aesKeyStrength = aesKeyStrength; } public void zip(String input, String output, String password) throws ZipException, IOException { zip(input, output, password, ""); } public void zip(String input, String output, String password, String fileNameCharset) throws ZipException, IOException { ZipFile zipFile = new ZipFile(output); // If the fileNameCharset is empty then charset is detected automatically // Try with Cp850 and UTF8 or OS default if (fileNameCharset.length() <= 0) { zipFile.setFileNameCharset(fileNameCharset); } ZipParameters parameters = new ZipParameters(); parameters.setCompressionMethod(compressionMethod); parameters.setCompressionLevel(compressionLevel); parameters.setEncryptFiles(true); parameters.setEncryptionMethod(encryptionMethod); parameters.setAesKeyStrength(aesKeyStrength); parameters.setPassword(password); File inputFile = new File(input); if (inputFile.isDirectory()) { zipFile.createZipFileFromFolder(inputFile, parameters, false, 0); } else { zipFile.addFile(inputFile, parameters); } } public void unzip(String input, String output, String password) throws ZipException { ZipFile zipFile = new ZipFile(input); if (zipFile.isEncrypted()) { zipFile.setPassword(password); } zipFile.extractAll(output); }
}
ーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーーー
投稿2015/08/08 01:45
退会済みユーザー
総合スコア0
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/10/03 01:09