Deflate Compression Stream where pre compressed Data can be inserted. Does a .NET lib exist?

試著忘記壹切 提交于 2019-12-01 11:13:33

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.

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.

Yes, you can insert precompressed blocks in to a zlib stream. Start with the zpipe.c example in the zlib source. Only where you want to insert your precompressed block, replace Z_NO_FLUSH with Z_FULL_FLUSH (otherwise don't use Z_FULL_FLUSH because the compression ratio will suffer.)

Now the compressed output is byte aligned and the last deflate block is closed. Full flush means that the next block past the precompressed block cannot contain any back references.

Append your precompressed block to the output stream (e.g. memcpy). Advance strm.next_out to the next empty byte. Continue with deflate where you left off.

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