5

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?

Phil
  • 135
  • 9
  • Does this answer your question? [DotNetZip: creating zip with C# permissions issue](https://stackoverflow.com/questions/606908/dotnetzip-creating-zip-with-c-sharp-permissions-issue) – panoskarajohn Dec 12 '19 at 15:20
  • Try using this to change the file permissions after the file is created https://stackoverflow.com/questions/45132081/file-permissions-on-linux-unix-with-net-core – Ryan Gaudion Dec 12 '19 at 15:21

1 Answers1

7

.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.

Marc Wittke
  • 2,991
  • 2
  • 30
  • 45