简体   繁体   中英

Zip files created with System.IO.Compression won't show directories on Mac

I'm creating a zip file containing the following structure:

readme.html
my-directory\\foo.txt

var zip = new ZipArchive(...);
zip.CreateEntryFromFile("c:\\readme.html", "readme.html");
zip.CreateEntryFromFile("c:\\foo.txt", "my-directory\\foo.txt");

This creates a zip file that works fine on Windows:

在此处输入图片说明

However, if I open the zip file on Mac, there is no my-directory folder. Instead, foo.txt has a name of my-directory\\foo.txt :

在此处输入图片说明

How can I add a directory to a zip file, such that the directory shows up in the zip on Mac?

For better portability, I suggest you to use the Path class in c# to form your paths, like this:

string root = @"c:\"; // different mount point if in mac

zip.CreateEntryFromFile(Path.Combine(root, "readme.html"), "readme.html");
zip.CreateEntryFromFile(Path.Combine(root, "my-directory", "foo.txt"), Path.Combine("my-directory", "foo.txt"));

Turns out, I had forgotten that directory separators are different between Windows and Mac. 😳

The fix was creating directories by using the "/" character, which works on both Windows and Mac:

zip.CreateEntryFromFile("c:\\foo.txt", "my-directory/foo.txt");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM