Using boost::archive with boost::iostreams to compress data
I want to write a serialize function for a class that can optionally compress the data. I would like to use the compression facilities provided in boost::iostreams. Does anyone know开发者_JAVA技巧 how to do this?
struct X
{
X() {}
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & compression;
if(compression == 0)
{
ar & data;
}
else if(compression == 1)
{
// use boost::iostream compression
// facilities to serialize data
}
}
int compression;
std::vector<int> data;
};
The only way I can see to do that is compress the data first and then use ar.load_binary and ar.save_binary. To compress the data, you could use a filtering_stream with std::ostringstream as sink and an appropriate compression filter.
Any reason you don't want to push the compression down the stack (that is, build your archive over a compressing stream)?
精彩评论