5

Please tell me how to count the number of files in ZIP file.

I need a C# code to do this job in visual studio. Tried many codes when I Googled but getting an error saying:

The namespace or assembly is not found for 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?

Zaheer Ahmed
  • 28,160
  • 11
  • 74
  • 110
praveen
  • 93
  • 1
  • 3
  • 6
  • See http://stackoverflow.com/questions/15241889/i-didnt-find-zipfile-class-in-the-system-io-compression-namespace – Vadim Aug 28 '13 at 05:19
  • thanks vadmin...i searched here also but couldnt get this link – praveen Aug 28 '13 at 05:23
  • possible duplicate of [Count number of files in a Zip File with c#](http://stackoverflow.com/questions/4785391/count-number-of-files-in-a-zip-file-with-c-sharp) – Zaheer Ahmed Aug 28 '13 at 05:36
  • http://stackoverflow.com/questions/15241889/i-didnt-find-zipfile-class-in-the-system-io-compression-namespace/42531985#42531985 – Burgo855 Mar 01 '17 at 12:42

4 Answers4

6

As MSDN puts it (.Net 4.5) you can use ZipArchive and ZipFile classes:

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.

so you may add references to System.IO.Compression and System.IO.Compression.FileSystem assemblies to your project and try something like this:

...
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:

Count number of files in a Zip File with c#

Community
  • 1
  • 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

You can try something like this, using DotNetZip.

using DotNetZip;

int count;
using (ZipFile zip = ZipFile.Read(path))
    count = zip.Count;

I found this solution here.

Neuron
  • 5,141
  • 5
  • 38
  • 59
adityaswami89
  • 573
  • 6
  • 15
  • 1
    Sorry, but according to MSDN http://msdn.microsoft.com/en-us/library/system.io.compression.zipfile.aspx ZipFile is a static class, no ZipFile zip = ... is possible – Dmitry Bychenko Aug 28 '13 at 05:58
  • 1
    Did you get this code from [this](http://stackoverflow.com/a/4785405/21567) answer? If so please indicate that (and add the part about "DotNetZip", which would at least make your code valid, regarding @DmitryBychenko's comment) – Christian.K Aug 28 '13 at 06:11
1

You have to add references System.IO.Compression and System.IO.Compression.FileSystem to your project

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

Use below:

using var zip = new ZipArchive(stream, ZipArchiveMode.Read);
var totalFiles = zip.Entries.Where(x=>x.Length>0).Count()

Folder do not have any Length