Shanna
Shanna

Reputation: 783

Restrict users to upload zip file with folder inside

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

Upvotes: 0

Views: 568

Answers (2)

Royi Namir
Royi Namir

Reputation: 148624

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

Upvotes: 2

Tarec
Tarec

Reputation: 3255

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

foreach(var entry in zip)
{
    if(entry.IsDirectory)
    {
        //your stuff
    }
}

Upvotes: 2

Related Questions