Documentation ¶
Overview ¶
Package gzran implements a seekable gzip.Reader that indexes offsets within the file as reading progresses, to make subsequent seeking more performant.
gzr, err := gzran.NewReader(r) if err != nil { panic(err) } // Seek forward within the file, indexing as we go. if _, err := gzr.Seek(n, io.SeekStart); err != nil { panic(err) } // Seek backward, using the on-the-fly index to do so efficiently. if _, err := gzr.Seek(n - 128000, io.SeekStart); err != nil { panic(err) }
The Index can also be persisted and reused later:
// Read through entire file to index it, and then save the Index. if _, err := io.Copy(ioutil.Discard, gzr); err != nil { panic(err) } if err := gzr.Index.WriteTo(f); err != nil { panic(err) } // Create a new gzip.Reader and load the Index to use it. gzr, err := gzran.NewReader(r) if err != nil { panic(err) } gzr.Index, err = gzran.LoadIndex(f) if err != nil { panic(err) } // Seek and read as desired using the Index.
Index ¶
Constants ¶
const DefaultIndexInterval = 1024 * 1024 // 1 MB
DefaultIndexInterval is how often the reader will save decompressor state by default.
Variables ¶
var ( // ErrChecksum is returned when reading GZIP data that has an invalid checksum. ErrChecksum = errors.New("gzip: invalid checksum") // ErrHeader is returned when reading GZIP data that has an invalid header. ErrHeader = errors.New("gzip: invalid header") // ErrInvalidSeek is returned if attempting to seek prior to beginning of the file. ErrInvalidSeek = errors.New("gzseek: attempting to seek before beginning of file") // ErrUnimplementedSeek is returned if attempting to seek from the end of the file. ErrUnimplementedSeek = errors.New("gzseek: seek from SeekEnd is not implemented") )
Functions ¶
This section is empty.
Types ¶
type Header ¶
type Header struct { Comment string // comment Extra []byte // "extra data" ModTime time.Time // modification time Name string // file name OS byte // operating system type }
The gzip file stores a header giving metadata about the compressed file. That header is exposed as the fields of the Writer and Reader structs.
Strings must be UTF-8 encoded and may only contain Unicode code points U+0001 through U+00FF, due to limitations of the GZIP file format.
type Index ¶
type Index []Point
Index collects decompressor state at offset Points. gzseek.Reader adds points to the index on the fly as decompression proceeds.
type Reader ¶
type Reader struct { Header // valid after NewReader Index // valid after NewReader // contains filtered or unexported fields }
A Reader is an io.Reader that can be read to retrieve uncompressed data from a gzip-format compressed file.
In general, a gzip file can be a concatenation of gzip files, each with its own header. Reads from the Reader return the concatenation of the uncompressed data of each. Only the first header is recorded in the Reader fields.
Gzip files store a length and checksum of the uncompressed data. The Reader will return an ErrChecksum when Read reaches the end of the uncompressed data if it does not have the expected length or checksum. Clients should treat data returned by Read as tentative until they receive the io.EOF marking the end of the data.
func NewReader ¶
func NewReader(r io.ReadSeeker) (*Reader, error)
NewReader creates a new Reader reading the given reader and default index interval. If r does not also implement io.ByteReader, the decompressor may read more data than necessary from r.
It is the caller's responsibility to call Close on the Reader when done.
The Reader.Header fields will be valid in the Reader returned.
func NewReaderInterval ¶
func NewReaderInterval(r io.ReadSeeker, indexInterval int64) (*Reader, error)
NewReaderInterval creates a new Reader consuming the given reader and checkpointing decompressor state at the given index interval. If r does not also implement io.ByteReader, the decompressor may read more data than necessary from r.
It is the caller's responsibility to call Close on the Reader when done.
The Reader.Header fields will be valid in the Reader returned.
func (*Reader) Close ¶
Close closes the Reader. It does not close the underlying io.Reader. In order for the GZIP checksum to be verified, the reader must be fully consumed until the io.EOF.
func (*Reader) Read ¶
Read implements io.Reader, reading uncompressed bytes from its underlying Reader.
func (*Reader) Seek ¶
Seek implements io.Seeker. The gzip file will be decompressed as needed to seek forward, building an index of offsets as it does so. Subsequent calls to seek will use the index to skip data more efficiently. Seeking from the end of the file is not implemented and will return ErrUnimplementedSeek.