6

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.

    public void makezip(string flname)
   {
      string  fln =flname;
        string curFile = @"d:\crs.zip";
        if (File.Exists(curFile))
        {
                ZipFile zipfl = ZipFile.Read(@"D:\crs.zip");
            var result = zipfl.Any(entry => entry.FileName.EndsWith(@fln));
            if (result == true) {
                zipfl.UpdateFile(@fln);
                }else{
                  zipfl.AddFile(@fln);
                }
            zipfl.Save(@"d:\crs.zip");
        }
        else
        {
            try
            {
                ZipFile zipfl = new ZipFile();

                var result = zipfl.Any(entry => entry.FileName.EndsWith(@fln));
                if (result == true)
                {
                  zipfl.AddFile(@fln);
                }
                zipfl.Save(@"d:\crs.zip");
            }catch {
                MessageBox.Show("Invalid Zip File");

            }}}
chetan
  • 129
  • 1
  • 2
  • 12

2 Answers2

14

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"));
cuongle
  • 74,024
  • 28
  • 151
  • 206
  • 1
    +1; one word of caution though: if I am not mistaken, the `FileEntry.FileName` may contain (part of) a directory path as well, see [here](http://dotnetzip.herobo.com/DNZHelp/html/913abfd3-bbc6-803c-858b-f53dfb008e55.htm). – Anders Gustafsson Sep 23 '12 at 17:03
  • @AndersGustafsson: thanks, I have run this code, it does not containe dir path – cuongle Sep 23 '12 at 17:14
  • How can i check if file has folder information like c:\ab\input.txt – chetan Sep 24 '12 at 08:05
  • Thanks for your reply, I have changed my code with that. I m not able to search a file with path. – chetan Sep 24 '12 at 08:57
  • 1
    zipFile.Any(entry => entry.FileName.Equals(directory + @"/" + "input.txt")) will search with directory information, as you might have a file with the same name or different name but ending with "input.txt" and your code will think it has already been added. – pengibot Oct 15 '12 at 14:20
6

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

StinkySocks
  • 812
  • 1
  • 13
  • 20
  • Be mindful of unzipping files that were compressed using a Mac. Those .zips might contain a subdirectory, "_MACOSX". I ignore those by testing for Contains("_MACOSX") first and continuing. – J. Horn Nov 06 '17 at 17:36