我正在使用System.IO.Compression来提取一些Zip文件的内容 . 问题是,只要存在包含某些Windows非法字符的文件名的条目,就会抛出异常 . 我尝试了几件事,但我仍然没有找到任何方法来忽略不良条目并提取那些好的条目 . 请注意,修改zip文件的内容不是我们正在执行的处理类型的可能性,因此我必须按原样处理该文件 .

系统通常处理具有多个条目的文件,这个数字是可变的,但在一个zip文件中最多可以有300个条目,偶尔会有一个带有文件名的条目,例如 'myfile<name>.txt' ,其中包含明显非法字符的尖括号对于Windows . 我真的想忽略这个条目,然后继续提取ZipArchive中的其余条目 . 但看起来这是不可能的 .

关于如何忽视ZipArchive的错误条目的任何想法?

到目前为止,我已经尝试了不同的东西来分别获取条目,但我总是得到完全相同的异常错误 .

以下是我迄今为止尝试过的一些事情:

  • 实现迭代条目的常规方法:
foreach (ZipArchiveEntry entry in ZipArchive.Entries)
  • 尝试通过索引只获取一个条目(即使第一个条目是有效条目,此处也是相同的异常):
ZipArchiveEntry entry = ZipArchive.Entries[0]
  • 使用lambda表达式应用过滤器来忽略无效的条目(同样的例外):
var entries = zipArchive.Entries.Where(a => 
a.FullName.IndexOfAny(Path.GetInvalidFileNameChars() ) == -1);

这没有任何帮助,每次我得到的例外情况如下:

位于System.IO.Comp.ZetArchiveEntry上System.IO.Compress.ZipHelper.EndsWithDirChar(String test)的System.IO.Path.GetFileName(String path)上的System.IO.Path.CheckInvalidPathChars(String path,Boolean checkAdditional) ZipLibraryConsole的System.IO.Compression.ZipArchive.get_Entries()上的System.IO.Compression.ZipArchive.ReadCentralDirectory()处的System.IO.Compression.ZipArchiveEntry..ctor(ZipArchive archive,ZipCentralDirectoryFileHeader cd)处的.set_FullName(String value) .MicrosoftExtraction.RecursiveExtract(Stream fileToExtract,Int32 maxDepthLevel,Attachment att)在C:\ Users \ myUser \ Documents \ Visual Studio 2015 \ Projects \ ZipLibraryConsole \ ZipLibraryConsole \ MicrosoftExtraction.cs:第47行

这是已实现代码的片段:

var zipArchive = new ZipArchive(fileToExtract, ZipArchiveMode.Read);
  try
    {
      foreach (var zipEntry in zipArchive.Entries) // the exception is thrown  here, there is no chance to process valid entries at all
      {
        // Do something and extract the file
      }
    catch (ArgumentException exception)
    {
      Console.WriteLine(
        String.Format("Failed to complete the extraction. At least one path contains invalid characters for the Operating System: {0}{1}",                       att.Name, att.Extention));
     }