XPenguen
XPenguen

Reputation: 183

Decompressing a file using boost

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

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(input);
        in.push(boost::iostreams::bzip2_decompressor());
        boost::iostreams::copy(in,myStream);

        return myStream;
    }

I used c++17, boost 1.58 and gcc 8.0 to compile the code above

and get the following error :

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/tips on how to solve this

Upvotes: 2

Views: 591

Answers (1)

Alan Birtles
Alan Birtles

Reputation: 36379

The device needs to be the last item you push into the filtering_streambuf, after you have pushed a device you aren't allowed to push anything else which is why you are getting the error. See https://www.boost.org/doc/libs/1_68_0/libs/iostreams/doc/classes/filtering_streambuf.html#policy_push

Your code should be:

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;
}

Upvotes: 1

Related Questions