【问题标题】:Missing read permission for Zip files created with .net core on Linux在 Linux 上使用 .net 核心创建的 Zip 文件缺少读取权限
【发布时间】:2019-12-12 15:18:19
【问题描述】:

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");
    }
  }
}

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

【问题讨论】:

标签: c# linux permissions zip


【解决方案1】:

.net 核心现在公开了 ZipArchiveEntryExternalAttributes 属性,允许您设置权限,如下所示 (664):

entry.ExternalAttributes = entry.ExternalAttributes | (Convert.ToInt32("664", 8) << 16);

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

【讨论】:

    猜你喜欢
    • 2017-11-09
    • 2022-01-03
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2021-12-18
    • 2011-05-27
    • 2017-12-21
    • 2014-09-17
    相关资源
    最近更新 更多