This project is read-only.

Question about extracting folders from zip with relative path

Jan 4, 2012 at 4:07 PM


I have a problem with extracting a folder within a zip file with its partial path. Is that possible ?

Basically the thing is lets say I have the following zip file:



And I want to extract the directory "temp\" from my zip file in directory "C:\qwerty\"

If I extract it the normal way I would have "C:\qwerty\temp\files\file1.txt".

What I want to achieve is somehow to pass a relative path so that only the files/folders within that path are extracted without the path given. So in other words I would like to have my file extracted in the following way:


I have figure out a solution but in the case above its not working. If I change the name for the item being extracted so that I delete the path in the beginning it is extracted. In the case above I change the name of "temp\files\file1.txt" to "files\file1.txt" and receive an exception that the item with the same key already exists (which is normal as there is another file with the same path)

Another way to achieve what I am trying is to extract everything in a temp folder and move it where I would like (this seems too complicated for such a small thing).

My question is am I missing something or I should do it the hard way ?



Jan 5, 2012 at 11:45 PM

You are not missing anything.

I think you understand the situation pretty well.

Each ZipEntry item in the ZipFile object must have a unique name. You can rename a ZipEntry before extracting, but you cannot rename an item using a name of a ZipEntry that already exists.

One way to avoid the issue is to  call ZipFile.Remove() on the ZipEntry with the existing name. If you like you can protect that call inside a try...catch clause if you're not sure that an item with the given name really exists.  Or you could test for existence before deleting, and then rename.

Anyway, it seems you understand it.

Jan 11, 2012 at 8:19 AM

Hi Chesso,

Thanks very much for the answer. It seems I will extract all the files in the Windows temp folder and after that moving them.