zip file addition error

Sep 7, 2009 at 9:45 PM

the code is throwing an error for duplicate files. i tried to catch the exception which will successfully creates the zip, BUT the exception is getting thrown when a file with the same name is added from a separate directory and only one of the files is added.

example only - not real code.

i used zip1.addDirectory("c:\dir1")  to add a directory :

c:\dir1\dirone\file.txt

c:\dir1\dirtwo\file.txt

c:\dir1\dirthree\file.txt

 

 

 

Coordinator
Sep 7, 2009 at 10:06 PM

Can you show me the real code?  And also the real exception.

Also which version of the library are you using?

The zipfile specification insists on a unique name for each entry within the zipfile.   But normally when you add a directory with AddDirectory(), files with the same name, but from different subbdirectories, get unique entry names in the zipfile.  The directory path is considered part of the entry name.

 

Sep 7, 2009 at 11:33 PM

im converting this program to use DotNetZip from DynaZip, and the way things are done is different, but i tried to retain some of it. im not the original author.

i think i found the issue. it was my code. the original author coded a bunch of methods for adding files to and from a list of FILES. it was done this way it seems to be displayed in the tree views, etc. the code i was running was using the file list, then calling addDirectory with the directory path from each file in the list. i just took the time to fix it and the zip is showing all 3 files correctly.

the Exception was an ArgumentException "The entry 'file.txt' already exists in the zip archive."

the code below is the buggy code.


        Dim MyID As New UserSettings
        Dim zip1 As MyZip
        If MyMode = RecipientMode.Suppliers Then
            zip1 = New MyZip(MyNode, MyUserID, MyFilePaths, New ThreadSafe(Me, pbZipProgress, pbMinorZipProgress))
        Else
            zip1 = New MyZip(MyNode, MyUserID, lvRecipients.Items(0).SubItems(0).Text.Trim, lvRecipients.Items(0).SubItems(2).Text.Trim, MyFilePaths, New ThreadSafe(Me, pbZipProgress, pbMinorZipProgress))
        End If


        Select Case MyFileSelectionType
            Case FileAccess.FileSelectionType.FileList, FileAccess.FileSelectionType.SingleDirectory
                For Each FilePath As OneFile In MyFileList
                    zip1.addFile(FilePath.PathAndName)
                Next
            Case FileAccess.FileSelectionType.ParentDirectory
                '*** new code that should get all directories
                For Each f As OneFile In MyFileList
                    zip1.addDirectory(f.DirectoryPath)
                Next

        End Select


        zip1.save() ' will be threaded so all below will need to be moved

        Exit Sub 'testing only

...

 

 

the calling methods in MyZip class

 

Public Sub addFile(ByVal file As String)

        'old
        zip1.AddFile(file)

        'new
        'Try
        '    zip1.AddFile(file)
        'Catch ex As ArgumentException
        '    If Not ex.Message.ToLower().Contains("already exists") Then
        '        ' throw new error
        '    End If
        'End Try

    End Sub

    Public Sub addDirectory(ByVal dir As String)

        'old
        zip1.AddDirectory(dir)
        'new
        'Try
        '    zip1.AddDirectory(dir)
        'Catch ex As ArgumentException
        '    If Not ex.Message.ToLower().Contains("already exists") Then
        '        ' throw new error
        '    End If
        'End Try

    End Sub
    Public Sub save()
        zip1.Save(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) & "\SNETPC_Temp\" & filename)
    End Sub

 

these are the values that were fed into the addDirectory method. the new code only adds "C:\dir1"

     f.DirectoryPath    "C:\dir1\dirone\"    String

     f.DirectoryPath    "C:\dir1\dirthree\"    String

<exception time>

---

this is my new code using the new property list

Select Case MyFileSelectionType
            Case FileAccess.FileSelectionType.FileList, FileAccess.FileSelectionType.SingleDirectory
                For Each FilePath As OneFile In MyFileList
                    zip1.addFile(FilePath.PathAndName)
                Next
            Case FileAccess.FileSelectionType.ParentDirectory
                '*** new code that should get all directories
                For Each path As String In MyDirList
                    zip1.addDirectory(path)
                Next

        End Select

 

thanks for your quick response

 

Sep 7, 2009 at 11:40 PM
Edited Sep 8, 2009 at 12:14 AM

it seems adding the directories separately causes the same error above.

"C:\dir1\dirone\" and "C:\dir1\dirthree\"

 

edit: forgot to add the version # 1.8.4.22

 

     stack    "   at Ionic.Zip.ZipFile.InsureUniqueEntry(ZipEntry ze1)
   at Ionic.Zip.ZipFile.AddFile(String fileName, String directoryPathInArchive)
   at Ionic.Zip.ZipFile.AddOrUpdateDirectoryImpl(String directoryName, String rootDirectoryPathInArchive, AddOrUpdateAction action, Int32 level)
   at Ionic.Zip.ZipFile.AddOrUpdateDirectoryImpl(String directoryName, String rootDirectoryPathInArchive, AddOrUpdateAction action)
   at Ionic.Zip.ZipFile.AddDirectory(String directoryName, String directoryPathInArchive)
   at Ionic.Zip.ZipFile.AddDirectory(String directoryName)
   at FileAccess.MyZip.addDirectory(String dir) in C:\Documents and Settings\ye456c\My Documents\SNETPC\SNETPC2006\FileAccess\MyZip.vb:line 55"    String

Coordinator
Sep 8, 2009 at 1:15 AM

That makes sense. 

If you call AddDirectory(dirName), all of the files found in the directory, and its subdirectories, are added to the zipfile, starting at the root.  so if you have a directory called c:\dir1, containing file1.txt, and a subbdirectory called subdir, with a file2.txt, and you call AddDirectory("c:\dir1") , then in the zipfile you will have /file1.txt, and /subdir/file2.txt.  If you then have a c:\dir2 with similar contents, and you call AddDirectory(), you will get an exception.

The wat to avoid the problem is to add each directory into its own directory in the zipfile.  call AddDirectory("c:\dir1", "dir1"), and AddDirectory("c:\dir2", "dir2"). 

 

Sep 8, 2009 at 1:49 AM

that worked.   my solution looks like this. zip1.AddDirectory(dir, dir).

it doesnt seem to complain that i gave it the full path including the C:\

 

Thanks!