【问题标题】:Setting a password for a zip file为 zip 文件设置密码
【发布时间】:2018-08-27 08:09:18
【问题描述】:

我正在尝试使用带有 .Net Core 的 SharpZipLib 库为 zip 文件设置密码。

我已按照this 示例设置密码,但是一旦创建了 zip 文件,文件就在其中并创建了 zip 文件,但是没有密码。

// Create a password for the Zipfolder
// https://github.com/icsharpcode/SharpZipLib/wiki/Zip-Samples
using (ICSharpCode.SharpZipLib.Zip.ZipFile ZipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(Path.GetFileName(destinationPath)))
{
     ZipFile.Password = "foo";
     ZipFile.Add(destinationPath, "");
}

【问题讨论】:

    标签: .net-core zipfile


    【解决方案1】:

    以上答案都不适合我,
    话虽如此,我在 SharpZipLib 库中找到了对我有用的 this Fast Zip 类。

    // Create a password for the Zipfolder
    // https://github.com/icsharpcode/SharpZipLib/wiki
    ICSharpCode.SharpZipLib.Zip.FastZip zipFile = new ICSharpCode.SharpZipLib.Zip.FastZip();
    zipFile.Password = "foo";
    zipFile.CreateEmptyDirectories = true;
    zipFile.CreateZip(destinationPath,tempPath, true, "");
    

    我唯一不喜欢的是它没有实现IDisposable

    【讨论】:

      【解决方案2】:

      我使用了 wiki 中的示例,它没有问题。

      代码:

      using (FileStream fsOut = File.Create(@"d:\temp\sharplib_pwtest.zip"))
      using (ZipOutputStream zipStream = new ZipOutputStream(fsOut)) {
          zipStream.SetLevel(3);
          zipStream.Password = "Testpassword";
          var folderName = @"D:\temp\sharpZipLibTest\";
          int folderOffset = folderName.Length + (folderName.EndsWith("\\") ? 0 : 1);
          CompressFolder(folderName, zipStream, folderOffset);
      }
      

      【讨论】:

      • 试一试,仍然没有密码。
      • 您是否尝试解压 zip 文件?当你打开 zip 文件时他不会要求它,所以文件名是可读的
      • @Lord_Pinhead zip 文件列表始终可见,即使它们受到密码保护。
      【解决方案3】:

      您只需输入BeginUpdate()CommitUpdate(),这将反映在您的输出中。

      using (ICSharpCode.SharpZipLib.Zip.ZipFile ZipFile = new ICSharpCode.SharpZipLib.Zip.ZipFile(Path.GetFileName(destinationPath)))
      {
          ZipFile.BeginUpdate();
          ZipFile.Password = "foo";
          ZipFile.Add(destinationPath, "");
          ZipFile.CommitUpdate();
      }
      

      【讨论】:

      • 你测试过这个是吗?似乎没有密码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-01
      • 2013-06-24
      相关资源
      最近更新 更多