4

I am trying to check if a folder exists in a zip file. The code is following:

//All entries refered too exists.
//For files (Workes fine, returns true)
var hello1 = zip.Any(entry => entry.FileName.Equals(@"Patients.xml"));
var hello2 = zip.Any(entry => entry.FileName.Equals(@"Bookings.xml"));

//For folders (Dosent work (returns false))
var result1 = zip.Any(entry => entry.FileName.Equals(@"PatientsF"));
var result2 = zip.Any(entry => entry.FileName.Equals(@"U14"));

I have tryed with:

entry.FileName.Contains(@"PatientsF"));

And that works, but i want to get the folder with the exact name "PatientsF". With the code "Contains" it would return true if the name just have "PatientsF". How should i fix this?

Any help will be appreciated. thanks in advance.

PS. If i'm unclear somewhere, or if you need more information then just explain what's needed.

MasterXD
  • 804
  • 1
  • 11
  • 18
  • 1
    you could check the filename length also – Sayse Jun 17 '13 at 13:32
  • Thanks Sayse, the trick worked, if the lenght have to be 9 (10 in the code, dont know why.) then there is no way that other folders can have the same name. But is there a more secure way? – MasterXD Jun 17 '13 at 13:48
  • Select every directory with entry.IsDirectory == true. Output them in the console see what their "real" names are. You now know what on filter on, instead of wild guessing :D. – C4stor Jun 17 '13 at 14:00

1 Answers1

7

Then expand on what does work to make it sure to find a folder:

entry.FileName.Contains("PatientsF/"));

The / is a path delimiter, so it cannot be part of a filename.

DonBoitnott
  • 10,787
  • 6
  • 49
  • 68
  • Sorry, With "\" it does not even find the folder. – MasterXD Jun 17 '13 at 13:42
  • Remove the first \ so try this `entry.FileName.Contains(@"PatientsF\"));` – Jason Jong Jun 17 '13 at 13:48
  • foreach (ZipEntry zip in objSourceZipfileCheck) { if(zip.FileName.Contains("/")) { folder there } } – Kamran Shahid Jun 17 '13 at 14:18
  • @KamranShahid That wouldn't work for me. In my case, when I inspect my ZIP collection of entries, I see that every one starts with `ZipEntry/` followed by the sub-path of the file itself. So for example: `ZipEntry/MyFolder/MyFile.txt`. So you'd simply flag every entry in error. – DonBoitnott Jun 17 '13 at 14:23
  • hmm.It works for me.Looks like i have to recheck the code or my zip file structure.or may i need your zip file – Kamran Shahid Jun 17 '13 at 19:53
  • 2
    @KamranShahid It is also possible that not every implementation of ZIP structure is the same. For the record, OP is using the DotNetZip library, and I am using Ionic.Zip. – DonBoitnott Jun 17 '13 at 20:57
  • As far as i know ionic zip and dotnetzip are same see http://dotnetzip.codeplex.com/ – Kamran Shahid Jun 18 '13 at 05:38