How to check whether file exists in zip file using dotnetzip

不羁的心 提交于 2019-12-01 06:06:05

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"));

(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");

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!