简体   繁体   English

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

[英]Create password protected zip file in Java without creating it on disk

I need a create zip file.我需要一个创建 zip 文件。 It should be password protected.它应该受密码保护。 I am using lingala jar.我正在使用 lingala jar。 Here is my below.这是我的下面。 Is there a way to do it?有没有办法做到这一点? I even tried zipoutstream, couldn't find a way to add password.我什至尝试了 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);
    }

}

The best solution for zip files zip4j lib. zip 文件zip4j lib 的最佳解决方案。 ( Github Link ) Github 链接

Features:特征:

  • Create, Add, Extract, Update, Remove files from a Zip file从 Zip 文件中创建、添加、提取、更新、删除文件
  • Support for streams (ZipInputStream and ZipOutputStream)支持流(ZipInputStream 和 ZipOutputStream)
  • Read/Write password protected Zip files and streams读/写受密码保护的 Zip 文件和流
  • Support for both AES and Zip-Standard encryption methods支持 AES 和 Zip-Standard 加密方法
  • Support for Zip64 format支持 Zip64 格式
  • Store (No Compression) and Deflate compression method存储(无压缩)和 Deflate 压缩方法
  • Create or extract files from Split Zip files (Ex: z01, z02,...zip)从拆分 Zip 文件(例如:z01、z02、...zip)创建或提取文件
  • Support for Unicode file names and comments in zip支持 zip 中的 Unicode 文件名和注释
  • Progress Monitor - for integration into apps and user facing applications进度监视器 - 用于集成到应用程序和面向用户的应用程序中

I think we need to create file on disk.我认为我们需要在磁盘上创建文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM