Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compress a file (gzip) using boost libraries in C++

Im trying to compress a file using boost library (gzip). Its a simple task: lets suppose i have the file data.xml and I need to compress it to data.xml.gz. I can even use the default compression values, doesn't matter.

I tryed to look up in google and BOOST pages but with no success.

I have the following:

bool SyncFrequencyHistory::frequencyHistoryCompressFile(void)
{
    printf("\r\n===== ACTION: frequencyHistoryCompressFile =====\r\n");

    std::ifstream inStream(FREQUENCY_HISTORY_FILE, std::ios_base::in);
    std::ofstream outStream(FREQUENCY_HISTORY_FILE_GZIP, std::ios_base::out);
    boost::iostreams::filtering_streambuf<boost::iostreams::output> out;
    out.push(boost::iostreams::gzip_compressor());
    out.push(outStream);
    boost::iostreams::copy(in, out);

    return true;
}

FREQUENCY_HISTORY_FILE have the full location of the XML file

FREQUENCY_HISTORY_FILE_GZIP have the full location for the XML.GZ file

I know almost all code is wrong, but i have no idea who to write it the correct way.

like image 823
Vasco Baptista Avatar asked Sep 12 '25 19:09

Vasco Baptista


1 Answers

Based on the following tutorial, I believe your code should be:

bool SyncFrequencyHistory::frequencyHistoryCompressFile(void)
{
    printf("\r\n===== ACTION: frequencyHistoryCompressFile =====\r\n");

    std::ifstream inStream(FREQUENCY_HISTORY_FILE, std::ios_base::in);
    std::ofstream outStream(FREQUENCY_HISTORY_FILE_GZIP, std::ios_base::out);
    boost::iostreams::filtering_streambuf< boost::iostreams::input> in;
    in.push( boost::iostreams::gzip_compressor());
    in.push( inStream );
    boost::iostreams::copy(in, outStream);

    return true;
}

Basically, you read the input, compress it and saves the dat

like image 83
Mr. Beer Avatar answered Sep 15 '25 08:09

Mr. Beer