如何使用zip4j在zip密码保护的存档中使用密码隐藏文件名

huangapple go评论72阅读模式
英文:

How to hide file names with password inside zip password protected archive using zip4j

问题

Sure, here is the translated code segment:

我想要用带密码保护的压缩档案来压缩文件我有以下实现

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;
    }
}

Please let me know if you need further assistance.

英文:

I wanna zip files with password protected archive. I have such imp:

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

    @Override
    public void compressFilesWithPassword(List&lt;String&gt; fileToZipPaths, String password) throws IOException {
        String sourcePath = System.getProperty(&quot;java.io.tmpdir&quot;) + &quot;supportPackage&quot;;
        String destPath = sourcePath + &quot;.zip&quot;;
        System.out.println(&quot;Destination &quot; + 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;
    }
}

And this code works. Archive is zipped with password.
The only problem with it is that anyone without knowing a password can open resulting zip file and see what files are inside. When You clicks on file it will require password from You.
But I wanna have files inside zip hidden with password. This can be achieved using any zip program.
I just don't know how to do this with zip4j. I've check docs:
http://www.lingala.net/zip4j.html
https://github.com/srikanth-lingala/zip4j
but not find an answer.

Please help

答案1

得分: 2

来自:
https://stackoverflow.com/questions/15085249/how-to-encrypt-zip-file-using-zip4j

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

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

更新:

如链接中所述,zip规范不包括对文件列表的加密。为了隐藏文件名,您可以创建一个包含您的文件的zip文件,然后再次对其进行压缩。因此,如果您打开zip2.zip,您只会看到"zip1.zip",而不会看到原始文件名。

英文:

Answer from:
https://stackoverflow.com/questions/15085249/how-to-encrypt-zip-file-using-zip4j

> Zip4j does not support the encryption of the file list because of
> patent issues.
>
> See: http://www.lingala.net/zip4j/forum/index.php?topic=104.0
>
> Update:
>
> As stated in the link. The zip-specification does not include the
> encryption of the filelist. To hide the filenames you can create a
> zip-file including your files encapsulate it by zip it again. Hence if
> you open zip2.zip you will only see "zip1.zip" and not the original
> filenames.

huangapple
  • 本文由 发表于 2020年8月17日 19:10:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63449658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定