Search code examples
c#zlibflushdeflatedeflatestream

"Sync flush" for Zlib Deflate


I need a zlib deflate compressed stream. In my implementation I must use a single stream over the entire session. during this session small chunks of data will be passed through the compressed stream. Everytime a chunk is passed it must be sent in compressed form immediately.

My first attempt was using DeflateStream but when I send the first chunk its compressed data wont appear until I close the stream.

Reading about zlib flush modes it appears as if there is one specific mode for what I need.

  1. Am I using the correct class(DeflateStream) for zlib deflate compression?
  2. How can I enable "sync flush" behavior?

Solution

  • The DotNetZip project has a submodule Zlib that contain an implementation of DeflateStream of its own.

    This implementation has another property named FlushMode:

    DeflateStream deflate = new DeflateStream(stream, CompressionMode.Compress);
    deflate.FlushMode = FlushType.Sync;
    deflate.Write (data, 0, data.Length);
    //No call to deflate.Flush() needed, automatically flushed on every write.