简体   繁体   English

用C ++编写gzip压缩文件流

[英]gzip compressed file streams in C++

My loader looks like that: 我的装载机看起来像这样:

void LispAna::loadF() {
    formatives.rehash(2991);
    std::ifstream fin("data.txt");
    formCondition st;

    std::string key;
    std::string tmp;
    int itmp;

    for (std::string row; getline(fin, row, '\n');) {
        //"a\t0\txx\tA\n"
        std::istringstream ss(row);

        getline(ss, key, '\t');

        //pos
        getline(ss, tmp, '\t');
        st.pos = *(tmp.c_str());

        //types
        getline(ss, tmp, '\t');
        st.types = tmp;

        //condition
        getline(ss, tmp, '\t');
        st.condition = tmp;

        //if(ok)?
        formatives.insert(std::pair<std::string, formCondition>(key, st));
    }
}

Id like to replace std::ifstream witch own made zifstream . 我喜欢替换std::ifstream巫婆自己创造的zifstream That can easily used for reading zlib compressed text files. 这可以很容易地用于读取zlib压缩文本文件。

zifstream fin("data.txt.gz");
//..
for (std::string row; getline(fin, row, '\n');) {
//..
}

I have tinfl.c v1.11. 我有tinfl.c v1.11。 And I dont have a clue witch class to extend and what functions to implement to achieve that. 而且我没有一个线索女巫类来扩展以及实现它的实现功能。 I dont need seek. 我不需要寻求。 Just simple linear reading and some basic error checking. 只需简单的线性读数和一些基本的错误检查。 Don't even need full header support, data files are own made. 甚至不需要完整的头支持,数据文件是自己做的。

First, I think Boost already has what you are looking for. 首先,我认为Boost已经拥有了你想要的东西。 If you can use it, it would certainly be simpler than rewriting the code yourself. 如果你可以使用它,那肯定比自己重写代码更简单。

Otherwise: you'll need to define a filtering streambuf which does the compression, and forwards the compressed data on to another streambuf (almost certainly a filebuf opened in binary mode). 否则:您需要定义一个进行压缩的过滤streambuf,并将压缩数据转发到另一个streambuf(几乎可以肯定是以二进制模式打开的filebuf )。 Just derive from std::streambuf , with a pointer to the final target streambuf in your class. 只需从std::streambuf派生,指向您班级中的最终目标streambuf At the least, you'll have to overwrite the function overflow ; 至少,你必须覆盖函数overflow ; you might want to override xsputn as well. 您可能还想覆盖xsputn

The actual zifstream is then very simple: something like: 实际的zifstream非常简单:类似于:

class zifstream : private std::filebuf, private zistreambuf, public std::istream
{
public:
    zifstream( std::string const& filename )
        : std::filebuf( filename.c_str(), std::ios_base::in | std::ios_base::binary )
        , zistreambuf( static_cast<std::filebuf>( this ) )
        , std::istream( static_cast<zistreambuf>( this ) )
    {
    }
};

is likely sufficient. 可能就足够了。 (The standard classes which derive from std::istream also provide a rdbuf function which hides the one in the base class, and returns the derived type. This is more a "nice to have", in a few particular cases, and not really necessary.) (从std::istream派生的标准类还提供了一个rdbuf函数,该函数隐藏了基类中的一个,并返回派生类型。在一些特殊情况下,这更像是“很好”,而不是真的必要。)

Note that if you're reading using locales other than "C" , you'll have to do the code translation in the filtering buffer; 请注意,如果您使用"C"以外的区域设置进行阅读,则必须在过滤缓冲区中进行代码转换; doing it in the filebuf will not work. 在filebuf中执行它将无法正常工作。 (Again, Boost has the necessary support.) (再一次,Boost有必要的支持。)

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

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