代码之家  ›  专栏  ›  技术社区  ›  Amir

如何在zip文件中查找实体是C中的文件或目录#

  •  -1
  • Amir  · 技术社区  · 6 年前

    我想在TreeView中创建一个zip文件的内容,但我的问题是如何在不提取文件的情况下识别zip文件的内容是文件或目录,然后将它们添加到树中?

    你能帮我解决这个问题吗?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Sepehr Samiei    6 年前

    此功能在.NET Framework 4.5及更高版本中是现成的。您必须使用此库:

    using System.IO.Compression;
    

    然后你就能做到:

    string zipPath = @"c:\example\start.zip";
    using (ZipArchive archive = ZipFile.OpenRead(zipPath))
    {
        foreach (ZipArchiveEntry entry in archive.Entries)
        {
            if (entry.FullName.EndsWith('\'))
                Console.WriteLine($"{entry.FullName} is a directory.");
            else
                Console.WriteLine($"{entry.FullName} is a file.");
        }
    } 
    

    有关详细信息,请参阅此处:

    How to list the contents of a .zip folder in c#?