如何解压gzip流
我想通过 gzip 压缩的网络发送数据。数据从 C# 服务器以块的形式发送,该服务器在发送之前使用流压缩器对其进行压缩。我希望 C++ 客户端能够在数据传入时对其进行解压缩并将其写入文件。我在互联网上查了很多,但到目前为止还没有想出一个好的解决方案。我发现执行此操作的方法似乎不会将其解压缩为流,而是将其解压缩为整个字符数组,这是一个问题,因为数据占用的内存量。
I would like to send data over a network which is gzip compressed. The data is sent in chunks from a C# server which compresses it using a stream compressor before it sends. I would like the C++ client to be able to decompress the data as it comes in and write it to a file. I have looked alot over the internet but have not come up with a good solution as of now. Methods I have found of doing this don't seem to decompress it as a stream but rather as an entire char array, which is an issue because of the amount of memory the data takes up.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这正是 zlib 的用途。它是一个 C 库,支持一次解压缩任意大小的块,提供迄今为止可以提取的尽可能多的未压缩数据。
This is exactly what zlib is for. It is a C library that supports decompressing whatever size chunks at a time you like, delivering as much uncompressed data as can be extracted so far.
未经测试,但我将启动 gzip 作为子进程,由其标准输入提供数据,然后您从其标准输出中读取解压缩的流。
您需要将源流重定向到
gzip
的stdin
,或者从程序中“手动”提取它。如何启动子进程并挂钩其标准流通常至少部分依赖于平台,因此我们需要详细信息才能获得更详细的答案。
例如,如果您在 Windows 或 Linux 上使用 C++,则将数据“管道”到子进程会有点不同,如果您使用 Qt 等框架,情况会更加不同。
Not tested, but I would launch
gzip
as a subprocess, fed by its standard input, and you read from its standard output the decompressed stream.You need to either redirect the source stream to
gzip
'sstdin
, or to pump it "manually" from your program.How you launch a subprocess and hook its standard streams is usually at least partially platform-dependent, so we'll need details for a more detailled answer.
For example, if you use C++ on Windows or on Linux, "piping" data to a subprocess is a bit different, and it's even more different if you use a framework like Qt.