2015-11-24 101 views
1

我有一个个人项目,我必须使用zlib来解压缩一些数据。我将二进制数据存储到std :: vector中,其中byte_t不是无符号的字符。Boost Zlib在解压缩时失败

所以,我从他们的网站修改zlib的解压缩功能,看起来像:http://pastebin.com/spXcRyxa

一切正常测试的约90%,但随后崩溃的应用程序(在addToVector一些bug - 找不到它)。

然后我记得boost有一个很好的zlib包装。我使用zlib编译boost,并搜索如何使用std :: vector(几乎所有示例都是针对std :: string)。但我找到了bzip的例子(Uncompress data in memory using Boost gzip_decompressor),然后我改变了一些东西让它运行;

std::vector<byte_t> unzip(const std::vector<byte_t> compressed) 
{ 
    std::vector<byte_t> decompressed = std::vector<byte_t>(); 

    boost::iostreams::filtering_ostream os; 

    os.push(boost::iostreams::zlib_decompressor()); 
    os.push(std::back_inserter(decompressed)); 

    boost::iostreams::write(os, reinterpret_cast<const char*>(&compressed[0]), compressed.size()); 

    return decompressed; 
} 

我的更改无法进行所有测试。来自boost zlib的解压缩数据比正常的zlib库短约10 000个字符。

我在这个boost zlib实现中做了什么错?谢谢 !

回答

1

真的,你的样品应该是自包含和可重复的(容易,只包括一个微小的压缩样品基准)。

在这种情况下,这是我的两分钱:确保它被刷新。最简单的方法是使用沉没数据之前关闭的ostream:

std::vector<byte_t> unzip(std::vector<byte_t> const& compressed) 
{ 
    std::vector<byte_t> decompressed = std::vector<byte_t>(); 

    { 
     boost::iostreams::filtering_ostream os; 

     os.push(boost::iostreams::zlib_decompressor()); 
     os.push(std::back_inserter(decompressed)); 

     boost::iostreams::write(os, reinterpret_cast<const char*>(&compressed[0]), compressed.size()); 
    } 

    return decompressed; 
} 

PS。请注意我如何通过const&获取压缩数据。您的功能将在解压缩前复制输入...

+0

它看起来工作。谢谢。我不需要复制数据,因为我没有使用解压缩后的压缩数据:D。 –

+0

您必须误读我关于复制输入数据的评论 – sehe