I'm having issues compressing an object to a string and then serializing that data to disk with the boost C++ library. This follows from a previous question I asked here which successfully solved the problem of serializing an IplImage struct from the OpenCV library.
My serialization code is as follows:
// 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;
and my deserialization code:
// 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;
Note that both uncompressed versions (commented out) work just fine. The error I get is from boost::archive::archive_exception regarding an input stream error.
I was naively loading the compressed file into a string separately. Of course, ifstream has no idea that the compressed string is indeed compressed.
The following code fixed the problem:
// 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With