当前位置:   article > 正文

boost 压缩与解压缩流

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;
}
  • 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
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/256205
推荐阅读