Only extract files in root directory

Aug 10, 2009 at 11:53 AM

I'm using this library and have been very impressed by its functionality, everything has been working great until someone uploaded a zip file that was created using a Mac. For some reason there is an added __MACOSX folder in there which is causing problems in my application. What I want to do is only process files that are in the root of the zip file, any directories and any files within them should be ignored. Is there a built-in way of doing this or would I need to do some processing myself?

Thanks for any help

Coordinator
Aug 10, 2009 at 11:38 PM
Edited Aug 10, 2009 at 11:39 PM

I don't know exactly what you mean by "process files that are in the root of the zip file".

You can select entries to extract using LINQ (See examples for C# or VB), or using the ZipFile.SelectEntries() method.

Example using LINQ to select files in C#:

Console.WriteLine("\n\nOnly those entries in the 'root':\n" + divider);
using (var zip = ZipFile.Read(_archiveToRead))
{
    var selection = from e in zip.Entries
        where !e.FileName.Contains("/")
        select e;

    foreach (var e in selection)
    {
        Console.WriteLine(e.FileName);
        e.Extract(_extractDir, overwrite);
    }
}

 

Console.WriteLine("\n\nOnly those entries from the root (method #2):\n" + divider);
using (var zip = ZipFile.Read(_archiveToRead))
{
    // select all zip entries in the "root" directory
    var selection = zip.SelectEntries ("*.*", "");

    foreach (var e in selection)
    {
        Console.WriteLine(e.FileName);
        e.Extract(_extractDir, overwrite);
    }
}

If you need an example in VB, let me know.

Aug 11, 2009 at 9:16 AM

Hi, what i meant by process the files in the root was select only the files that are in the root folder so I can process them in my application, not select any files that are within any directories in the zipfile. That example code you provided works great, I used the second example and it now ignores the __MACOSX that zip files created on a mac seem to insert for some reason!

Thanks for your help