Im trying to use the iostreams gzip compression on xml serializations produced using boost::serialization and Im having a bit of trouble with it. The variable to be serialized is savedClass. As shown below, I create the serialization, save to a file with gzip_compressor, then read it from the file with gzip_decompressor. My issue is that at the end of the code, test1.xml(~10k) and test2.xml(~12k) are not identical. Test2.xml has some extraneous characters added to the end (~2k worth). I assume these are leftovers from a memcopy somewhere, but Im not sure why. I have the same effect when I use zlib_compressor as well. What is wrong with my usage of the library(s)? //////////////////////////////////////////////////////////////// std::strstream sStream; if(true) { //Scope used to ensure that the archive is fully processed boost::archive::xml_oarchive oArchive(sStream); oArchive << BOOST_SERIALIZATION_NVP(savedClass); } std::ofstream oStreamXML1( "c:\\test1.xml", std::ios::out ); oStreamXML1 << sStream.str(); if(true) { //Scope used to ensure that the archive is fully processed std::ofstream file("c:\\test.gz", std::ios_base::out | std::ios_base::binary); boost::iostreams::filtering_streambufboost::iostreams::output outStream; outStream.push(boost::iostreams::gzip_compressor()); outStream.push(file); boost::iostreams::copy(sStream, outStream); } std::strstream sStream2; if(true) { //Scope used to ensure that the archive is fully processed std::ifstream file2("c:\\test.gz", std::ios_base::in | std::ios_base::binary); boost::iostreams::filtering_streambufboost::iostreams::input inStream; inStream.push(boost::iostreams::gzip_decompressor()); inStream.push(file2); boost::iostreams::copy(inStream, sStream2); } std::ofstream oStreamXML2( "c:\\test2.xml", std::ios::out ); oStreamXML2 << sStream2.str(); ////////////////////////////////////////////////////////////////
Jabin Reinhold wrote:
My issue is that at the end of the code, test1.xml(~10k) and test2.xml(~12k) are not identical. Test2.xml has some extraneous characters added to the end (~2k worth). I assume these are leftovers from a memcopy somewhere, but I’m not sure why. I have the same effect when I use zlib_compressor as well. What is wrong with my usage of the library(s)?
if(true) { //Scope used to ensure that the archive is fully processed std::ofstream file("c:\\test.gz", std::ios_base::out | std::ios_base::binary);
Should that be a iostreams::file_sink?
boost::iostreams::filtering_streambufboost::iostreams::output outStream; outStream.push(boost::iostreams::gzip_compressor()); outStream.push(file); boost::iostreams::copy(sStream, outStream); }
Try this: boost::iostreams::filtering_streambufboost::iostreams::output > outStream; outStream.push(boost::iostreams::gzip_compressor()); outStream.push(boost::iostreams::file_sink("c:\\test.gz", std::ios::binary)); boost::iostreams::copy(sStream, outStream);
gchen
Try this:
boost::iostreams::filtering_streambufboost::iostreams::output > outStream; outStream.push(boost::iostreams::gzip_compressor()); outStream.push(boost::iostreams::file_sink("c:\\test.gz", std::ios::binary)); boost::iostreams::copy(sStream, outStream);
At your suggestion, I replaced both the std::ifStream and std::ofStream with boost::iostreams::file_source and boost::iostreams::file_sink respectively. The results are the same.
participants (2)
-
gchen
-
Jabin Reinhold