boost压缩与解压缩流
- 开源代码
- 2026-07-04 02:00:01
boost 压缩与解压缩流(zip、zip2、gzip、lzma、zstd压缩方式)
#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;
}
boost压缩与解压缩流由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“boost压缩与解压缩流”