簡體   English   中英

如何從 boost 的 gzip_decompressor() 獲取 gzip_params

[英]How to get gzip_params from boost's gzip_decompressor()

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

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

很好的問題。

解決方案是使用component<N, T>來獲取指向實際解壓器實例的指針:

住在 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: 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM