如何从boost的gzip_decompressor()获取gzip_params

时间:2018-02-18 18:12:16

标签: c++ boost gzip

我正在使用以下链接中的boost gzip_decompressor(): How can I read line-by-line using Boost IOStreams' interface for Gzip files?

读取gzip文件工作正常,但我如何阅读gzip_params?我想知道存储在gzip_params.file_name中的原始文件名。

1 个答案:

答案 0 :(得分:2)

很棒的问题。

解决方案是使用component<N, T>获取指向实际解压缩器实例的指针:

<强> Live On Coliru

#include <iostream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main()
{
    std::ifstream file("file.gz", std::ios_base::in | std::ios_base::binary);
    try {
        boost::iostreams::filtering_istream in;
        using gz_t = boost::iostreams::gzip_decompressor;
        in.push(gz_t());
        in.push(file);

        for(std::string str; std::getline(in, str); )
        {
            std::cout << "Processed line " << str << '\n';
        }

        if (gz_t* gz = in.component<0, gz_t>()) {
            std::cout << "Original filename: " << gz->file_name() << "\n";
            std::cout << "Original mtime: " << gz->mtime() << "\n";
            std::cout << "Zip comment: " << gz->comment() << "\n";
        }
    }
    catch(const boost::iostreams::gzip_error& e) {
         std::cout << e.what() << '\n';
    }
}

使用

准备样本文件
gzip testj.txt
mv testj.txt.gz file.gz

打印

Processed line Hello world
Original filename: testj.txt
Original mtime: 1518987084
Zip comment: