Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check whether file exists in zip file using dotnetzip

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

            }}}
like image 748
chetan Avatar asked Sep 23 '12 15:09

chetan


1 Answers

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"));
like image 175
cuongle Avatar answered Sep 30 '22 14:09

cuongle