提高gzip的DECOM preSS字节数组 [英] boost gzip decompress byte array

查看:115
本文介绍了提高gzip的DECOM preSS字节数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了的gzip / zlib的DECOM $ P $文件pssion如图他们对提升网站的例子。

I implemented the gzip/zlib decompression of files as shown in their examples on the boost site.

void CompressionUtils::Inflate(std::ifstream& inputFile,
                               std::ofstream& outputFile)
{
   boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
   in.push(boost::iostreams::gzip_decompressor());
   in.push(inputFile);
   boost::iostreams::copy(in, outputFile);
}

这工作正常。我也从我从一个基于REST的JSON服务,为com pressed以及获得一个套接字读取数据。我想我会写一个基于内存的实现,如何努力可能呢。好吧,我想通了,我不明白的流和流缓冲,我应该。我责怪过去几年在Java中;)..于是我开始沿着这条道路。

this works fine. I am also reading data from a socket that I am getting from a rest based JSON service that is compressed as well. I figured I would write a memory based implementation, how hard could that be. Well, I figured out I do not understand the streams and stream buffers as I should. I blame the last few years in Java ;) .. So I started down this path.

void CompressionUtils::Inflate(char* compressed, 
                               int size,
                               char* decompressed)
{

   boost::iostreams::stream<boost::iostreams::array_source> source(compressed,size);
   //std::stringstream str;

   boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
   in.push(boost::iostreams::gzip_decompressor());
   in.push(source);
   //boost::iostreams::copy(in, str);   
}

不过,我很茫然,以什么样的流,我可以使用基本上得到的DECOM pressed 的char * 重的DECOM的presentation pressed流。这应该是容易的,可能是,但我已经浪费了最后的几个小时想出不成功的尝试。

But I am at a loss as to what kind of stream I can use to basically get the decompressed char* representation of the decompressed stream. This should be easy, and probably is, but I have been wasting the last couple hours coming up with unsuccessful attempts.

推荐答案

显然,你已经遇到<一个href=\"http://www.boost.org/doc/libs/1_48_0/libs/iostreams/doc/guide/filtering_streams.html\">filtering流和流缓冲的。你可以用同样的方法相反将数据转换为字符串。

Obviously, you've come across filtering streams and stream buffers. You can use the same method in reverse to get data into a string.

我没有我自己的例子得心应手,所以认为这是有些伪code,但是这应该是你在找什么:

I don't have my own examples handy, so consider this to be somewhat pseudo-code but this should be what you're looking for:

namespace io = boost::iostreams; //<-- good practice
typedef std::vector<char> buffer_t;

void CompressionUtils::Inflate(const buffer_t &compressed,
                               buffer_t &decompressed)
{
    io::filtering_ostream os;

    os.push(io::gzip_decompressor());
    os.push(io::back_inserter(decompressed));

    io::write(os, &compressed[0], compressed.size());
}

所以,你可以使用升压提供的背部插入。

So you can use the back inserter provided by Boost.

基本上,上述code的作用是定义一个可以写入到输出流。它设置,以便将写入到它的所有内容将首先DECOM $ P $由的gzip pssed和则附加到 back_inserter 这将作为back_inserters做,插入 DECOM pressed 缓冲区的后面。

Basically, what the above code does is define an output stream which you can write to. It is set up so that all content written to it will first be decompressed by gzip, and then appended to the back_inserter which will, as back_inserters do, insert into the back of the decompressed buffer.

另外,你可以看到,缓冲区包装在的std ::矢量。让我知道这对你的作品。

Also, as you can see, the buffers are wrapped in std::vector. Let me know if this works for you.

这篇关于提高gzip的DECOM preSS字节数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆