使用Boost内存Uncom preSS数据gzip_decom pressor [英] Uncompress data in memory using Boost gzip_decompressor
问题描述
我使用升压试图DECOM preSS二进制数据在内存中 gzip_decom pressor
。从这个答案,我适应以下code:
I'm trying to decompress binary data in memory using Boost gzip_decompressor
. From this answer, I adapted the following code:
vector<char> unzip(const vector<char> compressed)
{
vector<char> decompressed = vector<char>();
boost::iostreams::filtering_ostream os;
os.push(boost::iostreams::gzip_decompressor());
os.push(boost::iostreams::back_inserter(decompressed));
boost::iostreams::write(os, &compressed[0], compressed.size());
return decompressed;
}
但是,返回的向量长度为零。我究竟做错了什么?我试着打电话刷新()
上的操作系统流,但它并没有区别
However, the returned vector has zero length. What am I doing wrong? I tried calling flush()
on the os stream, but it did not make a difference
推荐答案
您code适用于我这个简单的测试程序:
Your code works for me with this simple test program:
#include <iostream>
#include <vector>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
std::vector<char> unzip(const std::vector<char> compressed)
{
std::vector<char> decompressed = std::vector<char>();
boost::iostreams::filtering_ostream os;
os.push(boost::iostreams::gzip_decompressor());
os.push(boost::iostreams::back_inserter(decompressed));
boost::iostreams::write(os, &compressed[0], compressed.size());
return decompressed;
}
int main() {
std::vector<char> compressed;
{
boost::iostreams::filtering_ostream os;
os.push(boost::iostreams::gzip_compressor());
os.push(boost::iostreams::back_inserter(compressed));
os << "hello\n";
os.reset();
}
std::cout << "Compressed size: " << compressed.size() << '\n';
const std::vector<char> decompressed = unzip(compressed);
std::cout << std::string(decompressed.begin(), decompressed.end());
return 0;
}
您确认您输入的是COM pressed用gzip而不是一些其他的方法(例如原放气)?用gzip的字节COM pressed数据开始 1F 8B
。
Are you sure your input was compressed with gzip and not some other method (e.g. raw deflate)? gzip compressed data begins with bytes 1f 8b
.
我一般使用 复位()
或放流和过滤器在自己的块,以确保输出完成。我做既为COM pression之上,只是作为一个例子。
I generally use reset()
or put the stream and filters in their own block to make sure that output is complete. I did both for compression above, just as an example.
这篇关于使用Boost内存Uncom preSS数据gzip_decom pressor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!