The error you are getting is actually misleading.
This is because:
if(boost::iostreams::bzip2::config_error)
...always evaluates to non-zero and gives you the wrong error message!
I don't have a Windows PC available to me, but I tested "your" code on a Mac (Boost installed using Homebrew), and got the same error.
libbzip2 has been improperly configured for the current platform
And so I fixed that up and added some error checking:
#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/filesystem.hpp>
int main()
{
using namespace std;
using namespace boost::iostreams;
char filename[] = "gcc-4.7.2.tar.bz2";
if (!boost::filesystem::exists(filename))
{
cout << "Can't find " << filename << ". Expect errors to follow! " << endl;
}
ifstream file(filename, ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
in.push(bzip2_decompressor());
in.push(file);
try
{
boost::iostreams::copy(in, cout);
}
catch (const bzip2_error& exception)
{
cout << exception.what() << endl;
int error = exception.error();
if (error == bzip2::data_error)
{
cout << "compressed data stream is corrupted";
}
else if (error == bzip2::data_error_magic)
{
cout << "compressed data stream does not begin with the 'magic' sequence 'B' 'Z' 'h'";
}
else if (error == bzip2::config_error)
{
cout << "libbzip2 has been improperly configured for the current platform";
}
else
{
cout << "Error: " << error;
}
cout << endl;
}
}
Build
> clang++ 18121908.cpp -lboost_iostreams -lboost_system -lboost_filesystem -o 18121908
Run
> ./18121908
Can't find gcc-4.7.2.tar.bz2. Expect errors to follow!
bzip2 error
Error: -7
Why -7
? Because #define BZ_UNEXPECTED_EOF (-7)
. Boost calls this bzip2::unexpected_eof
. You've unexpectedly hit the end of the stream!
Make a valid (if questionable) bzip2 file:
> echo 'There should be some compiler stuff in here' > gcc-4.7.2.tar
> bzip2 gcc-4.7.2.tar
Re-run
> ./18121908
There should be some compiler stuff in here
Conclusion
You need to check whether the file exists before attempting to decompress it.