一行一行地讀取 .gz 文字檔案

此類開啟 .gz 檔案(壓縮日誌檔案的常用格式),並在每次呼叫 .NextLine() 時返回一行

臨時解壓縮沒有記憶體使用,對大檔案非常有用。

Imports System.IO

Class logread_gz

  Private ptr As FileStream
  Private UnGZPtr As Compression.GZipStream
  Private line_ptr As StreamReader
  Private spath As String 

  Sub New(full_filename As String)
    spath = full_filename
  End Sub   

  Sub Open()
     Me.ptr = File.OpenRead(spath)
     Me.UnGZPtr = New Compression.GZipStream(ptr, Compression.CompressionMode.Decompress)
     Me.line_ptr = New StreamReader(UnGZPtr)
  End Sub()

  Function NextLine() As String
    'will return Nothing if EOF
    Return Me.line_ptr.ReadLine()
  End Function

  Sub Close()
    Me.line_ptr.Close()
    Me.line_ptr.Dispose()
    Me.UnGZPtr.Close()
    Me.UnGZPtr.Dispose()
    Me.ptr.Close()
    Me.ptr.Dispose()
  End Sub

End Class

注意:出於可讀性目的,沒有故障保護。