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

在Java中创建受密码保护的zip文件,而不在磁盘上创建,java,zip,Java,Zip,我需要一个创建zip文件。它应该有密码保护。我用的是lingala罐子。下面是我的名字。有办法吗?我甚至试过zipoutstream,找不到添加密码的方法 @Component public class FileZipUtils { @Value("${candela.email.zip.folder}") private String zipBaseDir; @Value("${candela.email.zip.encryptionmethod:AES}")

我需要一个创建zip文件。它应该有密码保护。我用的是lingala罐子。下面是我的名字。有办法吗?我甚至试过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);
    }

}

zip文件的最佳解决方案
zip4j
lib。 ()

特点:

  • 从Zip文件中创建、添加、提取、更新和删除文件
  • 支持流(ZipInputStream和ZipOutStream)
  • 读/写受密码保护的Zip文件和流
  • 支持AES和Zip标准加密方法
  • 支持Zip64格式
  • 存储(无压缩)和放气压缩方法
  • 从拆分的Zip文件中创建或提取文件(例如:z01、z02、…Zip)
  • 在zip中支持Unicode文件名和注释
  • 进度监视器-用于集成到应用程序和面向用户的应用程序中

我认为我们需要在磁盘上创建文件。

这正是我所使用的。