开发者

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)?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜