Reconstructing zlib stream

时光怂恿深爱的人放手 提交于 2019-12-11 06:29:39

问题


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.



来源:https://stackoverflow.com/questions/38550897/reconstructing-zlib-stream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!