简体   繁体   English

使用DotNetZip在Zip文件上设置密码

[英]Set password on Zip file using DotNetZip

I'm using DotNetZip to zip my files, but I need to set a password in zip. 我正在使用DotNetZip来压缩我的文件,但我需要在zip中设置密码。

I tryed: 我试过:

public void Zip(string path, string outputPath)
    {
        using (ZipFile zip = new ZipFile())
        {
            zip.AddDirectory(path);
            zip.Password = "password";
            zip.Save(outputPath);
        }
    }

But the output zip not have password. 但输出zip没有密码。

The parameter path has a subfolder for exemple: path = c:\\path\\ and inside path I have subfolder 参数path有一个例子的子文件夹: path = c:\\path\\和内部路径我有subfolder

What is wrong? 怎么了?

Only entries added after the Password property has been set will have the password applied. 只有在设置了Password属性添加的条目才会应用密码。 To protect the directory you are adding, simply set the password before calling AddDirectory . 要保护要添加的目录,只需在调用AddDirectory之前设置密码AddDirectory

using (ZipFile zip = new ZipFile())
{
    zip.Password = "password";
    zip.AddDirectory(path);
    zip.Save(outputPath);
}

Note that this is because passwords on Zip files are allocated to the entries within the zip file and not on the zip file themselves. 请注意,这是因为Zip文件上的密码分配给zip文件中的条目,而不是zip文件本身。 This allows you to have some of your zip file protected and some not: 这允许您保护您的一些zip文件,而不是:

using (ZipFile zip = new ZipFile())
{
    //this won't be password protected
    zip.AddDirectory(unprotectedPath);
    zip.Password = "password";
    //...but this will be password protected
    zip.AddDirectory(path);
    zip.Save(outputPath);
}

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

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