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

Ben*_*ley 5 c++ compression serialization boost

我想为一个可以选择压缩数据的类编写一个序列化函数.我想使用boost :: iostreams中提供的压缩工具.有谁知道如何做到这一点?

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;
};
Run Code Online (Sandbox Code Playgroud)

Igo*_*nko 0

我能想到的唯一方法是首先压缩数据,然后使用 ar.load_binary 和 ar.save_binary。要压缩数据,您可以使用带有 std::ostringstream 作为接收器的过滤流和适当的压缩过滤器。

您不想将压缩推入堆栈(即在压缩流上构建存档)有什么原因吗?