Search code examples
.netcompressiondotnetzip

DotNetZip not compressing my string


I am using DotNetZip version 1.8.4.5. I cannot upgrade at this time.

I am sending a large List to my client application from my server. I am serializing my List to a string variable (contentsAsString). I then compress contentsAsString into a MemoryStream and pass the byte array back to my client. My compressed byte array length is 2087188 (1.99 MB).

If I take the value of contentsAsString and save it to a text file, the file is 1.99 MB (same as above). If I use Windows Compression and compress the text file, the resulting zip file is 132 KB.

Why is my MemoryStream not compressing to 132 KB? Here is the code I am using:

   private byte[] zipContents<T>(List<T> contents)
    {

        using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
        {
            using (MemoryStream ms = new MemoryStream())
            {
                string contentsAsString = Utility.SerializeListToString<T>(contents);
                zip.AddEntry("stream.zip", null, contentsAsString);
                zip.Save(ms);
                return ms.ToArray();
            }

        }

    }

Solution

  • As I mentioned in the comment, using the latest 1.9 version of DotNetZip was compressing as expected with me. Could not find 1.8.4.5 to test out, so I think it is best to upgrade.

    Thank you.