开发者社区> 问答> 正文

在Java中创建受密码保护的zip文件,而无需在磁盘上创建

我需要创建一个zip文件。可以密码保护。我正在使用 lingala jar。这是我的代码。有办法吗?我甚至尝试了zipoutstream,找不到添加密码的方法。

@Component public 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);
}

}

展开
收起
小六码奴 2019-10-03 19:33:36 852 0
1 条回答
写回答
取消 提交回答
  • 创建,添加,提取,更新,从Zip文件中删除文件 支持流(ZipInputStream和ZipOutputStream) 读/写受密码保护的Zip文件和流 支持AES和Zip-Standard加密方法 支持Zip64格式 存储(无压缩)和放气压缩方法 从Split Zip文件创建或提取文件(例如:z01,z02,... zip) 支持Unicode中的Unicode文件名和注释 进度监视器-用于集成到应用程序和面向用户的应用程序中

    2019-10-09 16:24:37
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Spring Cloud Alibaba - 重新定义 Java Cloud-Native 立即下载
The Reactive Cloud Native Arch 立即下载
JAVA开发手册1.5.0 立即下载