Reputation: 961
I am trying to verify that there is a file with a certain filename in a zip file. Is there a better way to do it than the following?
using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
{
if (!archive.Entries.Any(e => e.Name.Equals(FileNameToCheckFor)))
{
// Throw an exception
}
foreach (ZipArchiveEntry file in archive.Entries)
{
// Do some processing. This is unrelated.
}
}
Upvotes: 2
Views: 206
Reputation: 8699
Since ZipArchive.GetEntry returns null if the entry does not exist, you could replace the lambda expression with:
if (archive.GetEntry(FileNameToCheckFor) == null)
{
// Throw an exception
}
This is slightly more concise, but that is not to imply anything incorrect about the original code.
Upvotes: 4