This project has moved and is read-only. For the latest updates, please go here.

extract files in forders

May 19, 2014 at 8:11 AM
Hi...amazing work! Well done...
I created a project and I want to extract a zipped folder...I managed do this but I get all files in one folder (output folder).. What I want to do is to extract my files but in their original position. eg. output_folder/output1_folder/extracted_files
output_folder/output3_folder/extracted_files, etc.

Is that possible???
May 27, 2014 at 2:26 AM
Edited May 27, 2014 at 2:26 AM
Hey mate,

I had the same problem, but after reading your post that had no answers I thought I would investigate on my own, and found a solution.

I'm not sure what method you are using to extract files. but I was using the example code method. When I looked through it I saw that the example method uses:
path = Path.Combine(TargetFolder, Path.GetFileName(entry.FilenameInZip));
result = zip.ExtractFile(entry, path);
So essentially it is stripping out any nesting for the filename, then extracting them all to the same root directory. I modified it to:
path = Path.Combine(TargetFolder, entry.FilenameInZip);     <--changed
result = zip.ExtractFile(entry, path);
So now each file is extracted to it's complete subfolder. No need to check for existence of the subfolder(s) as checking is done by the ExtractFile method already. The only 'improvement' I made is adding the following block around it all:
if (Path.GetFileName(entry.FilenameInZip) != String.Empty)
    path = ...
    result = ...
The reason for this is because when you loop over all entries in the zip file (foreach ZipFileEntry in zip.ReadCentralDir) every folder is stored as an entry as well, and I'm not sure whether passing in a folder to the ExtractFile method will work. Even if it does, the full folder chain will be created recursively in ExtractFile when the file is extracted anyway, so why double-handle it?

Hope this has helped :)