I have a zip archive and the folder structure inside of an archive looks something like this:
+ dirA
- fileA.txt
+ dirB
- fileB.txt
I'm trying to extract the contents of dirA to disk, but while doing so, I'm unable to perserve the folder structure, and instead of
- fileA.txt
+ dirB
- fileB.txt
I get
- fileA.txt
- fileB.txt
Here's my code:
using (ZipArchive archive = ZipFile.OpenRead(archivePath)) // archivePath is path to the zip archive
{
// get the root directory
var root = archive.Entries[0]?.FullName;
if (root == null) {
// quit in a nice way
}
var result = from curr in archive.Entries
where Path.GetDirectoryName(curr.FullName) != root
where !string.IsNullOrEmpty(curr.Name)
select curr;
foreach (ZipArchiveEntry entry in result)
{
string path = Path.Combine(extractPath, entry.Name); // extractPath is somwhere on the disk
entry.ExtractToFile(path);
}
}
I'm pretty positive that's because I use entry.Name instead of entry.FullName in Path.Combine(), but if I were to use FullName, I would have in path that root directory dirA I'm trying not to extract.
So I've hit a wall here, and the only solution I can think of is extracting the whole zip with:
ZipFile.ExtractToDirectory(archivePath, extractPath);
...and then moving the subfolders from dirA to a different location and deleting dirA. Which doesn't seem like the luckiest of ideas.
Any help is much appreciated.
You could just shorten the FullName by the part of the path you dont want:
foreach (ZipArchiveEntry entry in result)
{
var newName = entry.FullName.Substring(root.Length);
string path = Path.Combine(extractPath, newName);
if (!Directory.Exists(path))
Directory.CreateDirectory(Path.GetDirectoryName(path));
entry.ExtractToFile(path);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With