views:

154

answers:

2

I'm implementing Deflate and GZip compression for web content. The .NET Framework DeflateStream performs very well (it doesn't compress that good as SharpZipLib, but it is much faster). Unfortunately it (and all other libs i know) miss a function to write precompressed data like stream.WritePrecompressed(byte[] buffer).

With this function it would be possible to insert precompressed blocks in the stream. This could reduce the cpu load for compressing this part and increase the total throughput of the web server.

Is there any managed lib capable of doing this? Or is there any good starting point beyond ZLIB.NET from ComponentAce to do this?

A: 

IIRC the #ZipLib allows you to set the compression level, have you tried flushing the stream and dropping the level to 0 and then sending the already compressed data before raising the compression level again?

If you are only looking at doing this for performance reasons then this might be an acceptable solution.

PlausiblyDamp
This is the Quick and Dirty solution. In fact it works with all libraries as long as you mark the generated block "not the last". you can add simply a new block. it is acceptable if the inserted block is 5.000 bytes or bigger, because the backwards distances are limited to 32.768 bytes and often the algorithms will only use 4.096 bytes for speed reasons.
Thomas Maierhofer
+1  A: 

Another approach is to flush the deflater stream (and possibly also close it), to guarantee that all buffered compressed data is written to the output stream, and then simply write your precompressed data to the underlying output stream, then re-open the deflater stream on top of your output stream again.

Loadmaster