■ サンプル
import java.io.File; import net.lingala.zip4j.core.ZipFile; import net.lingala.zip4j.exception.ZipException; import net.lingala.zip4j.model.ZipParameters; import net.lingala.zip4j.util.Zip4jConstants; public class ZipDemo { public static void main(String[] args) { String source = "ZIPファイル.zip"; String destination = "etc"; String password = "password1"; System.out.println("Done " + zip(source, destination, password)); } public static boolean zip(String outFile, String input, String password) { try { ZipFile zipFile = new ZipFile(outFile); zipFile.setFileNameCharset("UTF-8"); ZipParameters parameters = new ZipParameters(); // 圧縮率 parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE); parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL); // 暗号化 parameters.setEncryptFiles(true); parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_STANDARD); parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256); parameters.setPassword(password); File inputFile = new File(input); if (inputFile.isDirectory()) { zipFile.createZipFileFromFolder(inputFile, parameters, false, 0); } else { zipFile.addFile(inputFile, parameters); } return true; } catch (ZipException ex) { ex.printStackTrace(); return false; } } }
参考文献
https://qiita.com/ysksuzuki/items/d17efb465b3f16836c8ahttp://hiro2tdn.hatenablog.com/entry/2015/06/21/163830