Home > Enterprise >  Check if zip is empty in c#
Check if zip is empty in c#

Time:12-03

I have a question. I'm trying to find out if my file which is a .zip is empty. The problem is that testing the length does not work because the size of a zip file is never equal to 0.

FileInfo[] fileInfos = ...
foreach (FileInfo fileInfo in fileInfos)
{                    
    if (fileInfo.Length < 0)
    {
       //  do action like delete
    }                                 
}

Thank you in advance for your help

CodePudding user response:

You can use the ZipArchive functionality:

using System.IO.Compression;

using (ZipArchive archive = ZipFile.Open(zipPath, ZipArchiveMode.Read))
{
  int fileCount = archive.Entries.Count;
  bool isEmpty = fileCount == 0;
}
  • Related