§ ¶And I thought my implementation of Deflate was bad, part 2
A few years ago I posted about poor compression ratios from the .NET Framework's System.IO.Compression library, more specifically files getting larger when going through compression using the .NET Framework 2.0 library. Fast forward to the more recent .NET Framework 4.0, where the changelog includes the following:
Compression Improvements
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.
Uh huh. Let's test that, shall we? :)
We'll use a revised version of the C# test program I used the last time, one which feeds an entire buffer to the compressor. This is necessary since GZipStream is unfortunately sensitive to write block sizes:
(Read more....)using System;
using System.IO;
using System.IO.Compression;namespace VCZipTest {
class Program {
static void Main(string[] args) {
using (FileStream src = new FileStream(args[0], FileMode.Open)) {
using (FileStream dst = new FileStream(args[1], FileMode.Create)) {
using (GZipStream def = new GZipStream(dst, CompressionMode.Compress, true)) {
byte[] buf = new byte[(int)src.Length];
int act = src.Read(buf, 0, buf.Length);
def.Write(buf, 0, act);
def.Flush();
}System.Console.WriteLine("{0} ({1} bytes) -> {2} ({3} bytes)", args[0],
src.Length, args[1], dst.Length);
}
}
}
}
}