简体   繁体   English

Boost gzip如何将output压缩字符串作为文本

[英]Boost gzip how to output compressed string as text

I'm using boost gzip example code here.我在这里使用 boost gzip 示例代码 I am attempting to compress a simple string test and am expecting the compressed string H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA as shown in this online compressor我正在尝试压缩一个简单的字符串测试,并期待压缩字符串H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA如此在线压缩器所示

static std::string compress(const std::string& data)
{
    namespace bio = boost::iostreams;
    std::stringstream compressed;
    std::stringstream origin(data);

    bio::filtering_streambuf<bio::input> out;
    out.push(bio::gzip_compressor(bio::gzip_params(bio::gzip::best_compression)));
    out.push(origin);
    
    bio::copy(out, compressed);
    return compressed.str();
}

int main(int argc, char* argv[]){
    std::cout << compress("text") << std::endl;
    // prints out garabage

    return 0;
}

However when I print out the result of the conversion I get garbage values like +I-.但是,当我打印出转换结果时,我会得到像 +I- 这样的垃圾值。 ~ ~

I know that it's a valid conversion because the decompression value returns the correct string.我知道这是一个有效的转换,因为解压值返回了正确的字符串。 However I need the format of the string to be human readable ie H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA .但是我需要字符串的格式是人类可读的,即H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA

How can I modify the code to output human readable text?如何将代码修改为 output 人类可读文本?

Thanks谢谢

Motivation动机

The garbage format is not compatible with my JSON library where I will send the compressed text through.垃圾格式与我将发送压缩文本的 JSON 库不兼容。

The example site completely fails to mention they also base64 encode the result:示例站点完全没有提到他们还 base64 编码结果:

base64 -d <<< 'H4sIAAAAAAAACitJLS4BAAx+f9gEAAAA' | gunzip -

Prints :打印

test

In short, you need to also do that:简而言之,您还需要这样做:

Live On Coliru住在科利鲁

#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <iostream>
#include <sstream>

#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/transform_width.hpp>

std::string decode64(std::string const& val)
{
    using namespace boost::archive::iterators;
    return {
        transform_width<binary_from_base64<std::string::const_iterator>, 8, 6>{
            std::begin(val)},
        {std::end(val)},
    };
}

std::string encode64(std::string const& val)
{
    using namespace boost::archive::iterators;
    std::string r{
        base64_from_binary<transform_width<std::string::const_iterator, 6, 8>>{
            std::begin(val)},
        {std::end(val)},
    };
    return r.append((3 - val.size() % 3) % 3, '=');
}

static std::string compress(const std::string& data)
{
    namespace bio = boost::iostreams;
    std::istringstream origin(data);

    bio::filtering_istreambuf in;
    in.push(
        bio::gzip_compressor(bio::gzip_params(bio::gzip::best_compression)));
    in.push(origin);

    std::ostringstream compressed;
    bio::copy(in, compressed);
    return compressed.str();
}

static std::string decompress(const std::string& data)
{
    namespace bio = boost::iostreams;
    std::istringstream compressed(data);

    bio::filtering_istreambuf in;
    in.push(bio::gzip_decompressor());
    in.push(compressed);

    std::ostringstream origin;
    bio::copy(in, origin);
    return origin.str();
}

int main() { 
    auto msg = encode64(compress("test"));
    std::cout << msg << std::endl;
    std::cout << decompress(decode64(msg)) << std::endl;
}

Prints印刷

H4sIAAAAAAAC/ytJLS4BAAx+f9gEAAAA
test

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM