boost流gzip压缩

注意二进制流需要调用write 

此处用拷贝

 

        //添加gzip支持
        SimpleWeb::CaseInsensitiveMultimap header;
        header.emplace("Cache-Control", "max-age=86400");
        header.emplace("Content-Encoding", "gzip");
        //读取文件
        std::ifstream inStream(path.string(), std::ios_base::in);
        //gzip压缩后的输出流
        std::stringstream ss;//std::ofstream outStream(AppContext::GetResourceFile("test.gz"), std::ios::binary);

        boost::iostreams::filtering_ostream compressingStream;
        //compressingStream.push(boost::iostreams::counter()); //压缩前计数 compressingStream.component<boost::iostreams::counter>(0)->characters()
        compressingStream.push(boost::iostreams::gzip_compressor(boost::iostreams::gzip_params(boost::iostreams::gzip::best_compression)));
        compressingStream.push(boost::iostreams::counter()); //添加压缩后计数
        compressingStream.push(ss);//输出压缩流至字符串流
        boost::iostreams::copy(inStream, compressingStream);//拷贝原始文件流到压缩流
        compressingStream.flush(); 
        boost::iostreams::close(compressingStream); //关闭压缩流

        header.emplace("Content-Length", to_string(compressingStream.component<boost::iostreams::counter>(1)->characters()));
        response->write(header);
        boost::iostreams::copy(ss, *response);

 

posted @ 2021-08-04 16:13  wolbo  阅读(616)  评论(0编辑  收藏  举报