Summary
The .Net-Technology features the very rich and useful FCL (Foundation Class Library) which covers almost every aspect of programming. Its quite surprising that there are no classes for easy and fast compression.
LZO.Net brings the power of Markus "FXJ" Oberhumer's great LZO compression library (V1.08) to .Net. It wraps the access to the native DLL with a small C# class maintaining the raw speed of the ANSI-C library.
Installation
The precompiled LZO.Net was build and tested with .NET-Framework 1.1 but should also work with 1.0.
For using LZO.Net in your application you need to reference the wrapper assembly Simplicit.Net.Lzo from the lib-Directory in your project. Copy the native lzo.dll right beside the assembly.
Usage
There is only one class you can use: LZOCompressor and it has only two methods: Compress and Decompress. The following example in C# shows how easy it is to use it.
// Create the compressor object
LZOCompressor lzo = new LZOCompress();
// Build a quite redundant string
StringBuilder sb = new StringBuilder();
for(int i = 0; i < 10000; i++) {
sb.Append("LZO.NET");
}
string str = sb.ToString();
Console.WriteLine("Original-Length: " + str.Length);
// Now compress the 70000 byte string to something much smaller
byte[] compressed = lzo.Compress(Encoding.Default.GetBytes(str));
Console.WriteLine("Compressed-Length: " + compressed.Length);
// Decompress the string to its original content
string str2 = Encoding.Default.GetString(lzo.Decompress(compressed));
Console.WriteLine("Decompressed-Length: " + str2.Length);
Console.WriteLine("Equality: " + str.Equals(str2));Output:
Original-Length: 70000
Compressed-Length: 297
Decompressed-Length: 70000
Equality: True
The most significant thing to say here is that the compressor only works with raw bytes, it doesn't know the concept of Unicode characters. So we must feed it with the byte representation of the string we want to compress. This is done by Encoding.Default.GetBytes(string). The opposite call is GetString(byte[]).
That's it ! Happy compressing ...