Decompressing a file using boost
Nov 24, 2018 at 11:38pm UTC
Hello,
I want to decompress a file using boost that has been compressed using bzip2
I tried the following which leads to an error I can not explain
1 2 3 4 5 6 7 8 9 10 11 12
std::stringstream readData(const std::string path) {
std::stringstream myStream;
std::ifstream input(path,std::ios_base::in);
boost::iostreams::filtering_streambuf<boost::iostreams::input>in;
in.push(boost::iostreams::bzip2_decompressor());
in.push(input);
boost::iostreams::copy(in,myStream);
return myStream;
}
I used c++17, boost 1.58 and gcc 8.0 to compile the code above
The Error I am getting:
1 2
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injectorstd::logic_error >'
what(): chain complete
Would appreciate any help
Last edited on Nov 24, 2018 at 11:38pm UTC
Nov 25, 2018 at 12:08am UTC
You should open the file in binary mode, which makes a difference on windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
// g++ -std=c++11 -Wall -o bzip2test bzip2test.cc -lboost_iostreams
#include <iostream>
#include <fstream>
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
std::stringstream readData(const std::string& path) {
using namespace boost::iostreams;
std::stringstream ss;
std::ifstream file(path, file.in | file.binary);
if (!file) return ss; // or throw an exception or whatever
filtering_streambuf<input> in;
in.push(bzip2_decompressor());
in.push(file);
copy(in, ss);
return ss;
}
int main() {
auto ss = readData("test01.bz2" );
std::cout << ss.str() << '\n' ;
}
Last edited on Nov 25, 2018 at 12:10am UTC
Topic archived. No new replies allowed.