tags:

views:

133

answers:

5

Hello,

I am looking for some recommendations about compressing data in .NET, aside from using the GZipStream class. I am looking for fast and high compression of byte arrays to be able to send them via tcp.

Thank you.

+3  A: 

SharpZipLib is an alternative. It's said that it's written more thoughtfully than the framework library GZipStream

BC
I will definitely check it out - thanks
GX
+2  A: 

DotNetZip offers native support and has a quite friendly API and is my opinion more flexible than SharpZipLib:

DotNetZip

0xA3
+4  A: 

If you are compressing data, then you might look at high-density serialization, rather than compression. Something like protobuf. There are a few C# implementations here. For existing objects, protobuf-net is IMO the simplest to implement (disclosure: I'm the author - but it is free etc). You just serialize to the stream, or if you want a byte[], a separate MemoryStream.

For continuous use over a socket (rather than the discreet request/response of HTTP), I would suggest looking at the Serializer.SerializeWithLengthPrefix / Serializer.DeserializeWithLengthPrefix operations (protobuf doesn't itself include a terminator, so a length-prefix is necessary to handle separate messages).

Marc Gravell
very interesting - i will give it a try. thanks
GX
Marc, what does "high-density serialization" mean, especially in contrast to compression? Does it make use of any compression scheme or is it just an efficient way to represent data? Could you give a short explanation or any pointer to read on?
0xA3
@0xA3 - what I mean is: if this is typical app data (that you might represent as xml or JSON), then use a different serializer to write the same data in less bytes. So instead of `<AccountId>123</AccountId>`, write 2 or 3 bytes instead. Not suitable for *arbitrary* `byte[]` though.
Marc Gravell
Thanks a lot :-)
0xA3
+1  A: 

.NET 3+ has built-in Zip support now, with the ZipPackage class.

Joel Mueller
Unfortunately, this only offers some sort of zip support. It allows you to create zip-based "packages", but these packages are not pure zip files but packages as described by the OpenPackaging Conventions (OPC-format). These packages consist of PackagePart and PackageRelationship elements. This format is probably best known as the container format used by the Office OpenXML format.
0xA3
+1  A: 

LZMA is supposed to be among the best for compression. 7-Zip is a public domain SDK implementation of LZMA, freely downloadable here:

http://www.7-zip.org/sdk.html

Wikipedia on compression algorithms:

7z's LZMA algorithm reaches a higher compression ratio than RAR, except for "multimedia" files like .wav and .bmp files where RAR uses specialized routines that outperform LZMA.[7] Other free compression software such as NanoZip and FreeArc usually outperform WinRAR.[8]

code4life
Im a big fan of 7-zip but not sure how accessible the api is.
James Westgate
The link points to LZMA SDK which has a C# API, and is public domain.
code4life