2011-10-27 22 views
2

我必須處理用gzip壓縮的大文件。我需要訪問行的一個子集,不一定按順序。因此,我想在我感興趣的線路上記錄流位置時,一次遍歷所有文件。然後,使用這些流位置快速檢索我需要的信息。我使用gzstream。但不幸的是tellg似乎不使用此包裝工作:如何在使用gzstream和gzip文件時保持流位置?

#include <iostream> 
#include <fstream> 
using namespace std; 

#include <gzstream.h> 

int main (int argc, char ** argv) 
{ 
    string inFile; 
    string line; 

    system ("rm -f infile1.txt; echo \"toto1\ntoto2\ntoto3\" > infile1.txt"); 
    inFile = "infile1.txt"; 
    ifstream inStream; 
    inStream.open (inFile.c_str()); 
    cout << inStream.tellg() << endl; 
    getline (inStream, line); 
    cout << inStream.tellg() << endl; 
    inStream.close(); 

    system ("rm -f infile1.gz; echo \"toto1\ntoto2\ntoto3\" | gzip > infile1.gz"); 
    inFile = "infile1.gz"; 
    igzstream igzStream; 
    igzStream.open (inFile.c_str()); 
    cout << igzStream.tellg() << endl; 
    getline (igzStream, line); 
    cout << igzStream.tellg() << endl; 
    igzStream.close(); 

    return 0; 
} 

此代碼返回此:

$ gcc -Wall test.cpp -lstdc++ -lgzstream -lz 
$ ./a.out 
0 
6 
18446744073709551615 
18446744073709551615 

有沒有一種方法,使與igzstream這項工作?或者我應該用Boost gzip filters代替?任何代碼片段將不勝感激;)

回答

0

gzstream不支持在一個文件中尋找,這不是一個gzip文件中特別有效的操作。你可以看看這個問題和它的答案:Random access gzip stream

其中一個答案給出了一個來自zlib源代碼的示例代碼的鏈接,你可以用它來幫助你在gzstream中實現你想要的功能。另一個答案提出了一種支持更高效尋找的變體壓縮格式。

Boost iostream可能支持尋找,但gzstream相當容易使用和修改,所以我傾向於堅持。

+0

謝謝。的確,提供變體壓縮格式(變體但非常接近gzip)的答案非常有用。更詳細的答案可以在這裏找到:http://biostar.stackexchange.com/questions/13627/random-access-of-lines-in-a-compressed-file-having-a-custom-tabulated-format/15098# 15098 – tflutre

相關問題