Hutool或JDK的Zip工具并不支持添加密码,可以考虑使用Zip4j完成,以下代码来自Zip4j官网。
ZipParameterszipParameters=newZipParameters(); zipParameters.setEncryptFiles(true); zipParameters.setEncryptionMethod(EncryptionMethod.AES); // Below line is optional. AES 256 is used by default. You can override it to use AES 128. AES 192 is supported only for extracting.zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256); List<File>filesToAdd=Arrays.asList( newFile("somefile"), newFile("someotherfile") ); ZipFilezipFile=newZipFile("filename.zip", "password".toCharArray()); zipFile.addFiles(filesToAdd, zipParameters);