Search code examples
c#-4.0dotnetzip

How To Remove Duplicate Values From Zip in c#


I have multiple folders having same files I want to download. In first time of foreach loop I add one directory and if any directory have duplicate files I can handle through removing duplicate values from List<>, but the second time the foreach loop is executed, another folder with the same file that already added in zip added into Zip,and i have to face that zip contain duplicate values...

Is there any method to remove duplicate values from zip...

using (var Zip = new ZipFile())
{
    foreach (var file in item)
    {
         if (file.isCheck == true)
         {
             string path = file.path;
             if (Directory.Exists(path))
             {
                 string[] folders = path.Split(new string[] { User.Identity.Name }, StringSplitOptions.None);
                 if (!folders[1].Contains("/"))
                 {
                     folders[1] = Path.Combine("", folders[1]);
                 }

                 string lastFolderName = folders[1];
                 string security = db.Users.Where(e => e.Email == User.Identity.Name).Select(s => s.SecurityStamp).Single();
                 string ext = Path.GetExtension(path);

                 if (path.Contains(security))
                 {
                     Zip.AddDirectory(file.path);

                     List<string> Generalfile = null;

                     Generalfile = db.General.Where(p => p.Folder == lastFolderName || p.Folder.Contains(lastFolderName)).Select(p => p.PathInDb).ToList();

                     List<string> unique = Generalfile.Distinct().ToList();
                     foreach (var GF in unique)
                     {             
                          Zip.AddFile(GF, "Files");
                     }
                 }

                 else
                 {
                      if (Directory.Exists(file.path))
                         Zip.AddDirectory(file.path);
                      else
                         Zip.AddFile(file.path, "Files");
                 }
              }
              else
                Zip.AddFile(path, "Files");
        }

      }

    Zip.Save(outputstream);
    }
    outputstream.Position = 0;
    return File(outputstream, "applicatoin/zip", "Download.zip");

Solution

  • Using the DotNetZip Library, you should use the ContainsEntry method to check if a file exists before adding it.