Unzip archive without overwrite throws exception when a file exists.

Jan 23, 2009 at 7:42 PM
Hello,

I have a scenario where I'm extracting a zip file into a directory and I don't want it to overwrite the preexisting files. I just want to extract the files that don't exist in the destination folder. Is this possible? Because before I was getting exceptions when I attempted this?

Thanks
-Blake Niemyjski
Coordinator
Jan 25, 2009 at 5:45 AM
Edited Jan 26, 2009 at 2:46 AM
Hmmm, Blake, what you want sounds reasonable, but
it is currently not possible to do that with a single method call.  You can do it with some extra logic, though.

In the ZipEnty interface, for Extract() I think there are two options: either overwrite existing files, or throw an Exception when there is an attempt to extract onto an existing file. 
There is currently no option to "just quietly not extract" files that already exist.  Therefore, the way to do what you want is to NOT try to Extract() when the file already exists.

I can suggest that you do something like this:
  foreach (ZipEntry e in zip)
  {
     if (!e.IsDirectory)
     {
          if (!File.Exists(Path.Combine(ExtractDir,e.FileName)))
          {
              e.Extract(ExtractDir);
          }
     }
  }
In english: enumerate through the entries in the zip file. Call Extract() on each entry only if the target file does not exist.

I can imagine getting fancier, for example only extracting files if the file on disk is OLDER than the file in the archive. And so on. That is all left as an exercise for YOU.
Feb 18, 2009 at 1:00 PM
Edited Feb 18, 2009 at 1:11 PM
Hello,

Thanks

Thanks
-Blake Niemyjski