简体   繁体   English

使用boost :: iostreams和zlib读取.gz文件时出现运行时错误

[英]Run-time error reading a .gz file using boost::iostreams and zlib

I am trying to read a .gz file and print the text content on screen by using boost::iostreams. 我正在尝试读取.gz文件并通过使用boost :: iostreams在屏幕上打印文本内容。 This is just a simple experiment to learn about this library, and I am using the "directors.list.gz" file from IMDb (ftp://ftp.fu-berlin.de/pub/misc/movies/database/) as my input file. 这只是了解该库的简单实验,我使用IMDb(ftp://ftp.fu-berlin.de/pub/misc/movies/database/)中的“ directors.list.gz”文件作为我的输入文件。

My code compiles, via MSVC-10, but the process aborts when executed. 我的代码通过MSVC-10进行编译,但是该过程在执行时中止。 There's not much information from the error message except for the error code being R6010. 除了错误代码为R6010之外,错误消息中没有太多信息。

Can someone please point me a direction in terms of what may have caused this and how do I make this work? 有人可以为我指出可能的原因以及如何使它工作吗?

This library looks pretty neat and I do hope to use it correctly. 这个库看起来很整洁,我希望正确使用它。 Thanks a lot for helping. 非常感谢您的帮助。

#include <fstream>                 // isalpha
#include <iostream>                // EOF
#include <boost/iostreams/categories.hpp> // input_filter_tag
#include <boost/iostreams/operations.hpp> // get
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/filter/zlib.hpp>


using namespace std;
namespace io = boost::iostreams;


int main() 
{

    if(true)
    {

        string infile_path = "c:\\Temp\\directors.list.gz";
        ifstream infile(infile_path, ios_base::in | ios_base::binary);
        io::filtering_streambuf<io::input> in; //filter
        in.push(io::zlib_decompressor());       
        in.push(infile); 

        //output to cout
        io::copy(in, cout);
    }

    return 0;
}

The gzip file format has an additional header around the zlib data, which zlib can't read. gzip文件格式在zlib数据周围有一个附加标头,但zlib无法读取。

So you want to use boost's gzip_decompressor instead of zlib_decompressor. 因此,您想使用boost的gzip_decompressor而不是zlib_decompressor。

in.push(gzip_decompressor());

Note you'll need to include boost/iostreams/filter/gzip.h instead of boost/iostreams/filter/zlib.h . 注意你需要包括升压/输入输出流/过滤/ gzip.h代替升压/输入输出流/过滤/ zlib.h。

Here's a working example of streaming a GZIP file: 这是流式传输GZIP文件的工作示例:

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

using namespace boost::iostreams;

int main()
{
    std::ifstream file("hello.gz", std::ios_base::in | std::ios_base::binary);
    filtering_streambuf < input > in;
    in.push(gzip_decompressor());
    in.push(file);
    boost::iostreams::copy(in, std::cout);
}

You'll find more information on specific boost::iostreams filters lurking here in boost's documentation: http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/quick_reference.html#filters 您可以在boost的文档中找到有关特定boost :: iostreams过滤器的更多信息: http : //www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/quick_reference.html#filters

I also feel I should point out that your code didn't compile with gcc: in the C++ standard library, the ifstream constructor takes a const char *, not a std::string . 我还觉得我应该指出,您的代码未使用gcc进行编译:在C ++标准库中, ifstream构造函数采用const char *而不是std :: string (I'm not sure about Microsoft's version). (我不确定Microsoft的版本)。

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

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