******.zip is not a valid zip file using DotNetZip-v1.9

Mar 8, 2011 at 12:29 AM

i'm getting this kind of error. but after i inserted the "goto here:" the program can still "read" the files inside the zipfile.

here is my code..

Imports Ionic.Zip
Imports System.Text.RegularExpressions
Imports System.io

Dim ZIP As Ionic.Zip.ZipFile
Dim teststr As String = "C:\Input"

Sub Main()
        ' Create a new FileSystemWatcher and set its properties.
        Dim watcher As New FileSystemWatcher()
        watcher.Path = teststr
        ' Watch for changes in LastAccess and LastWrite times, and
        ' the renaming of files or directories. 
        watcher.NotifyFilter = (NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName)
       ' Only watch zip files.
        watcher.Filter = "*.zip"

        ' Add event handlers.
        AddHandler watcher.Created, AddressOf OnChanged

        ' Begin watching.
        watcher.EnableRaisingEvents = True

        Console.WriteLine("Exit? Press Enter...")

        Process.Start(System.AppDomain.CurrentDomain.BaseDirectory + "SCST1700Edit.exe")

End Sub

Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' Specify what is done when a file is changed, created, or deleted.
        If e.ChangeType = IO.WatcherChangeTypes.Created Then

            'MsgBox("File " & e.FullPath & " has been created")
            Dim strZipfile As String
            Dim arrFileList As New ArrayList()
                Using ZIP As New ZipFile(e.FullPath)
                    Dim zipFile As ZipEntry
                    'adds zipfiles in an arraylist
                    'and checks if zip contains only tif and idx files
                    For Each zipFile In ZIP
                        strZipfile = zipFile.ToString.Replace("ZipEntry::", "")
                        If Regex.IsMatch(zipFile.ToString, "\d*(\.tif|.idx)") Then
                            ErrorPrinter(1, strZipfile)
                        End If
                End Using
            Catch ex As Exception
                GoTo here
            End Try

        End If
    End Sub

Mar 8, 2011 at 12:38 AM
Edited Mar 8, 2011 at 12:46 AM

here's the screenshot of the error: (try catch and goto statement's commented out)


Mar 9, 2011 at 3:18 PM

I think what you have there is a filesystem watcher, which notifies you when the filesystem changes.

Then, your OnChanged event tries reading the file that has changed.

I don't know what is changing the zip file, but... it is possible that a file called Input4.zip is being changed, and while it is being changed, your watcher fires.  During the change, the Input4.zip file is not a consistent zip file. Trying to read it, just at that moment, will cause an error.

If I am correct, then the solution is to try to read the zip file only when the changes are complete.  I suppose you could also just ignore errors reading the zip file; that would probably be easiest.


Mar 10, 2011 at 9:49 AM

actually i used filesystem watcher to make a "hot folder" (that's what we call it ;) )

the system checks if there is a new file created/inserted or paste on the the folder then calls the OnChanged event.

the OnChanged event adds the zipfile to an arraylist.  

Mar 10, 2011 at 11:56 AM

Well I think the possibility I described still exists. I don't have much experience with the FileSystemWatcher. Is it possible that you are getting a Changed event before the zip file is whole?

Your "goto here" is a crude approach - you might want to limit the number of retries on the same event, to say... 3.  Also you might want to introduce an artificial delay in the event handler, either before first attempting to read the zip file, or after first failure. Finally, you may be able to avoid the retry completely, if it really is true that the FileSystemWatcher fires more than once for a "Paste" event in that folder.

It will probably take some experimentation. But, because you yourself have observed that the ZipFile.Read() works fine, the second time through, I think you can conclude that the problem is not in the Ionic.Zip library.  It sure seems like a race condition exposed by your code.


Mar 10, 2011 at 11:19 PM

i don't know if it sense if the copying is complete or not...

i will try that artificial timer... 


anyways thanks to you and to everyone who made Ionic.Zip. it's a BIG help :)



Mar 11, 2011 at 11:55 AM

You're welcome, and good luck.

ps: Ionic.Zip was made by one person: Cheeso.