问题描述
我正在使用来压缩文件,但我需要设置密码
I'm using DotNetZip to zip my files, but I need to set a password in zip.
我尝试过:
public void Zip(string path, string outputPath)
{
using (ZipFile zip = new ZipFile())
{
zip.AddDirectory(path);
zip.Password = "password";
zip.Save(outputPath);
}
}
但是输出zip没有密码。
But the output zip not have password.
参数 path
有一个子文件夹,例如:
path = c:\路径\
和内部路径我有子文件夹
The parameter path
has a subfolder for exemple:path = c:\path\
and inside path I have subfolder
什么是
推荐答案
在密码$ c之后仅在之后添加条目$ c>属性已设置,将应用密码。为了保护您要添加的目录,只需在调用
AddDirectory
之前设置密码即可。
Only entries added after the Password
property has been set will have the password applied. To protect the directory you are adding, simply set the password before calling AddDirectory
.
using (ZipFile zip = new ZipFile())
{
zip.Password = "password";
zip.AddDirectory(path);
zip.Save(outputPath);
}
请注意,这是因为Zip文件上的密码已分配给zip文件,而不是zip文件本身。这使您可以保护一些zip文件,而其他一些则不受保护:
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. This allows you to have some of your zip file protected and some not:
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);
}
这篇关于使用DotNetZip在Zip文件上设置密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!