How to check whether file exists in zip file using dotnetzip

后端 未结 2 521
傲寒
傲寒 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条回答
  • 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");

    0 讨论(0)
  • 2021-01-13 07:53

    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"));
    
    0 讨论(0)
提交回复
热议问题