Setting a password in a Zip file using DotNetZip

I use DotNetZip for zip files, but I need to set a password in zip.

I tried:

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

But there is no password at the zip output.

The path parameter has a subfolder, for example: path = c:\path\ and inside the path I have a subfolder

What's wrong?

+12
source share
1 answer

Only entries added after setting the Password property will be applied to the password. To protect the directory that you are adding, simply set a password before calling AddDirectory .

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

Please note that this is due to the fact that passwords in Zip files are placed in the records inside the Zip file, and not in the Zip files themselves. This allows you to protect some of your zip files 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); } 
+19
source

All Articles