提升1.59不解压缩所有bzip2流

时间:2015-09-30 16:14:02

标签: c++ boost bzip2 bz2

我一直试图在运行中逐行解压缩一些.bz2文件,可以这么说,因为我正在处理的文件是大量未压缩的(未压缩的100 GB区域)所以我想添加一个节省磁盘空间的解决方案。

使用vanilla bzip2压缩的文件解压缩没有问题,但使用pbzip2压缩的文件只解压缩它找到的第一个bz2流。这个bugtracker与这个问题有关:https://svn.boost.org/trac/boost/ticket/3853但是我相信它已经修复了1.41版。我检查了bzip2.hpp文件,它包含'固定'版本,我还检查了程序中使用的Boost版本是1.59。

代码在这里:

cout<<"Warning bzip2 support is a little buggy!"<<endl;

//Open the file here
trans_file.open(files[i].c_str(), std::ios_base::in |  std::ios_base::binary);

//Set up boost bzip2 compression
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::bzip2_decompressor());
in.push(trans_file);
std::string str;

//Begin reading
while(std::getline(in, str))
{
    std::stringstream stream(str);
    stream>>id_f>>id_i>>aif;
    /* Do stuff with values here*/
}

任何建议都会很棒。谢谢!

1 个答案:

答案 0 :(得分:3)

你是对的。

似乎变更集#63057仅修复了部分问题。

相应的单元测试确实有效。但它使用copy算法(如果相关,也会使用composite<>代替filtering_istream

我将此视为缺陷或回归。当然,包括一个展示问题的文件。对我来说,只使用/etc/dictionaries-common/words压缩pbzip2(默认选项)来重现它。

我这里有test.bz2http://7f0d2fd2-af79-415c-ab60-033d3b494dc9.s3.amazonaws.com/test.bz2

这是我的测试程序:

#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
#include <boost/iostreams/stream.hpp>
#include <fstream>
#include <iostream>

namespace io = boost::iostreams;

void multiple_member_test(); // from the unit tests in changeset #63057

int main() {
    //multiple_member_test();
    //return 0;

    std::ifstream trans_file("test.bz2", std::ios::binary);

    //Set up boost bzip2 compression
    io::filtering_istream in;
    in.push(io::bzip2_decompressor());
    in.push(trans_file);

    //Begin reading
    std::string str;
    while(std::getline(in, str))
    {
        std::cout << str << "\n";
    }
}

#include <boost/iostreams/compose.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/device/back_inserter.hpp>
#include <cassert>
#include <sstream>

void multiple_member_test()  // from the unit tests in changeset #63057
{ 
    std::string      data(20ul << 20, '*');
    std::vector<char>  temp, dest; 

    // Write compressed data to temp, twice in succession 
    io::filtering_ostream out; 
    out.push(io::bzip2_compressor()); 
    out.push(io::back_inserter(temp)); 
    io::copy(boost::make_iterator_range(data), out); 
    out.push(io::back_inserter(temp)); 
    io::copy(boost::make_iterator_range(data), out); 

    // Read compressed data from temp into dest 
    io::filtering_istream in; 
    in.push(io::bzip2_decompressor()); 
    in.push(io::array_source(&temp[0], temp.size())); 
    io::copy(in, io::back_inserter(dest)); 

    // Check that dest consists of two copies of data 
    assert(data.size() * 2 == dest.size()); 
    assert(std::equal(data.begin(), data.end(), dest.begin())); 
    assert(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2)); 

    dest.clear(); 
    io::copy( 
            io::array_source(&temp[0], temp.size()), 
            io::compose(io::bzip2_decompressor(), io::back_inserter(dest))); 

    // Check that dest consists of two copies of data 
    assert(data.size() * 2 == dest.size()); 
    assert(std::equal(data.begin(), data.end(), dest.begin())); 
    assert(std::equal(data.begin(), data.end(), dest.begin() + dest.size() / 2)); 
}