Search code examples
c#c#-4.0sharpziplib

Why the size of compressed file is bigger than uncompressed in SharpZipLib?


I've a super weird problem. I use SharpZipLib library to generate .zip. I found that the size of .zip is a little bit bigger than total of my text files. I don't know what's wrong. I also tried to google but I cannot find any related to my case. This is my code :

    protected void CompressFolder(string[] files, string outputPath)
    {
        using (ZipOutputStream s = new ZipOutputStream(File.Create(outputPath)))
        {
            s.SetLevel(0);
            foreach (string path in files)
            {
                ZipEntry entry = new ZipEntry(Path.GetFileName(path));
                entry.DateTime = DateTime.Now;
                entry.Size = new FileInfo(path).Length;
                s.PutNextEntry(entry);
                byte[] buffer = new byte[4096];
                int byteCount = 0;
                using (FileStream input = File.OpenRead(path))
                {
                    byteCount = input.Read(buffer, 0, buffer.Length);
                    while (byteCount > 0)
                    {
                        s.Write(buffer, 0, byteCount);
                        byteCount = input.Read(buffer, 0, buffer.Length);
                    }
                }
            }
        }
    }

Solution

  • The compression level of 0 signals SharpZipLib to store instead of compress.

    The larger size is because of the Zip metastructure (filenames etc)

    Your solution would then be to change the level to something higher, ie:

    s.SetLevel(9);