0

I have a file upload control. I restrict users to upload only zip files. the namespace i use is Ionic.Zip; I also want check if that zip file has a folder inside. I have to restrict the users not upload a zipfile with a folder inside. I could check how many files inside zip file like

using (ZipFile zip = ZipFile.Read(file_path))
{
    if (zip.Count < 5)
    {
    }

I do not know how to check for a folder inside

Anyone can help me please. thanks in advance

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Shanna
  • 753
  • 4
  • 14
  • 34

2 Answers2

2

You can iterate on your zip object's ZipEntries - ZipEntry object contains IsDirectory property.

foreach(var entry in zip)
{
    if(entry.IsDirectory)
    {
        //your stuff
    }
}
Tarec
  • 3,268
  • 4
  • 30
  • 47
2
void Main()
{
var isGood=false;

    using (ZipFile zip = new ZipFile(@"c:\\1.zip"))
 {
     for (var i=0;i<zip.Count;i++)
     if (zip[i].Attributes==FileAttributes.Directory) 
      {
       isGood=false;
       break;
      }

 }

 if (isGood) Console.WriteLine ("ok");
 else
 Console.WriteLine ("error");
}

// Define other methods and classes here

edit :

there's seems to be a problem with the way you created this zip file.

I extracted the files from the file you sent me and created new zip : (named 3.zip):

enter image description here

and as you can see - the code works :

enter image description here

so I guess the dll is not powerful enough to recognize edge format

Royi Namir
  • 144,742
  • 138
  • 468
  • 792