Jea*_*los 18 c# zip password-protection dotnetzip
我正在使用DotNetZip来压缩我的文件,但我需要在zip中设置密码.
我试过:
public void Zip(string path, string outputPath)
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(path);
zip.Password = "password";
zip.Save(outputPath);
}
}
Run Code Online (Sandbox Code Playgroud)
但输出zip没有密码.
该参数path有一个例子的子文件夹:
path = c:\path\
我有内部路径subfolder
怎么了?
pet*_*ids 31
只有在设置Password属性后添加的条目才会应用密码.要保护要添加的目录,只需在调用之前设置密码即可AddDirectory.
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
zip.AddDirectory(path);
zip.Save(outputPath);
}
Run Code Online (Sandbox Code Playgroud)
请注意,这是因为Zip文件上的密码分配给zip文件中的条目,而不是zip文件本身.这允许您保护您的一些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);
}
Run Code Online (Sandbox Code Playgroud)