当前位置: 首页 > news >正文

boost 压缩与解压缩流

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;
}

相关文章:

  • 揭秘Apache Kafka消费者:深度理解与实战指南
  • python实现B/B+树
  • 基于左逆的三点法测距,MATLAB函数
  • react diff 原理
  • 梦百合将在深圳展发布定制新品牌“榀至”
  • Prompt Engineering(提示工程)
  • Apache Spark 的基本概念和在大数据分析中的应用
  • c++进阶(c++里的继承)
  • uniapp富文本编辑-editor-vue2-vue3-wangeditor
  • 设计模式 单例模式
  • SpringBoot集成netty实现websocket通信
  • 详细介绍Springcloud,Springcloud alibaba,dubbo等微服务框架,以及nacos和feign
  • 使用Spring事件机制,合理的进行业务解耦
  • Skywalking(9.7.0) 告警配置
  • 【研发日记】,Matlab/Simulink开箱报告(十)——Requirements Toolbox
  • 【PHP+代码审计】PHP基础——流程控制
  • Mysql数据库的优点
  • 去除PDF论文行号的完美解决方案
  • docker容器镜像管理+compose容器编排(持续更新中)
  • spring-boot-maven-plugin springboot打包配置问题
  • 2024年度全国秋粮收购达3.45亿吨
  • 世贸组织欢迎中美经贸高层会谈取得积极成果
  • 湖北宜昌:在青山绿水间解锁乡村振兴“密码”
  • 上汽享道出行完成13亿元C轮融资,已启动港股IPO计划
  • 马上评|让“贾宝玉是长子长孙”争议回归理性讨论
  • 招行:拟出资150亿元全资发起设立金融资产投资公司