How can I delete a directory in a Zip file using .NET?

Chris picture Chris · Mar 24, 2012 · Viewed 9.5k times · Source

How would I delete a directory in a .zip and all the files in it (preferably using DotNetZip)?

Right now I'm running through all the files in the zip, but it doesn't work:

foreach (ZipEntry e in zip)
{
    //If the file is in the directory I want to delete
    if(e.FileName.Substring(0, 9) == "FolderName/")
    {
        zip.RemoveEntry(e.FileName);                          
    }
}

Is there a better way, if not, how would I make this work?

Answer

kosinix picture kosinix · Sep 4, 2014

Here's a simple way to do this:

using (ZipFile zip = ZipFile.Read(@"C:\path\to\MyZipFile.zip"))
{
    zip.RemoveSelectedEntries("foldername/*"); // Delete folder and its contents
    zip.Save();
}

Documentation here http://dotnetzip.herobo.com/DNZHelp/Index.html