简体   繁体   English

使用boost :: archive和boost :: iostreams来压缩数据

[英]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. 我想使用boost :: iostreams中提供的压缩工具。 Does anyone know 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. 我能看到的唯一方法是首先压缩数据,然后使用ar.load_binary和ar.save_binary。 To compress the data, you could use a filtering_stream with std::ostringstream as sink and an appropriate compression filter. 要压缩数据,可以使用带有std :: ostringstream的filtering_stream作为接收器和适当的压缩过滤器。

Any reason you don't want to push the compression down the stack (that is, build your archive over a compressing stream)? 您不希望将压缩压缩到堆栈的任何原因(即,通过压缩流构建存档)?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM