How to check whether file exists in zip file using dotnetzip

后端 未结 2 548
傲寒
傲寒 2021-01-13 07:09

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.

2条回答
  •  猫巷女王i
    2021-01-13 07:51

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

提交回复
热议问题