ZipFile.CreateFromDirectory does not include direc

2019-08-14 03:34发布

问题:

I have a basic Windows 10 UWP app and use the following code to create a .zip file from a directory structure:

ZipFile.CreateFromDirectory("/inputpath", "/output.zip");

I noticed that the resulting .zip file does not have any entries for nested directories, thus unzipping on a Mac does not work.

Here is the directory structure that I want to include in the zip file:

./ziproot
    ./Data
        Version.txt

What I get using the API ZipFile.CreateFromDirectory(...):

$ zipinfo output.zip 
Archive:  output.zip   279202 bytes   4 files
-rw----     2.0 fat        1 b- defN 13-May-16 11:33 Data\Version.txt

When I use the Windows explorer to compress the test folder, I get the correct zip structure:

$ zipinfo zipwindows.zip 
Archive:  zipwindows.zip   279188 bytes   8 files
drwx---     2.0 fat        0 b- stor 18-May-16 09:05 Data/
-rw----     2.0 fat        1 t- stor 13-May-16 11:33 Data/Version.txt

Note the first entry for the Data directory in the output above.

Is there a way to have the ZipFile API include entries for directories without having to traverse the directory myself?

回答1:

For future reference, the problem was something else:

ZipFile.CreateFromDirectory() uses a backslash in path names. Archives made with the Windows file explorer use a forward slash (that's what I needed, too).

I don't see a way to change this default behavior of ZipFile and instead used ZipArchive to build the zip file myself. By doing this, I didn't even need to include entries for directories.

Those are added by appending a forward slash to the end of the path, by the way (e.g. foo/bar/).



回答2:

Good day, does this work for you?

ZipFile.CreateFromDirectory("/inputpath", "/Output.zip", CompressionLevel.Optimal, true);


标签: c# zip uwp unzip