Flash Sync for Zlib Deflate

I need a zlib deflate compressed stream. In my implementation, I have to use one thread throughout the session. During this session, small small pieces of data will be transmitted through the compressed stream. Each time a piece is handed over, it must be sent immediately in a concise form.

My first attempt was to use DeflateStream, but when I send the first fragment, its compressed data will not appear until I close the stream.

Reading about zlib flush modes , it looks like there is some kind of specific mode for what I need.

  • Am I using the correct class (DeflateStream) to compress sllate zlib?
  • How can I enable the "sync flush" behavior?
+3
source share
3 answers

The DotNetZip project has a Zlib submodule that contains an implementation of DeflateStream.

This implementation has another property called 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.
+2
source

This is really just a brief close. You will need to use a different instance of DeflateStream each time, passing true to the overloaded constructor , telling it not to close the underlying thread when you close DeflateStream.

0
source

, "sync flush", zpipe.c zlib.
1- ,

Deflate()will be returned every time the output buffer is full or when the input buffer is empty, meanwhile adding to the compressed stream is an empty literal, also called "sync flush", except for the end, which is the flag Z_FINISH.

    flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
    flush = feof(source) ? Z_FINISH : Z_SYNC_FLUSH;
    ret = deflate(&strm, flush);
0
source

Source: https://habr.com/ru/post/1777308/


All Articles