Mr. Monster Mouth
Creative Commons License photo credit: massdistraction

I’m writing this post since I’ve found a lot of people having similar problems, so hopefully it will be useful to someone.

I’ve had Linux/Mac people reporting that the ZIP package of GroundTruth which I released yesterday could not be extracted. The package was created programmatically using the SharpZibLib, an open source .NET ZIP library. I myself did not detect any problems, it works OK on my Vista machines.

I’ve done some investigation (Google, inspecting other open source projects…) and found a small hint what could be the culprit – I wasn’t setting the ZipEntry.Size property when I was adding files to the package. So here I’m posting a code snippet on how to create a ZIP package with selectively added files:

using (FileStream zipFileStream = new FileStream(
    zipFileName,
    FileMode.Create,
    FileAccess.ReadWrite,
    FileShare.None))
{
    using (ZipOutputStream zipStream = new ZipOutputStream(zipFileStream))
    {
        byte[] buffer = new byte[1024 * 1024];

        foreach (string fileName in filesToZip)
        {
            string cleanedFileName = ZipEntry.CleanName(fileName);

            using (FileStream fileStream = File.OpenRead (fileName))
            {
                ZipEntry entry = new ZipEntry (cleanedFileName);
                entry.DateTime = File.GetLastWriteTime(fileName);
                entry.Size = fileStream.Length;
                zipStream.PutNextEntry (entry);

                int sourceBytes;

                while (true)
                {
                    sourceBytes = fileStream.Read(buffer, 0, buffer.Length);

                    if (sourceBytes == 0)
                        break;

                    zipStream.Write(buffer, 0, sourceBytes);
                }
            }
        }
    }
}