Phil
Phil

Reputation: 145

Missing read permission for Zip files created with .net core on Linux

DotNetZip creates zip files with permission 000 (no read, no write, no execute) and thus I cannot easily open them on Linux (Windows Explorer doesn't care about it and opens the file normally). Same code on Windows produces files with read permission (on 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");
    }
  }
}

Can I either set the permissions or emulating that System.IO.Compression is running on Windows?

Upvotes: 5

Views: 1705

Answers (1)

Marc Wittke
Marc Wittke

Reputation: 3155

.net core now exposes the ExternalAttributes property of ZipArchiveEntry that allows you to set permissions, like this (664):

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

note that the string is an octal representation of the permission bit mask, therefore the convert to base 8.

Upvotes: 7

Related Questions