ZipFile.CheckZip fails

May 27, 2011 at 1:35 PM
Edited May 27, 2011 at 1:36 PM

Hi. I have an application where user specifies a directory to zip and if he wants the zip file to be splitted or not. He also specifies the maximum size he wants for the zip file (if the file is bigger than that value, he expects that the zip file will be splitted).

The problem occurrs when the specified maximum size is bigger than the zip file and no split is needed. Everything works fine and the zip file is correctly generated (can open it with WinRAR), but when I use the ZipFile.CheckZip method to check if the generated file can be unzipped latter, this method fails. This is the error message with the stack trace:

Ionic.Zip.ZipException: C:\Users\xxx\Desktop\ is not a valid zip file ---> Ionic.Zip.BadReadException:   ZipEntry::ReadHeader(): Bad signature (0x00000014) at position  0x00000008
   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._InitInstance(String zipFileName, TextWriter statusMessageWriter)
   at Ionic.Zip.ZipFile.Initialize(String fileName)
   --- End of inner exception stack trace ---
   at Ionic.Zip.ZipFile.Initialize(String fileName)
   at Ionic.Zip.ZipFile.CheckZip(String zipFileName, Boolean fixIfNecessary, ReadOnlyCollection`1& messages)
   at Ionic.Zip.ZipFile.CheckZip(String zipFileName)

Again, the error only occurs when maximum size is specified (through MaxOutputSegmentSize property) but the zip file does not reaches this size.

This is the method to create the zip file:

        public static void CreateZipFile(string directoryToZip, string fileName, bool split, int splitSize)
            ZipFile zipFile = new ZipFile();
            zipFile.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;

            if (split)
                zipFile.MaxOutputSegmentSize = splitSize;

            foreach (string directory in Directory.GetDirectories(directoryToZip))
                zipFile.AddDirectory(directory, directory.Substring(directory.LastIndexOf('\\') + 1));

            foreach (string file in Directory.GetFiles(directoryToZip))
                zipFile.AddFile(file, "/");

            zipFile.SaveProgress += new EventHandler<SaveProgressEventArgs>(zipFile_SaveProgress);

Can anyone help me on this?