简体   繁体   English

如何在C#中计算zipfile中的文件数

[英]how to count number of files in zipfile in c#

Please tell me how to count the number of files in ZIP file. 请告诉我如何计算ZIP文件中的文件数。

I need a C# code to do this job in visual studio. 我需要C#代码才能在Visual Studio中完成这项工作。 Tried many codes when I Googled but getting an error saying: 我在Google搜索时尝试了许多代码,但收到一条错误消息:

The namespace or assembly is not found for ZIPENTRY/ZIPFILE. 找不到ZIPENTRY / ZIPFILE的名称空间或程序集。

Can anyone tell me what i should include/anything need to be installed/provide me any code to count the number of files? 谁能告诉我我应该包含的内容/需要安装的所有内容/为我提供用于计数文件数量的任何代码?

As MSDN puts it (.Net 4.5) you can use ZipArchive and ZipFile classes: 正如MSDN所说(.Net 4.5),您可以使用ZipArchiveZipFile类:

http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive.aspx http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx

the classes being both in System.IO.Compression namespace are in different assemblies System.IO.Compression and System.IO.Compression.FileSystem though. 但是,位于System.IO.Compression命名空间中的类分别位于不同的程序集System.IO.CompressionSystem.IO.Compression.FileSystem中

so you may add references to System.IO.Compression and System.IO.Compression.FileSystem assemblies to your project and try something like this: 因此您可以在项目中添加对System.IO.CompressionSystem.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;
    }
  }

Another possibility is to use DotNetZip library, see: 另一种可能性是使用DotNetZip库,请参见:

Count number of files in a Zip File with c# 使用C#计算Zip文件中的文件数

You have to add references System.IO.Compression and System.IO.Compression.FileSystem to your project 您必须将引用System.IO.CompressionSystem.IO.Compression.FileSystem添加到您的项目

using (var archive = System.IO.Compression.ZipFile.Open(filePath, ZipArchiveMode.Read))
{
    var count = archive.Entries.Count(x => !string.IsNullOrWhiteSpace(x.Name));
}
int count;
using (ZipFile zip = ZipFile.Read(path))
count = zip.Count;

You can try something like this !!! 您可以尝试这样的事情!

You can refer this 你可以参考这个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM