2016-06-20 3 views
1

У меня возникают проблемы с сжатием объекта на строку, а затем сериализация этих данных на диск с помощью библиотеки ускорения C++. Это следует из предыдущего вопроса, который я задал here, который успешно решил проблему сериализации структуры IplImage из библиотеки OpenCV.Проблема с сериализацией + сжатие с помощью библиотеки boost

Моей сериализация код выглядит следующим образом:

// 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» - это путь на моей машине.

ответ

1

Я наивно загружал сжатый файл в строку отдельно. Конечно, 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; 
Смежные вопросы