在c++中使用deflestream

Using DeflateStream in C++?

本文关键字:deflestream c++      更新时间:2023-10-16

我目前正在尝试将一些涉及使用DeflateStream的c#代码移植到没有。net框架支持的标准c++中。这样的函数的一个例子是:

public static byte[] ReadCompressed(this Stream stream)
{
    var reader = new BinaryReader(stream);
    int len = reader.ReadInt32();
    var array = new byte[len];
    var ds = new DeflateStream(stream, CompressionMode.Decompress);
    ds.Read(array, 0, len);
    ds.Close();
    return array;
}

只是想知道,是否有一种简单的方法将上述代码移植到c++中?谢谢!

您可能想使用zlib。在c++中最简单的方法是使用Boost包装器。

我不完全确定你的例子做了什么,但这里是如何读取zlib压缩文件并将其内容写入标准输出(改编自文档中的示例):

namespace io = boost::iostreams;
std::ifstream file("hello.z", std::ios_base::binary);
io::filtering_streambuf<io::input> in;
in.push(io::zlib_decompressor());
in.push(file);
io::copy(in, std::cout);