DotNetZip密码不起作用或不正确。

huangapple go评论69阅读模式
英文:

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.

DotNetZip密码不起作用或不正确。

Uploaded to azure.

DotNetZip密码不起作用或不正确。

using code able to encrypt the files.

DotNetZip密码不起作用或不正确。

DotNetZip密码不起作用或不正确。

After download prompting for the password to open the file.
DotNetZip密码不起作用或不正确。

huangapple
  • 本文由 发表于 2023年5月10日 21:01:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76218736.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定