Try this. I'm sure there are better ways of doing it, but it works for me..
Code:
public static void extractDir(string directoryName, string zipPath, string extractPath)
{
//Open Zip Archive
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
//For each file in archive
foreach (ZipArchiveEntry entry in archive.Entries)
{
//If Directory name equals directoryName Splitting the Directory path, to get the first directory name of the path
//Console.WriteLine(Path.GetDirectoryName(entry.FullName));
if (Path.GetDirectoryName(entry.FullName).Split('/')[0].Equals(directoryName) || Path.GetDirectoryName(entry.FullName).Split('\\')[0].Equals(directoryName))
{
//Combine the destination path
string combinedPath = Path.Combine(extractPath, Path.GetDirectoryName(entry.FullName));
//Create directory(s)
if (!Directory.Exists(combinedPath))
{
Directory.CreateDirectory(combinedPath);
}
//Check if the entry is a directory
if (!Directory.Exists(Path.Combine(combinedPath, entry.Name)))
{
entry.ExtractToFile(Path.Combine(combinedPath, entry.Name));
}
}
}
}
}
A function call would look like this:
extractDir("FolderInZipFileToExtract","Zipfile.zip ","DestinationPath");
The Structure inside the folder to extract is kept the same.