Link to home
Start Free TrialLog in
Avatar of PDamasceno
PDamascenoFlag for Canada

asked on

Some problems with Compress/Decompress program.

Hey Experts.
Some days ago I posted a question here for some help with a compress/decompress program.
Got the help I needed and wrote the code below to test with boost iostreams a little bit and finally get to where I want to.

(I'm new to C++ development so it is possible to have syntax erros in the code, point them out for me if you see any please!)

#include <iostream>
#include <sstream>

#include </usr/include/boost/assign/list_of.hpp>
#include </usr/include/boost/iostreams/copy.hpp>
#include </usr/include/boost/iostreams/device/back_inserter.hpp>
#include </usr/include/boost/iostreams/filter/zlib.hpp>
#include </usr/include/boost/iostreams/filtering_stream.hpp>

//-----------------------------------------------------------------------------
void
    compress
    (
        const std::string& data,
        std::string&       buffer
    )
{
    buffer.clear();
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
    out.push(boost::iostreams::zlib_compressor());
    out.push(boost::iostreams::back_inserter(buffer));
    boost::iostreams::copy(boost::make_iterator_range(data), out);
}

//----------------------------------------------------------------------------
void
    decompress
    (
        const std::string& buffer,
        std::string&       data
    )
{
    boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
    in.push(boost::iostreams::zlib_decompressor());
    in.push(boost::make_iterator_range(buffer));
    data.clear();
    boost::iostreams::copy(in, boost::iostreams::back_inserter(data));
}

//----------------------------------------------------------------------------
int main()
{
    const std::string send_data = "Test, test, test, test!!!!!";
    std::string received_data;
    std::string buffer;

    compress(send_data, buffer);
    decompress(buffer, received_data);
   
    if(send_data == received_data)
       std::cout << send_data << " equals " << received_data << std::endl;
    else
       std::cout << "Erro: " << send_data << " differs from " << received_data << std::endl;

    return 0;
}

But I keep getting this freaking error while trying to compile it:

testedc.cpp: In function ‘int main()’:
testedc.cpp:45: error: invalid conversion from ‘int’ to ‘const char*’
testedc.cpp:45: error:   initializing argument 1 of ‘std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]’

I can't find that invalid conversion it is telling me.
Can you guys help me with this?
Also, I would like you guys to take a look in my code up there and see if everything is fine with it.

A couple more questions:
* How can I make the file go to a specific directory after it is compressed?
* If the file to decompress is not in a buffer but in a directory, how can I tell the function decompress() to work?
* If the path to the file needed be decompressed is stored in other variable, how can I tell the function decompress() to work?
* How can I handle possible exception errors from compress() and decompress()?
* Any thoughts on how to make this program the most efficient possible, in terms of speed of the compress and decompress functions?
SOLUTION
Avatar of letharion
letharion
Flag of Sweden image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I did locate this on exception handling.
It seems pretty good in that it steps through exception handling in several examples, rather than putting everything in the same example :)
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PDamasceno

ASKER

Thanks for the quick answer guys.
The compile problem was something seamless and I really don't know what it was.
Just compiled it and everything went fine. Weird...

As for the other questions, thanks for the insight letharion and Infinity08.
Just to clarify the first question, when I compress a file with boost.iostreams using zlib the file it create, after the compression is done, is something like "filename.z"?
If so I'll just have to use file streams to tell where the file will go?

Sorry if I'm not very clear with my questions and thanks again.
>> If so I'll just have to use file streams to tell where the file will go?

Indeed. Refer to letharion's example code earlier.