Search code examples
c#passwordszipcompressiondotnetzip

password option is not working in Zip file


Am creating a Password enabled zip file using DotNetZip Library following is the code for this:

using (ZipFile zip = new ZipFile())
{
    string[] Files = Directory.GetFiles(cryptPath, "*.*");
    foreach (string f in Files)
    {
        zip.AddFile(f);                       
    }                   
    zip.Password = "mypassord";
    zip.Save(cryptPath + @"\output.zip");
}

Everything works fine except the password option is not working, no password is prompted while opening the file? how can i enable this?


Solution

  • It seems to be using the password for encryption when you add the files so setting the password before adding the files worked for me:

    using (ZipFile zip = new ZipFile())
    {
        zip.Password = "mypassword";
    
        string[] Files = Directory.GetFiles(cryptPath, "*.*");
        foreach (string f in Files)
        {
            zip.AddFile(f);                       
        }                   
    
        zip.Save(cryptPath + @"\output.zip");
    }