| 1 | #include <iostream>
|
|---|
| 2 |
|
|---|
| 3 | #include <boost/iostreams/filtering_stream.hpp>
|
|---|
| 4 | #include <boost/iostreams/device/back_inserter.hpp>
|
|---|
| 5 | #include <boost/iostreams/copy.hpp>
|
|---|
| 6 |
|
|---|
| 7 | #pragma GCC visibility push(default) // required when compiling with g++ -fvisibility=hidden
|
|---|
| 8 | #include <boost/iostreams/filter/bzip2.hpp>
|
|---|
| 9 | #pragma GCC visibility pop
|
|---|
| 10 |
|
|---|
| 11 | using namespace std;
|
|---|
| 12 |
|
|---|
| 13 | int main()
|
|---|
| 14 | {
|
|---|
| 15 | string source("Original");
|
|---|
| 16 | string bzip2String;
|
|---|
| 17 | string sink;
|
|---|
| 18 |
|
|---|
| 19 | try
|
|---|
| 20 | {
|
|---|
| 21 | // Create compressing stream based on original input
|
|---|
| 22 | boost::iostreams::filtering_ostream compressingStream;
|
|---|
| 23 | compressingStream.push(boost::iostreams::bzip2_compressor());
|
|---|
| 24 | compressingStream.push(boost::iostreams::back_inserter(bzip2String));
|
|---|
| 25 |
|
|---|
| 26 | compressingStream << source;
|
|---|
| 27 |
|
|---|
| 28 | boost::iostreams::close(compressingStream);
|
|---|
| 29 |
|
|---|
| 30 | cerr << "size of bzip2String: " << bzip2String.size() << endl;
|
|---|
| 31 |
|
|---|
| 32 | // Create decompressing stream writing to sink
|
|---|
| 33 | boost::iostreams::filtering_ostream decompressingStream;
|
|---|
| 34 | decompressingStream.push(boost::iostreams::bzip2_decompressor());
|
|---|
| 35 | decompressingStream.push(boost::iostreams::back_inserter(sink));
|
|---|
| 36 |
|
|---|
| 37 | decompressingStream << bzip2String;
|
|---|
| 38 |
|
|---|
| 39 | boost::iostreams::close(decompressingStream);
|
|---|
| 40 |
|
|---|
| 41 | cerr << "Result: " << sink << endl;
|
|---|
| 42 | }
|
|---|
| 43 | catch(boost::iostreams::bzip2_error& error)
|
|---|
| 44 | {
|
|---|
| 45 | cerr << "Caught bzip2 error with code: " << error.error() << endl;
|
|---|
| 46 | cerr << "data_error: " << boost::iostreams::bzip2::data_error << endl;
|
|---|
| 47 | cerr << "data_error_magic: " << boost::iostreams::bzip2::data_error_magic << endl;
|
|---|
| 48 | cerr << "config_error: " << boost::iostreams::bzip2::config_error << endl;
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| 51 |
|
|---|