Search code examples
dotnetzip

Why is my zip folder not locked with password, but the files inside are locked?


I have been struggling for a few hours now, trying to set a password on a zip folder. However, however many times I tried with different code, the password is applied to each file inside the zip folder, and that's not what I want. I only want to apply the password to the folder itself, and no password should be set to the individual files inside.

Here is my code: (For your information, I am using DotNetZip)

//Assume that there is a folder with multiple files in it at C:\\ExampleFolder
using (Ionic.Zip.ZipFile z = Ionic.Zip.ZipFile())
{
    z.Password = "MyPassword";            //Setting the password
    z.AddDirectory(@"C:\\ExampleFolder"); //I thought the directory added here should be pw-protected
    z.Save(@"C:\\FinalResult.zip");       //Create the pw-protected zip folder
}

However, when I run this program, it does create a zip folder named FinalResult.zip, but that zip folder is not password-protected. All the files inside that zip folder are password-protected. If this is the case, the user will have to enter the password every time they try to open a file inside, which is kind of inconvenient. I just want the user to have to enter the password only once when they try to open the zip folder. Can anyone tell me why this code doesn't do what I want to achieve?


Solution

  • The password is actually working correctly:

    When writing a zip archive, this password is applied to the entries, not to the zip archive itself. It applies to any ZipEntry subsequently added to the ZipFile, using one of the AddFile, AddDirectory, AddEntry, or AddItem methods, etc. When reading a zip archive, this property applies to any entry subsequently extracted from the ZipFile using one of the Extract methods on the ZipFile class.

    See the remarks section in the documentation for Password in DotNetZip

    https://documentation.help/DotNetZip/4444d7a5-3324-8af9-3ed3-5bf6551d3cd1.htm