首页 > 解决方案 > 如何使用 zip4j 在 zip 密码保护的存档中隐藏带有密码的文件名

问题描述

我想用密码保护档案压缩文件。我有这样的小鬼:

    public class ZipArchiverAdapter implements ZipArchiverPort {
    private static final Logger log = LoggerFactory.getLogger(ZipArchiverAdapter.class);

    @Override
    public void compressFilesWithPassword(List<String> fileToZipPaths, String password) throws IOException {
        String sourcePath = System.getProperty("java.io.tmpdir") + "supportPackage";
        String destPath = sourcePath + ".zip";
        System.out.println("Destination " + destPath);
        ZipFile zipFile = new ZipFile(destPath, password.toCharArray());
        for(String f: fileToZipPaths) {
            zipFile.addFile(new File(f), getParameters());
        }
    }

    private ZipParameters getParameters() {
        ZipParameters zipParameters = new ZipParameters();
        zipParameters.setCompressionMethod(CompressionMethod.DEFLATE);
        zipParameters.setCompressionLevel(CompressionLevel.ULTRA);
        zipParameters.setEncryptionMethod(EncryptionMethod.AES);
        zipParameters.setAesKeyStrength(AesKeyStrength.KEY_STRENGTH_256);
        zipParameters.setEncryptFiles(true);
        zipParameters.setIncludeRootFolder(true);

        return zipParameters;
    }
}

这段代码有效。存档用密码压缩。唯一的问题是任何不知道密码的人都可以打开生成的 zip 文件并查看里面有什么文件。当您单击文件时,它将需要您提供密码。但我想用密码隐藏 zip 中的文件。这可以使用任何 zip 程序来实现。我只是不知道该怎么做zip4j。我检查了文档: http ://www.lingala.net/zip4j.html https://github.com/srikanth-lingala/zip4j 但没有找到答案。

请帮忙

标签: javazipzip4j

解决方案


来自的答案: 如何使用 zip4j 加密 zip 文件

由于专利问题,Zip4j 不支持文件列表的加密。

见:http ://www.lingala.net/zip4j/forum/index.php?topic=104.0

更新:

如链接中所述。zip 规范不包括文件列表的加密。要隐藏文件名,您可以创建一个 zip 文件,其中包括您的文件,通过再次压缩它来封装它。因此,如果您打开 zip2.zip,您只会看到“zip1.zip”而不是原始文件名。


推荐阅读