在 Linux 上使用 .net core 创建的 Zip 文件缺少读取权限

Phi*_*hil 5 c# linux permissions zip

DotNetZip 创建权限为 000(不可读取、不可写入、不可执行)的 zip 文件,因此我无法在 Linux 上轻松打开它们(Windows 资源管理器不关心它并正常打开文件)。Windows 上的相同代码生成具有读取权限的文件(在 Linux 上):

using (var fs = new System.IO.FileStream("./test.zip"))
{
  using (var archive = new System.IO.Compression.ZipArchive(fs, ZipArchiveMode.Create))
  {
    var entry = archive.CreateEntry("test.txt");
    using (var writer = new System.IO.StreamWriter(entry.Open()))
    {
      writer.Write("Hello World");
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我可以设置权限或模拟System.IO.Compression在 Windows 上运行的程序吗?

Mar*_*tke 7

.net core 现在公开了允许您设置权限ExternalAttributes的属性,如下所示(664):ZipArchiveEntry

entry.ExternalAttributes = entry.ExternalAttributes | (Convert.ToInt32("664", 8) << 16);
Run Code Online (Sandbox Code Playgroud)

请注意,该字符串是权限位掩码的八进制表示形式,因此转换为基数 8。