c++ - boost库的序列化+压缩问题

标签 c++ opencv serialization boost

我在将对象压缩为字符串然后使用 boost C++ 库将该数据序列化到磁盘时遇到问题。 这是我之前问过的一个问题 here成功解决了从 OpenCV 库序列化 IplImage 结构的问题。

我的序列化代码如下:

// Now save the frame to a compressed string
boost::shared_ptr<PSMoveDataFrame> frameObj = frame->getCurrentRetainedFrame();
std::ostringstream oss;
std::string compressedString;
{
    boost::iostreams::filtering_ostream filter;
    filter.push(boost::iostreams::gzip_compressor());
    filter.push(oss);
    boost::archive::text_oarchive archive(filter);
    archive & frameObj;
} // This will automagically flush when it goes out of scope apparently

// Now save that string to a file
compressedString = oss.str();
{
    std::ofstream file("<local/file/path>/archive.bin");
    file << compressedString;
}

//    // Save the uncompressed frame
//    boost::shared_ptr<PSMoveDataFrame> frameObj = frame->getCurrentRetainedFrame();
//    std::ofstream file("<local/file/path>/archive.bin");
//    boost::archive::text_oarchive archive(file);
//    archive & frameObj;

和我的反序列化代码:

// Simply load the compressed string from the file
boost::shared_ptr<PSMoveDataFrame> frame;
std::string compressedString;
{
    std::ifstream file("<local/file/path>/archive.bin");
    std::string compressedString;
    file >> compressedString;
}

// Now decompress the string into the frame object
std::istringstream iss(compressedString);
boost::iostreams::filtering_istream filter;
filter.push(boost::iostreams::gzip_decompressor());
filter.push(iss);
boost::archive::text_iarchive archive(filter);
archive & frame;

//    // Load the uncompressed frame
//    boost::shared_ptr<PSMoveDataFrame> frame;
//    std::ifstream file("<local/file/path>/archive.bin");
//    boost::archive::text_iarchive archive(file);
//    archive & frame;

请注意,两个未压缩版本(已注释掉)都可以正常工作。 我收到的错误来自关于输入流错误的 boost::archive::archive_exception。

  • “本地/文件/路径”是我机器上的一个路径。

最佳答案

我天真地将压缩文件单独加载到一个字符串中。 当然,ifstream 并不知道压缩后的字符串确实被压缩了。

下面的代码解决了这个问题:

// Simply load the compressed string from the file
boost::shared_ptr<PSMoveDataFrame> frame;
// Now decompress the string into the frame object
std::ifstream file("<local/file/path>/archive.bin");
boost::iostreams::filtering_stream<boost::iostreams::input> filter;
filter.push(boost::iostreams::gzip_decompressor());
filter.push(file);
boost::archive::binary_iarchive archive(filter);
archive & frame;

关于c++ - boost库的序列化+压缩问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37924255/

相关文章:

android - 使用 opencv 进行图像分类

c++ - 从曲线中提取点

java - Weblogic 是否支持未标记为可序列化的 session 对象的故障转移?

java - 如何使用 GSON 反序列化 Map<String, Object>

python - 如何通过 python 脚本使用多处理或任何其他模块序列化 msiexec.exe 安装?

c++ - 连续固定长度子序列的最大差异

c++ - CUDA内核作为类的成员函数

c - 断言失败

c++ - 为什么 auto 将这个变量推导出为 double 而不是 float?

c++ - 无法转换具有多重继承的类