I wanted to compress some data so i thought i’d run the stream by deflate
It went from 304 bytes to 578. Thats 1.9x larger. I was trying to compress it…..
What am i doing wrong here?
using (MemoryStream ms2 = new MemoryStream())
using (var ms = new DeflateStream(ms2, CompressionMode.Compress, true))
{
ms.WriteByte(1);
ms.WriteShort((short)txtbuf.Length);
ms.Write(txtbuf, 0, txtbuf.Length);
ms.WriteShort((short)buf2.Length);
ms.Write(buf2, 0, buf2.Length);
ms.WriteShort((short)buf3.Length);
ms.Write(buf3, 0, buf3.Length);
ms.Flush();
result_buf = ms2.ToArray();
}
The degree to which your data is expanding is a bug in the DeflateStream class. The bug also exists in the GZipStream class. See my description of this problem here: Why does my C# gzip produce a larger file than Fiddler or PHP?.
Do not use the DeflateStream class provided by Microsoft. Use DotNetZip instead, which provides replacement classes.
Incompressible data will expand slightly when you try to compress it, but only by a small amount. The maximum expansion from a properly written deflate compressor is five bytes plus a small fraction of a percent. zlib‘s expansion of incompressible data (with the default settings for raw deflate) is 5 bytes + 0.03% of the input size. Your 304 bytes, if incompressible, should come out as 309 bytes from a raw deflate compressor like DeflateStream. A factor of 1.9 expansion on something more than five or six bytes in length is a bug.