DotNetZip Passwords Not Working or Incorrect

186 Views Asked by At

I have an ADF that generates files and then Zips them to a container. After that's completed, I call an Azure Function that reads the Zip file and adds passwords. From the documentation, the password is applied to the individual entries in the archive. I iterate through the entries in the Azure Function and add the passwords and then upload that zip file back to the same container OVERWRITING the orginal one. However, when I unzip the file (using 7zip) the passwords I added doesn't work and says it's wrong.

Here's the block of code:

string zipName = objParam.ZipFileName;
                Logger.LogInformation("Starting Password Protect of Zip File");
                var blobClient = BlobServiceClient.GetBlobContainerClient(objParam.DestinationContainer).GetBlobClient(zipName);

                Logger.LogInformation("Zip File Name: " + objParam.ZipFileName)  
using (Stream zipStream = await blobClient.OpenReadAsync().ConfigureAwait(false))
                {
                    ZipFile zipFile = ZipFile.Read(zipStream);    
                    zipFile.Encryption = EncryptionAlgorithm.None;
                    zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

                    foreach (var entry in zipFile)
                    {                        
                        entry.Password = "Test123";
                    }

                    var zipOutputStream = new MemoryStream();
                    zipFile.Save(zipOutputStream);
                    zipOutputStream.Seek(0, SeekOrigin.Begin);
                    zipStream.Close();
                    await blobClient.UploadAsync(zipOutputStream, true);
                    zipOutputStream.Close();
                }            

Password protect a zip file using DotNetZip library.

1

There are 1 best solutions below

3
Rajesh  Mopati On

I modified the code a bit and it worked for me.

     string zipName = "Files.zip";
     log.LogInformation("Starting Password Protect of Zip File");

      BlobServiceClient blob_SrvcClnt = new BlobServiceClient("Connection_String");
      BlobContainerClient cntrClnt = blob_SrvcClnt.GetBlobContainerClient("mycntr444");
      BlobClient blbClient = cntrClnt.GetBlobClient(zipName);

      log.LogInformation("Zip File Name: " + zipName);
       using (Stream zipStream = await blbClient.OpenReadAsync().ConfigureAwait(false))
         {
                ZipFile zipFile = ZipFile.Read(zipStream);
                zipFile.Encryption = EncryptionAlgorithm.None;
                zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

                foreach (var entry in zipFile)
                {
                    entry.Password = "Test123";
                }

                var zipOutputStream = new MemoryStream();
                zipFile.Save(zipOutputStream);
                zipOutputStream.Seek(0, SeekOrigin.Begin);
                zipStream.Close();
                await blbClient.UploadAsync(zipOutputStream, true);
                zipOutputStream.Close();
        }
       log.LogInformation("Password Protect of Zip Files is successfully done.");

And installed the below NuGets.

enter image description here

Uploaded to azure.

enter image description here

using code able to encrypt the files.

enter image description here

enter image description here

After download prompting for the password to open the file. enter image description here