Jack
Jack

Reputation: 3943

Best .NET Framework compression class?

Yes, I know GZipStream or DeflateStream is the common ones in .NET Framework which handle compression/decompression.

I wish to have compress/decompress functions in my program, but

  1. I wish a .NET Framework C# one, not a 3rd party open source. I can't use because of those copyright restrictions in my program.

  2. GZipStream and DeflateStream are not so good. for e.g., GZipStream compress a file to 480KB while 7Zip compress the same file to the size of 57KB.

Does Microsoft have other good compression methods???

Thanks

Upvotes: 9

Views: 14812

Answers (5)

Luis Bernal
Luis Bernal

Reputation: 21

Well, I try to compress like recursive data, is funny. Check my example:

private byte[] CompressWithLevels(byte[] data)
{
    using(MemoryStream ms = new MemoryStream())
    {
        using(GZipStream gz = new GZipStream(ms, CompressionMode.Compress))
        {
            gz.Write(data, 0, data.Length);
            return ms.ToArray();
        }
    }
}

Now, I try to Compress a file too big, for example:

string path = @"c:\test.bin";
byte[] buffer = File.ReadAllBytes(path);
byte[] level1 = CompressWithLevels(buffer);
byte[] level2 = CompressWithLevels(level1);

Check the size of buffer, level1 and level2.
buffer size is 77683, level1 = 57354 and level2 = 8202...

buffer is 100%, then:
57354 is 73,83%
8202 is 10,55%
so funny.

Upvotes: 1

Yonatan Karni
Yonatan Karni

Reputation: 977

you can also use the open source ZLib (http://www.zlib.net/) with PInvoke, or use a wrapper for it (I've used zlib.net - http://www.componentace.com/zlib_.NET.htm - but I believe it had some bugs). it's less convenient than managed libraries, but more efficient than DeflateStream/GZipStream (which are the same except for an extra CRC in GZipStream).

Upvotes: 0

Stefan Egli
Stefan Egli

Reputation: 17018

There is a managed wrapper for 7zip. The license is LGPL so you can use it in closed source projects. I do not know if this fits your license requirements as you did not state them.

http://sevenzipsharp.codeplex.com/

Upvotes: 2

Cocowalla
Cocowalla

Reputation: 14331

I don't have any statistics regarding compression rates, but I'd been using the SharpZipLib library for years with much success.

Upvotes: 1

Dave Swersky
Dave Swersky

Reputation: 34810

GZipStream and DeflateStream are specifically intended for compressed streams, not general compression of files for storage.

Other than those classes, compression is not built into .NET. If you want high-quality compression you will have to go to a third party library. Check out http://www.7-zip.org/sdk.html for an open-source 7zip library.

Upvotes: 6

Related Questions