This project is read-only.

Unable to get ZipError handler to file with AddFiles

Apr 7, 2011 at 4:41 PM

I'm trying to get the ZipError event to fire when I have an error adding an entry.  I created my list of files I wan and then use the AddFiles method.  When I step through the code it enters the AddProgressHandler and exits the sub and I recieve an exception with "An item with the same key has already been added".  I'm opening an existing zip file and found that occasionally I might have an item in the zip that I'm trying to add again.  I would like to be able to use the ZipError event to capture that information and handle it.

    Sub Main

                    'Add the handlers
                    AddHandler objZip.AddProgress, AddressOf AddProgressHandler
                    AddHandler objZip.ZipError, AddressOf ZipErrorHandler

                    objZip.CompressionLevel = Ionic.Zlib.CompressionLevel.Default
                    objZip.AddFiles(strSelectedFiles, "")

    End Sub

    Private Sub AddProgressHandler(ByVal sender As Object, ByVal e As AddProgressEventArgs)
        Select Case e.EventType
            Case ZipProgressEventType.Adding_Started
                Console.WriteLine("Adding files to the zip...")
                Exit Select
            Case ZipProgressEventType.Adding_AfterAddEntry
                Console.WriteLine(String.Format("Adding file {0}", e.CurrentEntry.FileName))
                Exit Select
            Case ZipProgressEventType.Adding_Completed
                Console.WriteLine("Added all files")
                Exit Select
        End Select
    End Sub

    Private Sub ZipErrorHandler(ByVal sender As Object, ByVal e As ZipErrorEventArgs)
        ' At this point, the application could prompt the user for an action to take.
        ' But in this case, this application will simply automatically skip the file, in case of error.
        Console.WriteLine("Zip Error,  entry {0}", e.CurrentEntry.FileName)
        Console.WriteLine("   Exception: {0}", e.Exception)
        ' set the desired ZipErrorAction on the CurrentEntry to communicate that to DotNetZip
        e.CurrentEntry.ZipErrorAction = ZipErrorAction.Skip
    End Sub

Apr 7, 2011 at 4:58 PM
Edited Apr 7, 2011 at 5:00 PM

Here is the exception detail.

System.ArgumentException was unhandled
  Message="An item with the same key has already been added."
       at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
       at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
       at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
       at Ionic.Zip.ZipFile.InternalAddEntry(String name, ZipEntry entry)
       at Ionic.Zip.ZipFile._InternalAddEntry(ZipEntry ze)
       at Ionic.Zip.ZipFile.AddFile(String fileName, String directoryPathInArchive)
       at Ionic.Zip.ZipFile.AddFiles(IEnumerable`1 fileNames, Boolean preserveDirHierarchy, String directoryPathInArchive)
       at Ionic.Zip.ZipFile.AddFiles(IEnumerable`1 fileNames, String directoryPathInArchive)
       at Directory_Maintenance.Module1.Main()
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()