RemoveEntry throws exception

Oct 16, 2008 at 3:14 AM
When I try the following code, I get an exception:

Dim zip As New Ionic.Utils.Zip.ZipFile()
Dim entry1 As Ionic.Utils.Zip.ZipEntry = zip.AddFile("c:\test1.txt")
zip.AddFile("c:\test2.txt")
zip.AddFile("c:\test3.txt")
zip.Save("c:\test.zip")
zip.RemoveEntry(entry1) '<-- This throws "Object reference not set to an instance of an object." but entry1 is not nothing
zip.RemoveEntry("c:\test1.txt") '<-- this throws "The entry you specified was not found in the zip archive." even though the code above shows it is in there

I'm using 1.6.3.7.

Any ideas?

Coordinator
Oct 16, 2008 at 3:29 AM
Let me look.
Coordinator
Oct 16, 2008 at 5:29 AM
Edited Oct 16, 2008 at 5:34 AM
Bug.  The entry1 goes stale across the Save().  I filed a workitem (6442), and fixed it. 
I also updated the v1.6 release downloads with new binaries that include this fix (v1.6.3.8). 
 
ps: I forgot to say:
With this fix, after you call RemoteEntry(entry1),
the succeeding call to RemoveEntry("C:\test1.txt") will fail, because that entry is gone.

you can remove an entry only once.
Oct 16, 2008 at 6:09 AM
Edited Oct 16, 2008 at 10:55 AM
Wow, you are fast!

I have the same problem even if Save() is not called. Remove that line and try the rest of the code, you should see it then.

P.S. The original code where I am removing the same file twice was simply to show that either attempt will fail. I don't expect to be able to delete a deleted entry. :-)

Coordinator
Oct 16, 2008 at 4:06 PM
Edited Oct 16, 2008 at 4:15 PM

Hmmm, You cannot extract if you don't call Save().
I have to think about the Remove() if you don't call save.  
Let me look.

This code does not throw for me, using 1.6.3.8

            Dim zip As New Ionic.Utils.Zip.ZipFile()
            Dim entry1 As Ionic.Utils.Zip.ZipEntry = zip.AddFile("file1.txt")
            zip.AddFile("file2.txt")
            zip.AddFile("file3.txt")
            ' zip.Save(ZipToCreate)
            zip.RemoveEntry(entry1)

Is that what you mean?

Oct 16, 2008 at 4:19 PM
OK, let me try to describe more about what I'm doing.

I want to add several files, save (so I can get the compressed file size), then I want to delete one or more files from the zip archive (and re-save to get the new compressed size). Right now, it seems like I have to create a new ZipFile and re-add all files except those I wanted to remove.

I have not even gotten to the extraction part, yet. As I understand, the DeleteEntry method will delete an entry from the archive but will not extract that file anywhere.

My point is that the DeleteEntry causes problems whether or not you first call Save (since you said the Save method was invalidating object references to entries).

Am I misunderstanding something or is there really a bug trying to delete files you have added to an archive?
Coordinator
Oct 16, 2008 at 4:35 PM
Edited Oct 16, 2008 at 5:19 PM

can you show me the code?
The code I showed you - it seems to do what you described. 
It works for me. (using v1.6.3.8)
Does it work for you?

            Dim zip As New Ionic.Utils.Zip.ZipFile()
            Dim entry1 As Ionic.Utils.Zip.ZipEntry = zip.AddFile("file1.txt")
            zip.AddFile("file2.txt")
            zip.AddFile("file3.txt")
            zip.Save(ZipToCreate)

            ListFiles()

            zip.RemoveEntry("c:\file1.txt")
            zip.Save(ZipToCreate)

            ListFiles()

The above code snippet works for me.  What do you get when you compile and run this?

Oct 17, 2008 at 3:07 AM
I just tested it with 1.6.3.9 and the code works great now. Thanks!