我需要創建一個 zip 文件。它應該受到密碼保護。我正在使用林加拉罐子。這是我的下面。有辦法做到嗎?我什至嘗試過 zipoutstream,但找不到添加密碼的方法。@Componentpublic class FileZipUtils { @Value("${candela.email.zip.folder}") private String zipBaseDir; @Value("${candela.email.zip.encryptionmethod:AES}") private String encryptionMethod; @Value("${candela.email.zip.encryptionstrength:KEY_STRENGTH_128}") private String encryptionStrength; private ZipParameters zipParameters; @PostConstruct private void initializeZipProperties() { zipParameters = new ZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_128); } /* * Creates a zipfile in the zipBaseDir location */ public ZipFile createZipFile(String zipFileName,char[] password) { return new ZipFile(zipBaseDir + "/" + zipFileName,password); } /** * Adds attachment to Zip file */ public void addAttachementToZip(ZipFile zipFile, ByteArrayResource fileContentInBytes, String fileName) throws IOException { zipParameters.setFileNameInZip(fileName); zipFile.addStream(fileContentInBytes.getInputStream(), zipParameters); }}
2 回答

www說
TA貢獻1775條經驗 獲得超8個贊
zip 文件 lib 的最佳解決方案zip4j
。
特征:
創建、添加、提取、更新、從 Zip 文件中刪除文件
支持流(ZipInputStream 和 ZipOutputStream)
讀/寫受密碼保護的 Zip 文件和流
支持 AES 和 Zip-Standard 加密方法
支持 Zip64 格式
Store(無壓縮)和 Deflate 壓縮方法
從分割 Zip 文件創建或提取文件(例如:z01、z02、...zip)
支持 zip 中的 Unicode 文件名和注釋
進度監視器 - 用于集成到應用程序和面向用戶的應用程序
添加回答
舉報
0/150
提交
取消