/ walking bear:D / C++ note 使用 boost iostreams 库压缩和解压数据

C++ note 使用 boost iostreams 库压缩和解压数据

2019-09-27 posted in [筆記]

參考這篇文章:使用boost::iostreams库压缩和解压数据 修改程式如下:

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/device/file.hpp>
#include <iostream>
#include <sstream> 

int main()      
{
        try {  
                std::stringstream ss;
                {
                boost::iostreams::filtering_ostream out;
                out.push(boost::iostreams::gzip_compressor());
                out.push(ss);    //壓縮到字元流中
                // out.push(boost::iostreams::file_sink("test.txt")); //壓縮到檔案中
                out << "Hello, brave new world!\n" << std::flush;
                } 
                std::cout << "compressor data: " << ss.str() << std::endl;
                {
                boost::iostreams::filtering_istream in;
                in.push(boost::iostreams::gzip_decompressor());
                in.push(ss);     //從字元流中解壓
                // in.push(boost::iostreams::file_source("test.txt")); //從檔案中解壓
                std::cout << "decompressor data: " << in.rdbuf() << std::endl;
                }
        }
        catch(std::exception& e)  {
                std::cout << "exception:" << e.what() << std::endl;
        }       
        catch(...) {
            std::cout << "unknown exception." << std::endl;
        }       
        return 0;
}               

. reply_count: 0 get_replies : 0 .

walking bear:DRSS feed

关于

wkliang

Clarke's Three Laws:

  • When a distinguished but elderly scientist states that something is possible, he is almost certainly right. When he states that something is impossible, he is very probably wrong.
  • The only way of discovering the limits of the possible is to venture a little way past them into the impossible.
  • Any sufficiently advanced technology is indistinguishable from magic.
  • 版权申明

    知识共享许可协议

    Fork me on GitHub

    Powered by

    Disqus, GitHub, Google Custom Search, Gravatar, HighlightJS, jekyll