compress

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2018 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AlgoNone represents a ,,uncompressed” algorithm.
	AlgoNone = iota

	// AlgoSnappy represents the snappy compression algorithm:
	// https://en.wikipedia.org/wiki/Snappy_(software)
	AlgoSnappy

	//AlgoLZ4 represents the lz4 compression algorithm:
	// https://en.wikipedia.org/wiki/LZ4_(compression_algorithm)
	AlgoLZ4
)
View Source
const (
	// HeaderSizeThreshold is the number of bytes needed to enable compression at all.
	HeaderSizeThreshold = 2048
)

Variables

View Source
var (
	// ErrBadIndex is returned on invalid compression index.
	ErrBadIndex = errors.New("Broken compression index")

	// ErrHeaderTooSmall is returned if the header is less than 10 bytes.
	// It usually indicates a broken file or a non-compressed file.
	ErrHeaderTooSmall = errors.New("Header is less than 10 bytes")

	// ErrBadMagicNumber is returned if the first 8 bytes of the stream is not
	// the expected "elchwald".
	ErrBadMagicNumber = errors.New("Bad magic number in compressed stream")

	// ErrBadAlgorithm is returned when the algorithm was either not present
	// or it had an invalid value
	ErrBadAlgorithm = errors.New("Invalid algorithm")

	// ErrUnsupportedVersion is returned when we don't have a reader that
	// understands that format.
	ErrUnsupportedVersion = errors.New("Version of this format is not supported")
)
View Source
var (
	// AlgoMap is a map of available algorithms.
	AlgoMap = map[AlgorithmType]Algorithm{
		AlgoNone:   noneAlgo{},
		AlgoSnappy: snappyAlgo{},
		AlgoLZ4:    lz4Algo{},
	}
)
View Source
var CompressibleMapping = map[string]bool{}/* 636 elements not displayed */

CompressibleMapping maps between mime types and a bool indicating if they're compressible. Choice of Algorithm comes later.

This was converted from this mime db: https://cdn.rawgit.com/jshttp/mime-db/master/db.json

View Source
var (
	// ErrBadAlgo is returned on a unsupported/unknown algorithm.
	ErrBadAlgo = errors.New("Invalid algorithm type")
)
View Source
var (
	// TextFileExtensions not covered by mime.TypeByExtension
	TextFileExtensions = map[string]bool{
		".go":   true,
		".json": true,
		".yaml": true,
		".xml":  true,
		".txt":  true,
	}
)

Functions

func AlgoToString

func AlgoToString(a AlgorithmType) string

AlgoToString converts a algorithm type to a string.

func Pack

func Pack(data []byte, algo AlgorithmType) ([]byte, error)

Pack compresses `data` with `algo` and returns the resulting data. This is a convinience method meant to be used for small data packages.

func Unpack

func Unpack(data []byte) ([]byte, error)

Unpack unpacks `data` and returns the decompressed data. The algorithm is read from the data itself. This is a convinience method meant to be used for small data packages.

Types

type Algorithm

type Algorithm interface {
	Encode([]byte) ([]byte, error)
	Decode([]byte) ([]byte, error)
}

Algorithm is the common interface for all supported algorithms.

func AlgorithmFromType

func AlgorithmFromType(a AlgorithmType) (Algorithm, error)

AlgorithmFromType returns a interface to the given AlgorithmType.

type AlgorithmType

type AlgorithmType byte

AlgorithmType user defined type to store the algorithm type.

func AlgoFromString

func AlgoFromString(s string) (AlgorithmType, error)

AlgoFromString tries to convert a string to AlgorithmType

func GuessAlgorithm

func GuessAlgorithm(path string, header []byte) (AlgorithmType, error)

GuessAlgorithm takes the path name and the header data of it and tries to guess a suitable compression algorithm.

func (AlgorithmType) IsValid

func (at AlgorithmType) IsValid() bool

IsValid returns true if `at` is a valid algorithm type.

func (AlgorithmType) String

func (at AlgorithmType) String() string

type Reader added in v0.2.0

type Reader struct {
	// contains filtered or unexported fields
}

Reader implements an decompressing reader

func NewReader

func NewReader(r io.ReadSeeker) *Reader

NewReader returns a new ReadSeeker with compression support. As random access is the purpose of this layer, a ReadSeeker is required as parameter. The used compression algorithm is chosen based on trailer information.

func (*Reader) Read added in v0.2.0

func (r *Reader) Read(p []byte) (int, error)

Read reads len(p) bytes from the compressed stream into p.

func (*Reader) Seek added in v0.2.0

func (r *Reader) Seek(destOff int64, whence int) (int64, error)

Seek implements io.Seeker

func (*Reader) WriteTo added in v0.2.0

func (r *Reader) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo

type Writer added in v0.2.0

type Writer struct {
	// contains filtered or unexported fields
}

Writer implements a compression writer.

func NewWriter

func NewWriter(w io.Writer, algoType AlgorithmType) (*Writer, error)

NewWriter returns a WriteCloser with compression support.

func (*Writer) Close added in v0.2.0

func (w *Writer) Close() error

Close cleans up internal resources. Make sure to call close always since it might write data.

func (*Writer) ReadFrom added in v0.2.0

func (w *Writer) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom

func (*Writer) Write added in v0.2.0

func (w *Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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