views:

37

answers:

1

I keep track of the original size of the files that I'm compressing using .Net's GZipStream class, and it seems like the file that I thought I was compressing has increased in size. Is that possible?

This is how I'm doing the compression:

Byte[] bytes = GetFileBytes(file);

using (FileStream fileStream = new FileStream("Zipped.gz", FileMode.Create))
{
    using (GZipStream zipStream = new GZipStream(fileStream, CompressionMode.Compress))
    {
        zipStream.Write(bytes, 0, bytes.Length);
    }
}
+3  A: 

Yes, it can. It has been fixed in .NET 4.

The compression algorithms for the System.IO.Compression..::.DeflateStream and System.IO.Compression..::.GZipStream classes have improved so that data that is already compressed is no longer inflated. This results in much better compression ratios. Also, the 4-gigabyte size restriction for compressing streams has been removed.

Check: GZipStream/DeflateStream increase file size on compression

Also check here SO: GZipStream and DeflateStream produce bigger files

Nick Martyshchenko
Note however that any compression scheme that is able to compress some input data will also certainly produce bigger files for some other input data (check on the pidgeon hole principle).
gusbro
Yes, of cause. Basicly any compression scheme rely on fact that data is redundant so can be reduced through use of specific encoding schemes. But this is not always true, e.g. if you try to compress file with a random data in it or already compressed with good compressor. But original question was about particular compressor implementation (not about compression algorithm itself). Good compressor implemetation can fallback to store file without compression at all so almost not increasing its size. I don't think that Mike ask about result files increased to a few header's bytes.
Nick Martyshchenko