Exception: entry already exists

Jan 4, 2010 at 9:26 PM

basically i pre-build folder lists by recursing directories according to filters. 

so i might have a directory tree that looks something like this but much larger





the code i am using to add each folder is:


For Each folder As String In f
                                zip.AddDirectory(folder, IO.Path.GetDirectoryName(folder + "\"))

 For Each folder As String In folders

                                zip.AddDirectory(folder, IO.Path.GetDirectoryName(folder + "\"))




this works fine when i dont have folders with duplicate files in. when i try parse this with subfolders with duplicate files it throws a file exists exception. looking at the zip file it creates separate folders and places the files in each but it seems to still throw this error even though the paths are different.

Jan 4, 2010 at 9:37 PM

ok .. 2 things

1. what version of the library are you using?

2. I feel like I have most of the information on the scenario, but not quite enough.  How exactly do you build the pre-build the folder lists?   What does it mean when you say "subfolders with duplicate files?"  if you give me just a little bit more information, actual code and an actual dir hierarchy that truly (not theoretically) reproduces the problem, I will be able to understand and fix the problem quickly.


Jan 4, 2010 at 10:01 PM

using version

so building the folder list is based on a user entering a source path eg: c:\test and then parsing in an option like "subfolders' = gets all the sub folders from this path, "contains"=folder name contains a a string

after the user enters this i go off and get a list of all the folders.. example of problem that reproduces the error.

(0) = "C:\testhousekeeping\Copy of felogs"

(1) = "C:\testhousekeeping\felogs"

(2) = "C:\testhousekeeping\subempty"

(3) = "C:\testhousekeeping\subempty\groupempty"

(4) = "C:\testhousekeeping\subempty\New Folder"

(5) = "C:\testhousekeeping\subempty\pics 1"

(6) = "C:\testhousekeeping\subempty\groupempty\New Folder"

(7) = "C:\testhousekeeping\subempty\groupempty\New Folder (2)"


i get the error "The entry 'C:/testhousekeeping/subempty/pics 1/CAD.png' already exists in the zip archive."

Jan 4, 2010 at 10:05 PM

if i save after each add it builds the zip file until the failure. the zip file contains separate folders

Jan 5, 2010 at 5:18 AM

Ah, ok.

here's what's happening .

You have a directory tree, like this :


And what you're doing is adding each of those directories into the zip, by name.  If you iterate through that list of directories, calling AddDirectory() on each one, You will be adding c:\root\a\a1 TWICE.  And this throws an exception.

Here's why c:\root\a\a1 gets added twice: When you call AddDirectory(), the specified  filesystem directory and all files and directories contained within it are added to the zip.  Therefore when you call AddDirectory(c":\root\a"), the directory c:\root\a\a1 is one of the things added into the zip implicitly, because you added its parent.  When you later call AddDirectory("c:\root\a\a1"), explicitly, you then attempt to add the specified  filesystem directory (c:\root\a\a1) again.  (And likewise with c:\root\a\a2, c:\root\b\b1, and so on, for any 2nd-level directory in that hierarchy, except the code never gets that far, because you receive an exception the first time you add a duplicate, which you encounter with c:\root\a\a1).

The conclusion is: you're doing it wrong. If you want to add all the files contained within a particular directory, as with c:\root above, or your c:\testhousekeeping, you must call AddDirectory() once, for c:\root (or c:\testhousekeeping).  Or, you must call AddDirectory() for only the toplevel child directories of c:\root.   Calling Adddirectory() again, for a subdirectory of a directory for which you've already called AddDirectory(), will result in that "duplicate entry" exception you saw.

I hope that makes sense.