请告诉我如何计算ZIP文件中的文件数量。
我需要一个C#代码来完成这个工作在视觉工作室。当我在谷歌上搜索时,尝试了很多代码,但是发现一个错误,说:
在ZIPENTRY/ for文件中找不到命名空间或程序集。
有人能告诉我应该包括什么/任何需要安装的/提供任何代码来统计文件的数量吗?
发布于 2013-08-28 05:54:17
正如MSDN所说(.Net 4.5),您可以使用ZipArchive和ZipFile类:
http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx
不过,System.IO.Compression命名空间中的类都位于不同的程序集System.IO.Compression和System.IO.Compression.FileSystem中。
因此,您可以将对System.IO.Compression和System.IO.Compression.FileSystem程序集的引用添加到项目中,并尝试如下所示:
...
using System.IO.Compression; 
...
  // Number of files within zip archive
  public static int ZipFileCount(String zipFileName) {
    using (ZipArchive archive = ZipFile.Open(zipFileName, ZipArchiveMode.Read)) {
      int count = 0;
      // We count only named (i.e. that are with files) entries
      foreach (var entry in archive.Entries)
        if (!String.IsNullOrEmpty(entry.Name))
          count += 1;
      return count;
    }
  }另一种可能是使用DotNetZip库,请参见:
发布于 2017-05-10 08:56:54
必须将引用System.IO.Compression和System.IO.Compression.FileSystem添加到项目中。
using (var archive = System.IO.Compression.ZipFile.Open(filePath, ZipArchiveMode.Read))
{
    var count = archive.Entries.Count(x => !string.IsNullOrWhiteSpace(x.Name));
}发布于 2022-11-09 06:50:46
使用如下:
using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
var totalFiles = zip.Entries.Where(x=>x.Length>0).Count()文件夹没有任何长度
https://stackoverflow.com/questions/18479921
复制相似问题