• boost 压缩与解压缩流


    boost 压缩与解压缩流(zip、zip2、gzip、lzma、zstd压缩方式)

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    #include 
    #include 
    
    #include 
    #include 
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
  • 相关阅读:
    2024/4/16 网络编程day4
    Golang企业面试题
    17.Http__Linux
    Java8新特性--新的时间和日期API
    P2605 [ZJOI2010]基站选址(线段树优化dp经典题)
    统计指定字符串中每个单词出现的次数(C++)
    常用数学分析和建模软件
    【中间件】Redis监控以及指标
    Webpack
    指针成员操作符
  • 原文地址:https://blog.csdn.net/qiufeng_xinqing/article/details/136690143