I'm trying to serialize and compress a std::map
using Boost. Here's the serialization code:
#include <boost/archive/binary_oarchive.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
int main()
{
std::map<uint64_t, std::vector<uint64_t>> m;
m[0].push_back(100);
m[0].push_back(200);
m[0].push_back(300);
m[0].push_back(400);
m[42].push_back(1);
m[42].push_back(2);
m[42].push_back(3);
m[42].push_back(4);
std::ofstream f("test_archive.gz", std::ios::binary);
if(f.fail())
std::cerr << "Couldn't create the archive\n";
boost::iostreams::filtering_ostream filter;
filter.push(boost::iostreams::gzip_compressor());
filter.push(f);
boost::archive::binary_oarchive ar(filter);
ar << m;
}
This is the de-serialization code:
#include <boost/archive/binary_iarchive.hpp>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/serialization/map.hpp>
#include <boost/serialization/vector.hpp>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <map>
#include <vector>
int main()
{
std::map<uint64_t, std::vector<uint64_t>> m;
std::ifstream f("test_archive.gz", std::ios::binary);
if(f.fail())
std::cerr << "Couldn't open the archive\n";
boost::iostreams::filtering_istream filter;
filter.push(boost::iostreams::gzip_decompressor());
filter.push(f);
boost::archive::binary_iarchive ar(f);
ar >> m;
for(const auto& [key, value] : m)
{
std::cout << key << " -> " << "[";
for(const auto& e: value)
std::cout << e << ", ";
std::cout << "]\n";
}
}
When I run the serialization code, I get a 83 bytes long gzip archive named test_archive.gz
. When I run file
on test_archive.gz
, I get test_archive.gz: gzip compressed data, original size modulo 2^32 158
. So based on this it seems like the serialization code is working. However, when I try to run the de-serialization code, I get the following error:
$ ./deserialize
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
zsh: IOT instruction (core dumped) ./deserialize
I don't understand why my de-serialization code isn't working. Can someone please help me with this? Moreover, how can debug
I found the error. The de-serialization code should use boost::archive::binary_iarchive ar(filter);
instead of boost::archive::binary_iarchive ar(f);