extract a zip file - flatten directory structure

Sep 26, 2011 at 1:46 PM

Hi,

I have a photoalbum and I'm using a zip file to add the images to the album.  As long as the zip file contains no directories, it all goes fine.  

But let's say I have zip file like this;

album.zip

containing

1.jpg

2.jpg

sub/3.jpg

sub/sub/4.jpg

Is there a way to extract all the jpgs at the root level of the unpackfolder?  Meaning that it ignores the directories in the zip, and extract jpg's only.

If I do it like this, it's takes the directory structure into the unpackDirectory.

    foreach (ZipEntry e in zip)
    {
          e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);                                   
    }

I need a way to remove the preceding folder path before extracting...  any ideas?

Coordinator
Sep 27, 2011 at 4:33 AM

Try something like this:

    foreach (ZipEntry e in zip)
    {
          e.FileName = Path.GetFileName(e.FileName);
          e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);                                   
    }

Sep 27, 2011 at 7:48 AM

Thanks, got there with your tip and this article; http://blogs.planetcloud.co.uk/mygreatdiscovery/post/Flattening-a-directory-structure-with-DotNetZip.aspx

Following code worked for me :

using (ZipFile zip = ZipFile.Read(path + zipToUnpack))
{
    // here, we extract every entry, but we could extract conditionally
    // based on entry name, size, date, checkbox status, etc.  
    foreach (ZipEntry e in zip.EntriesSorted)
    {
        try
        {
            e.FileName = Path.GetFileName(e.FileName);
            e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
        }
        catch { continue; }
    }
} // end using