gzran

package module
v0.0.0-...-7b631e5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2020 License: Apache-2.0 Imports: 11 Imported by: 0

README

gzran

GoDoc Build Status Coverage Status Go Report Card

Package gzran reads arbitrary offsets of uncompresssed data from compressed gzip files. This is accomplished by saving decompressor state periodically as the Reader progresses on the fly. The built Index can then be used to seek back to points within the file efficiently, or serialized and then used later.

Gzran is based on the c library, zran, by Mark Adler: https://github.com/madler/zlib/blob/master/examples/zran.c

Example

gzr, _ := gzran.NewReader(r)
// Seek forward within the file, building index 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)
}

// Read through entire file to index it, and then save the Index to a file.
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, _ := gzran.NewReader(r)
gzr.Index, _ = gzran.LoadIndex(f)
// Seek and read as desired using the Index.

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

View Source
const DefaultIndexInterval = 1024 * 1024 // 1 MB

DefaultIndexInterval is how often the reader will save decompressor state by default.

Variables

View Source
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 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.

func LoadIndex

func LoadIndex(r io.Reader) (Index, error)

LoadIndex deserializes an Index from the given io.Reader.

func (Index) WriteTo

func (idx Index) WriteTo(w io.Writer) error

WriteTo serializes the index to the given io.Writer. It can be deserialized with LoadIndex.

type Point

type Point struct {
	CompressedOffset   int64
	UncompressedOffset int64
	DecompressorState  []byte
}

Point holds the decompressor state at a given offset within the uncompressed data.

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

func (z *Reader) Close() error

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

func (z *Reader) Read(p []byte) (n int, err error)

Read implements io.Reader, reading uncompressed bytes from its underlying Reader.

func (*Reader) Seek

func (z *Reader) Seek(offset int64, whence int) (position int64, err error)

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.

Directories

Path Synopsis
internal
flate
Package flate implements the DEFLATE compressed data format, described in RFC 1951.
Package flate implements the DEFLATE compressed data format, described in RFC 1951.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL