Reconstructing zlib stream

2019-07-23 09:22发布

问题:

My project contains a Sender/Receiver framework that can only talk one-way , there is no a return channel from the receiver to the sender. The sender compress part of the data it's send to the receiver using zlib.

I want to make my receiver resilient to crashes/reboots/restarts, is it possible to join the zlib stream from a random point somehow?

Both Sender/Receiver using Z_SYNC_FLUSH.

Some ideas I had:

  1. Saving state structures to disk and reload them after restart of the receiver.
  2. Replacing Z_SYNC_FLUSH to Z_FULL_FLUSH.

I tried saving the first chunk of zlib compressed data, restart the receiver and than resend the first chunk again and after that continue the stream from a random chunk and it seems to work - I don't understand why, is it a solid solution or it was just a luck?

Replacing to Z_FULL_FLUSH didn't seem to make any difference.

Is there another way to work around this? Do you think I missed something?

Thanks a lot,
Jason

回答1:

To assure that you can start decompression at some point with no history, you must either use Z_FULL_FLUSH or simply end the stream and start a new one.

For the former you could do a Z_SYNC_FLUSH followed by a Z_FULL_FLUSH in order to insert two markers resulting in the nine bytes 00 00 ff ff 00 00 00 ff ff, which would be unlikely to be seen randomly in the compressed data. You can do the same for the latter, simply inserting a large-enough marker between the end of the previous zlib stream and the start of the next zlib stream.