I've discovered that the GZip compression built into C# (System.IO.Compression.GZipStream) doesn't really do that good a job at compressing data. I heard (cannot confirm) that MS had to tread lightly around some patents which results in pretty poor compression ratios. I needed to compress some data, and compress it well, as I had a whole bunch of data files from a reporting project totalling up to 5gigs. So the GZipStream wouldn't do. I then tried DotNetZip which had much the same problems of low compression ratios. Finally I stumbled across Peter Bromberg's example of how to use the 7-Zip C# libraries (totally managed code - BIG PLUS), so here is an example project I did using it for your reference. This is a C# / .Net console app, but the code will work the same for ASP.Net too. Basically there are two helper functions (compress / decompress) that take a byte array (byte[]) and return a byte array. Too simple! Obviously not a Java library (just kidding, dont flame me!). And the compression ratio is much better than the others. For my already-optimised binary data, it came to 40% of the original size. Download MySevenZipper example project The gist of how I came up with this is, you download the latest 7-Zip SDK, just extract the C# bits (the CS folder). Then delete the CS\7Zip\Compress\LzmaAlone folder, as it contains a Main function and a solution file and some other junk you dont need. Then go to Peter Bromberg's helper code, download it and find the 'SevenZipHelper.cs' file, it's all you need. Then copy all these files into one 7Zip folder and include it in your C# project and use it as shown below:
// Some random text to compress, thanks to http://en.wikipedia.org/wiki/V8
string OriginalText =
@"From A V8 engine is a V engine with eight cylinders mounted on the crankcase
in two banks of four cylinders, in most cases set at a right angle to each other
but sometimes at a narrower angle, with all eight pistons driving a common crankshaft.
In its simplest form, it is basically two straight-4 engines sharing a common
crankshaft. However, this simple configuration, with a single-plane crankshaft,
has the same secondary dynamic imbalance problems as two straight-4s, resulting
in annoying vibrations in large engine displacements. As a result, since the 1920s
most V8s have used the somewhat more complex crossplane crankshaft with heavy
counterweights to eliminate the vibrations. This results in an engine which is
smoother than a V6, while being considerably less expensive than a V12 engine.
Racing V8s continue to use the single plane crankshaft because it allows faster
acceleration and more efficient exhaust system designs.";

// Convert the text into bytes
byte[] DataBytes = ASCIIEncoding.ASCII.GetBytes(OriginalText);
Console.WriteLine("Original data is {0} bytes", DataBytes.Length);

// Compress it
byte[] Compressed = SevenZip.Compression.LZMA.SevenZipHelper.Compress(DataBytes);
Console.WriteLine("Compressed data is {0} bytes", Compressed.Length);

// Decompress it
byte[] Decompressed = SevenZip.Compression.LZMA.SevenZipHelper.Decompress(Compressed);
Console.WriteLine("Decompressed data is {0} bytes", Decompressed.Length);

// Convert it back to text
string DecompressedText = ASCIIEncoding.ASCII.GetString(Decompressed);
Console.WriteLine("Is the decompressed text the same as the original? {0}",
  DecompressedText == OriginalText);

// Print it out
Console.WriteLine("And here is the decompressed text:");
Console.WriteLine(DecompressedText);

Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.

Chris Hulbert

(Comp Sci, Hons - UTS)

iOS Developer in Sydney.

I have worked at places such as Google, Cochlear, News Corp, Fox Sports, NineMSN, FetchTV, Woolworths, and Westpac, among others. If you're looking for a good iOS developer, drop me a line!

Get in touch:
[email protected]
github.com/chrishulbert
linkedin
my resume



 Subscribe via RSS