从升压filtering_streambuf DECOM preSS文件为std ::矢量&lt;&字符GT ;? [英] Decompress file from Boost filtering_streambuf to std::vector<char>?
问题描述
我想DECOM preSS使用deflate算法和东西它变成一个矢&lt这是COM pressed文件; unsigned char型&GT;
。从调研至今我所完成的,这似乎是我可以使用的boost ::输入输出流:: filtering_streambuf
,然后用的boost ::输入输出流::复制()
来得到它变成一个的boost ::进程间:: basic_vectorstream&LT;的std ::矢量&lt; unsigned char型&GT;&GT;
,然后拉底层矢量出vectorstream的。不过,我得到一吨的编译器错误的,首先是这样的:
I'm trying to decompress a file that was compressed using the DEFLATE algorithm and stuff it into a vector<unsigned char>
. From the research I've done so far, it seemed like I could use a boost::iostreams::filtering_streambuf
and then use boost::iostreams::copy()
to get it into a boost::interprocess::basic_vectorstream<std::vector<unsigned char>>
and then pull the underlying vector out of the vectorstream. However, I'm getting a ton of compiler errors, starting with this:
/usr/include/boost/iostreams/copy.hpp: In function ‘std::streamsize boost::iostreams::detail::copy_impl(Source, Sink, std::streamsize) [with Source = boost::reference_wrapper<boost::iostreams::filtering_streambuf<boost::iostreams::input> >, Sink = boost::reference_wrapper<boost::interprocess::basic_vectorstream<std::vector<unsigned char> > >, std::streamsize = long int]’:
/usr/include/boost/iostreams/copy.hpp:245:79: instantiated from ‘std::streamsize boost::iostreams::copy(Source&, Sink&, std::streamsize, typename boost::enable_if<boost::iostreams::is_std_io<Source> >::type*, typename boost::enable_if<boost::iostreams::is_std_io<Sink> >::type*) [with Source = boost::iostreams::filtering_streambuf<boost::iostreams::input>, Sink = boost::interprocess::basic_vectorstream<std::vector<unsigned char> >, std::streamsize = long int, typename boost::enable_if<boost::iostreams::is_std_io<Source> >::type = void, typename boost::enable_if<boost::iostreams::is_std_io<Sink> >::type = void]’
ValueFileReader.cpp:92:41: instantiated from here
/usr/include/boost/iostreams/copy.hpp:178:5: error: static assertion failed: "(is_same<src_char, snk_char>::value)"
在code我用低于(简称和使用命名空间为简便起见在这里做):
The code I'm using is below (shortened up and doing "using namespace" for brevity here):
using namespace boost::iostreams;
using namespace boost::interprocess;
filtering_streambuf<input> in;
std::ifstream file(filename, std::ifstream::in);
in.push(zlib_decompressor());
in.push(file);
basic_vectorstream<std::vector<unsigned char>> vectorStream;
copy(in, vectorStream);
std::vector<unsigned char> chars(vectorStream.vector());
我见过的<一个href=\"http://stackoverflow.com/questions/9557555/how-can-i-decom$p$pss-a-vector-of-deflated-data-with-boost\">this螺纹,但我不知道,如果将所有内容复制到一个载体,然后DECOM pressing从载体将是绕了最有效的方式。
I've seen this thread, but I wasn't sure if copying everything into a vector, then decompressing from the vector would be the most efficient way of going about it.
有没有更好的办法,我可以去吗,或者我有个好主意,但在code一些错误?
Is there a better way I can go about this, or do I have the right idea but some error in the code?
推荐答案
的问题是,你的 ifstream的
和 filtering_streambuf
使用字符
作为其基础字符类型,但你的 basic_vectorstream
使用 unsigned char型
作为它的值类型。升压code有一个静态断言要求这些类型是一样的,所以,如果你使用两种不同类型的不可转换你没有得到编译器错误的溢料。
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.
幸运的是,这里的修复是很容易与ndash的;变化:
Fortunately, the fix here is easy – change:
basic_vectorstream<std::vector<unsigned char>> vectorStream;
copy(in, vectorStream);
std::vector<unsigned char> chars(vectorStream.vector());
到
basic_vectorstream<std::vector<char>> vectorStream;
copy(in, vectorStream);
std::vector<unsigned char> chars(
vectorStream.vector().begin(),
vectorStream.vector().end()
);
这是安全的,因为字符
和 unsigned char型
是由C ++标准保证具有相同的对象重新presentation(§3.9.1/ 1)。
This is safe because char
and unsigned char
are guaranteed by the C++ standard to have the same object representation (§3.9.1/1).
无关于直接的问题,但你也需要通过的std :: IOS ::二进制
到文件
的构造函数,否则你将不得不因行结束转换损坏的数据。
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.
这篇关于从升压filtering_streambuf DECOM preSS文件为std ::矢量&lt;&字符GT ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!