简体   繁体   中英

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 eg, 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

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.

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/

我没有关于压缩率的任何统计数据,但我多年来一直在使用SharpZipLib库并取得了很大的成功。

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM