I am creating zip using dotnetzip library.
But I don\'t know how to check if a file exists in the zip. If the file exists then I will update the file with path.
(This is not dotnetzip but will get the job done.)
Requires: using System.IO.Compression;
Assembly: System.IO.Compression.FileSystem.dll
public static bool ZipHasFile(string fileFullName, string zipFullPath)
{
using (ZipArchive archive = ZipFile.OpenRead(zipFullPath)) //safer than accepted answer
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (entry.FullName.EndsWith(fileFullName, StringComparison.OrdinalIgnoreCase))
{
return true;
}
}
}
return false;
}
Example call: var exists = ZipHelper.ZipHasFile(@"zipTest.txt", @"C:\Users\...\Desktop\zipTest.zip");
How to check whether file exists in zip file?
Just use LINQ Any
, assume you have input zip file input.zip
, to check whether input.zip
contains input.txt
:
var zipFile = ZipFile.Read(@"C:\input.zip");
var result = zipFile.Any(entry => entry.FileName.EndsWith("input.txt"));