英文:
DotNetZip Passwords Not Working or Incorrect
问题
我有一个ADF生成文件,然后将它们压缩到一个容器中。完成后,我调用一个Azure函数读取Zip文件并添加密码。根据文档,密码应用于存档中的各个条目。我在Azure函数中遍历条目并添加密码,然后将该zip文件上传回相同的容器,覆盖原始文件。然而,当我解压文件(使用7zip)时,我添加的密码不起作用,显示密码错误。
以下是代码块:
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();
}
使用DotNetZip库对Zip文件进行密码保护。
英文:
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
得分: 1
我稍微修改了代码,然后它对我起作用了。
     string zipName = "Files.zip";
     log.LogInformation("开始对 Zip 文件进行密码保护");
      BlobServiceClient blob_SrvcClnt = new BlobServiceClient("Connection_String");
      BlobContainerClient cntrClnt = blob_SrvcClnt.GetBlobContainerClient("mycntr444");
      BlobClient blbClient = cntrClnt.GetBlobClient(zipName);
      log.LogInformation("Zip 文件名: " + 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("成功完成对 Zip 文件的密码保护");
英文:
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.

Uploaded to azure.

using code able to encrypt the files.


After download prompting for the password to open the file.

通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论