DotNetZip not compressing my string
-
10-10-2019 - |
Pergunta
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();
}
}
}
Solução
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.
Outras dicas
Note that if the GZIP or Deflate compressions would be also suitable for you, .NET Framework (2.0+) provides GZIP and Deflate compressions for streams (found in the System.IO.Compression namespace)
I wasn't aware of such a cool library, but looking at their reference I've found something about the compression method.
http://cheeso.members.winisp.net/DotNetZipHelp/html/f52f1f79-0eed-92c8-5938-89ccfc77ca53.htm
Does that help at all?
Also I've found this: http://www.java2s.com/Open-Source/CSharp/Development/DotNetZip/Ionic/Zip/Tests/Compatibility.cs.htm