“同步刷新”对于 Zlib 放气

发布于 2024-10-04 10:26:03 字数 357 浏览 5 评论 0原文

我需要一个 zlib deflate 压缩流。在我的实现中,我必须在整个会话中使用单个流。在此会话期间,小块数据将通过压缩流传递。每次传递一个块时,都必须立即以压缩形式发送。

我的第一次尝试是使用 DeflateStream,但是当我发送第一个块时,它的压缩数据不会出现,直到我关闭流。

阅读zlib刷新模式,似乎有一种特定的模式我需要。

  1. 我是否使用正确的类(DeflateStream)进行 zlib deflate 压缩?
  2. 如何启用“同步刷新”行为?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少年亿悲伤 2024-10-11 10:26:03

DotNetZip 项目有一个子模块 Zlib,其中包含自己的 DeflateStream 实现。

此实现还有另一个名为 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.

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.
青春如此纠结 2024-10-11 10:26:03

它确实只在关闭时刷新。您每次都需要使用不同的 DeflateStream 实例,将 true 传递给 重载的构造函数告诉它在关闭 DeflateStream 时关闭底层流。

It does indeed only flush upon close. You will need to use a different DeflateStream instance each time, passing true to the overloaded constructor telling it not to close the underlying stream when you close the DeflateStream.

眼角的笑意。 2024-10-11 10:26:03

要回答有关如何启用“同步刷新”行为的问题,您应该查看 zlib 源代码中的 zpipe.c 示例。
将第一行替换为如下所示的第二行

Deflate() 将在每次输出缓冲区已满时返回
或者当输入缓冲区为空时同时附加到
压缩流 一个空的文字块,也称为
“同步刷新”,除了最后的 Z_FINISH 标志。

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

To answer your question about how you could enable "sync flush" behavior, you should see the zpipe.c example in the zlib source code.
Replace the 1st line with the 2nd line shown below

Deflate() will return each time the output buffer is full
or when the input buffer is empty meanwhile appending to the
compressed stream an empty literal block also called a
"sync flush" except at the end which is the Z_FINISH flag.

    flush = feof(source) ? Z_FINISH : Z_NO_FLUSH;
    flush = feof(source) ? Z_FINISH : Z_SYNC_FLUSH;
    ret = deflate(&strm, flush);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文