【问题标题】:get archive file list recursively递归获取归档文件列表
【发布时间】:2018-10-07 21:25:14
【问题描述】:

例如,我有一个名为 test.zip 的存档。

我希望它在不解压缩的情况下列出此存档中的所有文件和其中的存档。

  • test.zip
    • 1.txt
    • 2.txt
    • subarchive.zip
      • 3.txt
      • 4.txt
    • 一些迪尔
      • 5.txt

最好在 C# 或 Powershell 上执行此操作。

有可能吗?

【问题讨论】:

    标签: c# powershell archive 7zip


    【解决方案1】:

    您可能想先看看 ZipArchive 类: https://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive(v=vs.110).aspx

    如果您像我一样无法直接使用该类,则可能缺少 ZipArchive 引用。

    正如 user5093161 在这篇文章中所解释的:Cannot find `ZipArchive` in the “System.IO.Compression” namespace 您可能会安装以下两个 NuGet 包。

    1. NuGet System.IO.Compression
    2. NuGet 40-System.IO.Compression.FileSystem

    第一种方法是显示每个条目的全名,例如:

    public void ZipTesting()
    {
        string zipPath = @"c:\zipTest\TestArchive.zip";
        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            foreach (ZipArchiveEntry entry in archive.Entries)
            {
                Console.WriteLine(entry.FullName);
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以通过使用DotNetZip 库来实现这一点,如下所示:

          static void Main(string[] args)
          {
              using (ZipFile zip = ZipFile.Read(@"d:\test.zip"))
              {
                  foreach (ZipEntry e in zip)
                  {
                      Console.WriteLine(e.IsDirectory);
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:

        这是另一个基于 System.IO.Compression.ZipArchive 类遍历存档的版本

        static void OutputEntries(ZipArchive archive) {
                foreach (ZipArchiveEntry entry in archive.Entries) {
                    Console.WriteLine(entry.Name);
        
                    if (entry.FullName.EndsWith(".zip")) {
        
                        ZipArchive embeddedZipArchive = new ZipArchive(entry.Open());
                        OutputEntries(embeddedZipArchive);
        
                    }
                }
        

        就像@Faenrig 已经指出的那样

        如果你像我一样直接使用类有困难 可能缺少 ZipArchive 引用。

        正如 user5093161 在这篇文章中所解释的那样:找不到ZipArchive in “System.IO.Compression”命名空间,您可以安装这两个 关注 NuGet 包。

        NuGet System.IO.Compression NuGet 40-System.IO.Compression.FileSystem

        【讨论】:

          猜你喜欢
          • 2011-11-26
          • 1970-01-01
          • 2020-09-20
          • 2011-11-10
          • 1970-01-01
          • 2013-05-07
          • 2011-04-26
          • 2019-06-22
          • 1970-01-01
          相关资源
          最近更新 更多