Search if File Exists in ZIP Archive without Extracting or Decompressing | C# .NET

Hi – is there a way to find a file and if exists get only that file without decompressing/un-archiving it?

@mbalathandapani,

You can try using the following C# code of Aspose.ZIP for .NET API to determine if a File exits in ZIP archive or not:

bool flag = false;
string fileName = "file.pdf";

Archive o = new Archive(dataDir + "CompressSingleFile_out.zip");
foreach (ArchiveEntry entry in o.Entries)
{
    if (entry.Name.Equals(fileName))
    {
        flag = true;
    }
}

if (flag)
{
    Console.WriteLine("File found");
}
else
{
    Console.WriteLine("File not found");
}

If this is not what you are asking about, then please elaborate your inquiry further by providing complete details of your usecase. What do you mean by ‘if exists get only that file without decompressing or un-archiving it’?

Thanks. Is this is the only way? If I have an archive around 5000 entries and that would loop through all the entries (of course we could do a break; to get out) if that file doesn’t exist? Is there any performance cost associated with this?

@mbalathandapani,

Yes, you need to iterate through each ArchiveEntry to be able to determine existence of a particular file. In this case, the processing time (performance) fully depends on your ZIP archives (number of files it contains etc). You may experiment on your end (_using the latest version https://downloads.aspose.com/zip/net_) to determine the exact performance cost of 5000 file archive vs small archive. Please let us know if we can be of any further assistance.