Boost logo

Boost Users :

From: Andre Krause (post_at_[hidden])
Date: 2005-12-17 10:29:51


hello,
i have a problem. i'm using boost::iostreams to compress and decompress
files using lib bzip2. compressing filestreams works fine, also
decompressing a filestream into a stringstream.
but compressing stuff into a stringstream fails. any idea? is my code wrong?
below the code:

#define BOOST_ALL_NO_LIB
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
using namespace std;
using namespace boost;
using namespace boost::iostreams;

int main()
{

    // filestream --> compressed stringstream
    // FAILS !!
    {
        stringstream ss;

        filtering_streambuf<output> out;
        out.push(bzip2_compressor());
        out.push(ss);

        ifstream file_in("sometext.txt", ios::in | ios::binary);
        if(file_in.is_open())
            cout << "\n bytes compressed to sstream:" << copy(file_in, out);
        cout << "\n length of compressed stringstream:" <<
ss.str().length(); // <-- ss is empty!
    }

    // filestream --> compressed filestream
    // WORKS!
    {
        ofstream file_out("compressedstuff.bz2", ios::out | ios::binary);
        if(file_out.is_open())
        {
            filtering_streambuf<output> out;
            out.push(bzip2_compressor());
            out.push(file_out);

            ifstream file_in("sometext.txt", ios::in | ios::binary);
            if(file_in.is_open())
                copy(file_in, out);
        }
    }

    // compressed filestream --> filestream
    // WORKS!
    {
        ifstream file("compressedstuff.bz2", ios::in | ios::binary);
        if(file.is_open())
        {
            filtering_streambuf<input> in;
            in.push(bzip2_decompressor());
            in.push(file);
            ofstream file_out("somedecompressed.txt", ios::out |
ios::binary);
            if(file_out.is_open())
                copy(in, file_out);
        }
    }

    // compressed filestream --> stringstream
    // WORKS!
    {
        ifstream file("compressedstuff.bz2", ios::in | ios::binary);
        if(file.is_open())
        {
            filtering_streambuf<input> in;
            in.push(bzip2_decompressor());
            in.push(file);

            stringstream ss;
            copy(in, ss);
            cout << "\n length of stringstream:" << ss.str().length();
        }
    }

}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net