C++ note 使用 boost iostreams 库压缩和解压数据
參考這篇文章:使用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 .