Decompress file from Boost filtering_streambuf to std::vector<char>?

亡梦爱人 提交于 2019-12-06 15:53:16

The problem is that your ifstream and filtering_streambuf use char as their underlying character type, but your basic_vectorstream uses unsigned char as its value type. The Boost code has a static assertion requiring that these types be the same so that you don't get a spew of compiler errors if you use two different types that are not convertible.

Fortunately, the fix here is easy – change:

basic_vectorstream<std::vector<unsigned char>> vectorStream;
copy(in, vectorStream);
std::vector<unsigned char> chars(vectorStream.vector());

to:

basic_vectorstream<std::vector<char>> vectorStream;
copy(in, vectorStream);
std::vector<unsigned char> chars(
    vectorStream.vector().begin(),
    vectorStream.vector().end()
);

This is safe because char and unsigned char are guaranteed by the C++ standard to have the same object representation (§3.9.1/1).


Unrelated to your direct problem, but you also need to pass std::ios::binary to file's constructor, otherwise you'll have corrupt data due to line-ending conversions.

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