ZipFile.CreateFromDirectory 不包括目录条目 [英] ZipFile.CreateFromDirectory does not include directory entries

查看:30
本文介绍了ZipFile.CreateFromDirectory 不包括目录条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的 Windows 10 UWP 应用,并使用以下代码从目录结构创建一个 .zip 文件:

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

我注意到生成的 .zip 文件没有嵌套目录的任何条目,因此无法在 Mac 上解压缩.

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

这是我想包含在 zip 文件中的目录结构:

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

./ziproot
    ./Data
        Version.txt

我使用 API ZipFile.CreateFromDirectory(...) 得到了什么:

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

当我使用 Windows 资源管理器压缩测试文件夹时,我得到了正确的 zip 结构:

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

注意上面输出中 Data 目录的第一个条目.

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

有没有办法让 ZipFile API 包含目录条目,而不必自己遍历目录?

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

推荐答案

为了将来参考,问题是别的:

For future reference, the problem was something else:

ZipFile.CreateFromDirectory() 在路径名中使用反斜杠.使用 Windows 文件资源管理器创建的档案使用正斜杠(这也是我需要的).

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

我没有看到改变 ZipFile 的默认行为的方法,而是使用 ZipArchive 自己构建 zip 文件.通过这样做,我什至不需要包含目录条目.

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.

顺便说一下,这些是通过在路径末尾附加一个正斜杠来添加的(例如 foo/bar/).

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

这篇关于ZipFile.CreateFromDirectory 不包括目录条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆