序列化 压缩的问题与Boost库
Issue with serialization + compression with the boost library
我在将对象压缩到字符串中,然后将数据序列化以使用Boost C 库将其序列化。这是从我在此处问的一个问题中提出的,这成功解决了从OpENCV库中序列化Iplimage struct的问题。
我的序列化代码如下:
// 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。
- " local/file/path"是我机器上的路径。
我天真地将压缩文件分别加载到字符串中。当然,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;
相关文章:
- 基于boost的程序的静态链接——zlib问题
- C++boost序列化多态性问题
- 在 boost::variant 中类 holden 的复制构造函数存在问题
- 我在使用 boost::serialization 时遇到了一个问题,我的代码在 Linux 中运行良好,但在 Wind
- 将 Boost 与 CMAKE 联系起来时出现问题
- boost Logger出现问题
- 在 Windows 10 上安装 Boost 时遇到问题
- 链接问题 boost::p ython::numpy.
- 递归 Boost.Spirit 解析的问题
- 在C++中读取文件系统时出现问题(Boost 未编译)
- 使用 boost::序列化将派生类指针序列化为向量时出现问题
- 获取“boost::文件系统::p ath”字符指针时出现问题
- Xcode(版本9.2(9C40B)):试图包括Boost时C 语义和链接器问题
- 与 boost.python 和 python3 链接时出现问题
- PCL 构建存在 boost::throw_exception 问题
- 链接boost.python示例的问题
- BOOST-ASIO ASYNC_RECEIVE_FROM功能超载问题( 动态指针)
- 在Windows 10中的Boost :: FileSystem :: remove_all遇到问题
- Boost-beast websocket握手问题
- 在 gcc 中使用 fsanitize 开关的 Boost 出现问题