0

I'm trying to password protect a zip file using DotNetZip. But it doesn't work. It creates the zip just fine, but if i open it using 7zip I can extract the files without a password. Here is the code I'm using.

        using (ZipFile zip = new ZipFile())
        {
            zip.Password = password;
            zip.Encryption = EncryptionAlgorithm.WinZipAes256;
            zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestSpeed;

            // Adding folders in the base directory
            foreach (var item in Directory.GetDirectories(someFilePath))
            {
                string folderName = new DirectoryInfo(item).Name;
                zip.AddDirectory(item, folderName);
            }

            // Adding files in the base directory
            foreach (string file in Directory.GetFiles(someFilePath))
            {
                zip.AddFile(file, "");
            }
            zip.Save(someFilePath);
        }
  • 2
    You've confirmed that you're able to not just view, but extract without the password? The docs on DotNetZip say you can see the files when password protected, just not extract without them. – gilliduck Dec 11 '18 at 15:23
  • 1
    I used your code in the question **as-is**, and the resulting ZIP file created is password-protected. Your problem lies somewhere else. Did you perhaps make an error during testing? Did you test the ZIP archive that was actually created, and not another (older test) ZIP archive by accident? Is your real code different from the question? –  Dec 11 '18 at 15:23
  • I can concur with @elgonzo, your code as written works fine. I can see the files, but I can't extract without providing a password. – gilliduck Dec 11 '18 at 15:27
  • Another possibility: Does your 7-zip installation perhaps have the password stored/cached/memorized already, making it unneccessary for it to ask for the password again? –  Dec 11 '18 at 15:27
  • Gilliduck, yes i can actually extract the files. Elgonzo, thanks for confirming that my code works. I've tried extracting with winzip, and that extracts fine too. So it's probably not a 7zip cache problem. – Shayne T. Thiessen Dec 11 '18 at 15:34

1 Answers1

1

Okay, I fixed it. I downloaded an older version of the DotNetZip dll.

I was previously using version 1.12 and it didn't work.

Using version 1.10.1 and the created zip is password protected

  • I used 1.12.0 (installed via nuget) for my tests and i had no trouble. Weird. Perhaps you used some alpha/beta/pre-release/custom build of 1.12. Who knows... Anyway, glad you found a fix... –  Dec 11 '18 at 16:03
  • Thanks for your help! – Shayne T. Thiessen Dec 11 '18 at 17:53