枚举特定文件夹 DotNetZip 的内容,没有子文件夹

Enumerate contents of specific folder DotNetZip, without child folders

使用 Ionic.Zip

我想显示特定文件夹中的文件或文件夹。我正在使用 SelectEntries 方法,但不幸的是它过滤掉了文件夹。不是我所期望的使用“*”。

ICollection<ZipEntry> selectEntries = _zipFile.SelectEntries("*",rootLocation)

如果我采用替代方法:

IEnumerable<ZipEntry> selectEntries = _zipFile.Entries.Where(e => e.FileName.StartsWith(rootLocation))

我面临两个问题:

  1. 我可能必须将“/”切换为“\”。
  2. 我得到了所有的子文件夹。

这是不可取的。

有人知道为什么 SelectEntries returns 没有文件夹,还是我误用了它?

我在我的特殊情况下找到了解决方案。我认为 Zipfile 的构建方式导致它看起来有文件夹,但 none 实际上存在,即以下代码产生了一个空列表。

_zipFile.Entries.Where(e=>e.IsDirectory).AsList();  // always empty!

我使用了以下代码片段来实现我所需要的。正则表达式没有应有的全面,但适用于我需要的所有情况。

var conformedRootLocation = rootLocation.Replace('\','/').TrimEnd('/') + "/";
var pattern = string.Format(@"({0})([a-z|A-Z|.|_|0-9|\s]+)/?", conformedRootLocation);
var regex = new Regex(pattern);
return _zipFile.EntryFileNames.Select(e => regex.Match(e))
        .Where(match => match.Success)
        .Select(match => match.Groups[2].Value)
        .Distinct()
        .Select(f => new DirectoryResource
        {
            Name = f, IsDirectory = !Path.HasExtension(f)
        })
        .ToList();