Remove password protection from ZIP without extracting the files

Hello,

is there a way to remove a password protection from ZIP file without extracting them (password is knowing)?

We using the following code to encrypt a ZIP with a password:

                zipFileStream = File.Open(targetZipFile, FileMode.Create, FileAccess.ReadWrite,
                    FileShare.ReadWrite);

                EncryptionSettings encryptionSettings = null;
                if (!string.IsNullOrWhiteSpace(password))
                    encryptionSettings = new AesEcryptionSettings(password, encryptionMethod);

                archive = new Archive(new ArchiveEntrySettings(encryptionSettings: encryptionSettings));
                foreach (var inputFile in inputFiles)
                {
                    archive.CreateEntry(Path.GetFileName(inputFile), new FileInfo(inputFile), true);
                }
                
                archive.Save(zipFileStream);

Kind regards,
Andy

Hello @AStelzner,
No, it is impossible, but you can proceed without saving decrypted files to disk. Here is sample code:

using (var decrypted = new Archive())
{
    using (var encrypted = new Archive("encrypted.zip", new ArchiveLoadOptions(){DecryptionPassword = "p@s$"}))
    {
         foreach (var encryptedEntry in encrypted.Entries)
         {
             MemoryStream ms = new MemoryStream();
             decrypted.CreateEntry(encryptedEntry.Name, ms);
             encryptedEntry.Open().CopyTo(ms);
         }
     }            
     decrypted.Save("decrypted.zip");
}

Please also note that Archive is IDisposable and should be disposed - in your code it is not within using block.

1 Like