Corruption of zip files

Jan 26, 2010 at 12:33 PM


Using DotNetZip

While adding an entry to an existing zip file containing approx. 9000 entries, my server shutdown unexpectedly.

On reboot, I found out that the original file was corrupt:

Ionic.Zip.BadReadException:   ZipEntry::ReadHeader(): Bad signature (0x00000000) at position  0x00000000
   at Ionic.Zip.ZipEntry.ReadHeader(ZipEntry ze, Encoding defaultEncoding)
   at Ionic.Zip.ZipEntry.ReadEntry(ZipContainer zc, Boolean first)
   at Ionic.Zip.ZipFile.ReadIntoInstance_Orig(ZipFile zf)
   at Ionic.Zip.ZipFile.ReadIntoInstance(ZipFile zf)
   at Ionic.Zip.ZipFile.Read(String fileName, TextWriter statusMessageWriter, Encoding encoding, EventHandler`1 readProgress)
   at Ionic.Zip.ZipFile.Read(String fileName)

In the same directory, I found the temporary file (DotNetZip-XXXXXX). After changing the extension to .zip, I can't seem to open it either. This is unexpected for me. I thought the temporary file was a copy of the zip file, with the new entry. Here is my expected behavior if the system shutdown during Save():

- The original zip file is left untouched, and the DotNetZip-XXXXXX file is corrupt. (stopped while building the new file)


- The original zip file is corrupt, and the DotNetZip-XXXXXX file is okay­. (stopped while overwriting the original file)

Here is the calling code:

                using (ZipFile zip = ZipFile.Read(this.OutputFile))
                        zip.ParallelDeflateThreshold = -1; //Do not multi-thread inner operations.
                        zip.BufferSize = 20480;
                        zip.CompressionLevel = CompressionLevel.BestCompression;
                        zip.UseZip64WhenSaving = Zip64Option.AsNecessary;
                        zip.SaveProgress += this.SaveZipHandler;
                        foreach (var entry in entries)
                            //Simply queue the right amount of entries.
                            zip.AddEntry(entry.EntryName, Stream.Null);
                        zip.SaveProgress -= this.SaveZipHandler;

SaveZipHandler opens a Stream to the file before saving each entry.

Please tell be how DotNetZip is supposed to manage transactional zipping, and what is the behavior is the system shuts down during the save.

Many thanks,



Jan 26, 2010 at 10:08 PM

I don't know exactly what happened, but:

- your expectation is correct regarding the behavior during savve. 

- the normal savve operation is for DotNetZip to save into a temp file, then rename the temporary file to the permanent name.  But, If a problem occurs during the rename, you may be left with nothing.  In other words there is a window of vulnerability, after the new zipfile has been written, and before the rename is complete.  If you can't tolerate that vulnerability, then you should save to a new file name, and do the transactional rename yourself.

- the exception message indicates to me that the original zip file was corrupt, before you tried to Save.  Reading 00000000 at position 00000000 indicates either an empty file, or a file with all zeroes.   This can happen if you run out of disk space or quota. 

- If the problem happened before the save, as your exception indicates, then I don't know why you had a temp file there.  it must have been left over from a prior error.