Some problems with Compress/Decompress program.

PDamasceno
PDamasceno used Ask the Experts™
on
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?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Your code compiles fine for me, what compiler are you using? Most likely the error is trivial, you've forgotten to save the file or so.

In the code below, you get an answer to your second question, how to decompress a file.
If you then switch the cout for a different stream, you get the answer to your first question, since you can use a stream to write to any file :)

As for question number three, just replace "hello.z" with your variable.

I'm not terribly good with C++ exceptions, so I'll leave that for another expert :)
//Simple decompression example shamelessly taken from boost docs:
ifstream file("hello.z", ios_base::in | ios_base::binary);
filtering_streambuf<input> in;
in.push(zlib_decompressor());
in.push(file);
boost::iostreams::copy(in, cout);

Open in new window

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 :)
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Top Expert 2009
Commented:
>> testedc.cpp:45: error: invalid conversion from ‘int’ to ‘const char*’

Which line is line 45 ?


>> * How can I make the file go to a specific directory after it is compressed?

You'll want to save the file there. Have a look at file I/O in C++ :

        http://www.cplusplus.com/doc/tutorial/files/

Or better yet, you probably want to stream the data directly to the file while compressing. So, instead of writing to a buffer, write to a file stream.


>> * If the file to decompress is not in a buffer but in a directory, how can I tell the function decompress() to work?

Similar answer.


>> * If the path to the file needed be decompressed is stored in other variable, how can I tell the function decompress() to work?

When you open the file stream, you'll have to specify the filename.


>> * How can I handle possible exception errors from compress() and decompress()?

Have a try-catch block to catch and handle the appropriate exceptions.

        http://www.cplusplus.com/doc/tutorial/exceptions/


>> * Any thoughts on how to make this program the most efficient possible, in terms of speed of the compress and decompress functions?

I'd keep that for later. First get it working the way you want, and then do some performance tests to see if it meets your performance requirements.

Author

Commented:
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.
Top Expert 2009

Commented:
>> 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.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial