boost filtering_istream gzip_decompressor未压缩文件大小

时间:2009-11-19 15:26:44

标签: c++ boost gzip

我正在使用boost过滤流对象来读取gzip压缩文件。效果很好! 我想显示已处理文件数量的进度条。我需要找到输入未压缩的文件大小。 gzip解压缩程序是否可以从gzip压缩文件访问原始文件大小?我在boost gzip_decompressor reference页面上找不到它。真的是进度对话框是目标,还有另一种方法可以找出压缩文件中的位置吗?

    // gets compressed file size, need uncompressed size
    boost::uintmax_t fs = boost::filesystem::file_size (
        boost::filesystem::path (fname)
        );

    std::ifstream file (fname, std::ios_base::in | std::ios_base::binary);
    boost::iostreams::filtering_istream in;
    in.push (boost::iostreams::gzip_decompressor());
    in.push (file);

    std::string line;
    size_t bytes_read = 0;
    while (in)
    {
        std::getline (in, line);
        bytes_read += line.size ();
        // progress dlg with bytes_read / uncompressed size
    }

1 个答案:

答案 0 :(得分:4)

您所追求的信息肯定存在(未压缩的数据大小记录在gzip文件的最后4个字节中,(参见GZIP spec),但要查看boost库的标题(见{{ 3}})它没有暴露在任何地方。它看起来唯一的地方就是在进行检查以确保read_footer中没有损坏时。 您可以直接从文件中读取值(只需将最后4个字节组装成int,注意它们的排序(请参阅GZIP规范)),或使用不同的库进行解压缩。