This project is read-only.

OverflowException when accessing zip file twice


Hello everybody,

I started using DotNetZip in my application but I have problem when I want to use a zip file twice within my application. For example, first I read my zip file and display the content in my application.
using (ZipFile zip = ZipFile.Read(zipPath))
Then later, I want to extract the same zip file, therefore I use the same code above and read the zip file into 'zip'. But then I receive an OverflowException:
Zip File Error: Cannot read that as a ZipFileSystem.OverflowException: overflow while creating filename
   at Ionic.Zip.ZipEntry.CopyHelper.AppendCopyToFileName(String f)
   at Ionic.Zip.ZipEntry.ReadDirEntry(ZipFile zf, Dictionary`2 previouslySeen)
   at Ionic.Zip.ZipFile.ReadCentralDirectory(ZipFile zf)
   at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf) ; File
When I close the application and start again, I can do the first step again but not the second. This happens to me every time I start reading to a zip file the second time.

Any idea why I get this exception? Thanks in advance.


janba wrote May 3, 2013 at 10:07 AM

I'm experiencing exactly the same problem. In my case though I'm trying to read 3 zips (a test sample really) and only one of them fails. As such I looked into it a bit closer and here are some results:
  • The file name is irrelevant. I renamed it even to and it still doesn't work.
  • Renaming the file between the first and second scan (I'm iterating through a folder) does not change anything, there's still an exception on the 2nd pass
So, if it's not the file name of the zip, it sounds like it must be the file names within the zip. And yep, that's it. Here's what's causing it:

Duplicate file names inside the zip. For some bizarre reason the zip file causing this exception had two files with the same name (both in the root of the zip) inside. Now, I'd understand there being an exception for this, but if there is it should happen on both passes, not just the second and consecutive. Please make this behavior consistent and either throw an exception always or never. I vote for never as reading the zip works fine despite the duplicates, it's just extracting it that would cause problems.

wrote May 3, 2013 at 10:07 AM

BiggRedd wrote Nov 5, 2013 at 7:07 PM

I am having a similar problem - my exception error is a little bit different though -

Ionic.Zip.ZipException: Cannot read that as a ZipFile ---> Ionic.Zip.BadReadException: Bad signature (0x1ED19BC7) at position 0x002248E8
at Ionic.Zip.ZipEntry.ReadDirEntry(ZipFile zf, Dictionary`2 previouslySeen)

I have a C# program that is a "Restore". It looks for a Backup zip file both on the PC where you're running the Restore and on a Server PC to which the client is connected. I never get this error when selecting a Backup file on the Client PC, but when we select from the Server PC, we get this error on a semi-regular basis. The problem is, we almost always want to use the zip file on the Server PC, because the program is run when a Client fails and we put a new Client in its place. The weird thing is, in testing this, I've run a Backup which creates these zip files and places it in a directory on the Client as well as FTPs it to the Primary - so the EXACT SAME FILE is on both PCs. And the program works fine if I choose the zip file on the Client, but gives me that error if I use the zip file on the Server. Why would it work fine on one zip file, but not on the other, if they are IDENTICAL???

wrote Jun 9, 2014 at 7:56 AM

Igulator wrote Aug 24 at 9:25 AM

I found the cause and a workaround for this problem.
Ionic.Zip.ZipEntry.CopyHelper.AppendCopyToFileName is called when duplicate filenames are present in the zip file, and attaches "(copy 1)", "(copy 2)" ... to the filenames. I guess to prevent infinite loops there is a hard limit checked at the beginning of the method:
if (callCount > 25) 
    throw new OverflowException("overflow while creating filename");
But there is a bug: callCount is a static variable which is never reset, so after you opened zip files with 25 duplicate entries in total, opening any other zip file with at least one duplicate file throws this exception.

A workaround is to reset this counter via reflection, for example:
Type copyHelper = typeof(ZipEntry).Assembly.GetTypes().FirstOrDefault(t => t.FullName == "Ionic.Zip.ZipEntry+CopyHelper");
if (copyHelper != null)
    FieldInfo callCount = copyHelper.GetField("callCount", BindingFlags.Static | BindingFlags.NonPublic);
    if (callCount != null)
        callCount.SetValue(null, 0);