赞
踩
#include <boost/iostreams/filtering_stream.hpp> #include <boost/iostreams/filter/zlib.hpp> #include <boost/iostreams/filter/bzip2.hpp> #include <boost/iostreams/filter/gzip.hpp> #include <boost/iostreams/filter/lzma.hpp> #include <boost/iostreams/filter/zstd.hpp> #include <boost/iostreams/device/file_descriptor.hpp> #include <boost/iostreams/device/file.hpp> #include <iostream> #include <sstream> int main() { try { // boost::iostreams::zlib_compressor() zip压缩方式 // boost::iostreams::zip2_compressor() zip2压缩方式 // boost::iostreams::gzip_compressor() gzip压缩方式 // boost::iostreams::lzma_compressor() lzma压缩方式 // boost::iostreams::zstd_compressor() zstd压缩方式 // 压缩数据流 { boost::iostreams::filtering_ostream out; out.push(boost::iostreams::zlib_compressor()); //out.push(ss_comp); //压缩到字符流中 out.push(boost::iostreams::file_sink("test.txt")); //压缩到文件中 out.write("hello", sizeof("hello") - 1); out.write("hellohello", sizeof("hellohello") - 1); out.write("hellohello", sizeof("hellohello") - 1); out.write("hellohello", sizeof("hellohello" - 1)); out.write("this is a test", sizeof("this is a test") - 1); out.write("hellohello", sizeof("hellohello") - 1); std::cout << "compressor data end" << std::endl; } // 解压缩数据流 { boost::iostreams::filtering_istream in; in.push(boost::iostreams::zlib_decompressor()); //in.push(ss_comp); //从字符流中解压 in.push(boost::iostreams::file_source("test.txt")); //从文件中解压 char databuf[1024]{ 0 }; in.read(databuf, sizeof(databuf)); std::cout << "decompressor data:" << databuf << ", len=" << in.gcount() << std::endl; } } catch (std::exception& e) { std::cout << "exception:" << e.what() << std::endl; } catch (...) { std::cout << "unknown exception." << std::endl; } system("pause"); return 0; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。