compress

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2018 License: Apache-2.0 Imports: 5 Imported by: 0

README

GoDoc Go Report Card License Build Status codecov

compress

Package provides a generic interface to compress and un-compress

Example

package main
  
import (
        "flag"
        "fmt"
        "io/ioutil"
        "log"
        "os"
        "strings"

        "github.com/mickep76/compress"
        _ "github.com/mickep76/compress/gzip"
        _ "github.com/mickep76/compress/lzw"
        _ "github.com/mickep76/compress/snappy"
        _ "github.com/mickep76/compress/xz"
        _ "github.com/mickep76/compress/zlib"
)

func usage() {
        fmt.Printf("Usage: example [options] file\n\nOptions:\n")
        flag.PrintDefaults()
        os.Exit(0)
}

func main() {
        out := flag.String("out", "example", "Output.")
        algo := flag.String("algo", "gzip", fmt.Sprintf("Algorithms: [%s].", strings.Join(compress.Algorithms(), ", ")))
        dec := flag.Bool("dec", false, "Decode.")

        flag.Parse()

        if len(flag.Args()) < 1 {
                usage()
        }
        file := flag.Args()[0]

        if *dec {
                encoded, err := ioutil.ReadFile(file)
                if err != nil {
                        log.Fatal(err)
                }

                b, err := compress.Decode(*algo, encoded)
                if err != nil {
                        log.Fatal(err)
                }

                fmt.Print(string(b))
        } else {
                b, err := ioutil.ReadFile(file)
                if err != nil {
                        log.Fatal(err)
                }

                encoded, err := compress.Encode(*algo, b)
                if err != nil {
                        log.Fatal(err)
                }

                if err := ioutil.WriteFile(*out+"."+*algo, encoded, 0644); err != nil {
                        log.Fatal(err)
                }
        }
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrUnsupportedOption unsupported option
	ErrUnsupportedOption = errors.New("unsupported option")
)

Functions

func Algorithms

func Algorithms() []string

Algorithms registered.

func Decode

func Decode(algo string, encoded []byte, opts ...DecoderOption) ([]byte, error)

Decode method.

func Encode

func Encode(algo string, v []byte, opts ...EncoderOption) ([]byte, error)

Encode method.

func Register

func Register(name string, algorithm Algorithm)

Register algorithm.

Types

type Algorithm

type Algorithm interface {
	NewEncoder(w io.Writer, opts ...EncoderOption) (Encoder, error)
	NewDecoder(r io.Reader, opts ...DecoderOption) (Decoder, error)
}

Algorithm interface.

func Registered

func Registered(name string) (Algorithm, error)

Registered algorithm.

type Decoder

type Decoder interface {
	Read(v []byte) (int, error)
	Close() error
	SetOrder(o int) error
	SetLitWidth(w int) error
}

Decoder interface.

func NewDecoder

func NewDecoder(algo string, r io.Reader, opts ...DecoderOption) (Decoder, error)

NewDecoder variadic constructor.

type DecoderOption

type DecoderOption func(Decoder) error

DecoderOption function.

type Encoder

type Encoder interface {
	Write(v []byte) (int, error)
	Close() error
	SetEndian(endian Endian) error
	SetLitWidth(width int) error
	SetLevel(level Level) error
}

Encoder interface.

func NewEncoder

func NewEncoder(algo string, w io.Writer, opts ...EncoderOption) (Encoder, error)

NewEncoder variadic constructor.

type EncoderOption

type EncoderOption func(Encoder) error

EncoderOption variadic function.

func WithEndian added in v1.1.0

func WithEndian(endian Endian) EncoderOption

WithEndian either MSB (most significant byte) or LSB (least significant byte). Supported by lzw.

func WithLevel added in v1.1.0

func WithLevel(level Level) EncoderOption

WithLevel compression level. Supported by gzip, zlib.

func WithLitWidth added in v1.1.0

func WithLitWidth(width int) EncoderOption

WithLitWidth the number of bit's to use for literal codes. Supported by lzw.

type Endian added in v1.1.0

type Endian int

Endian the order in which bytes are arranged into larger values.

const (
	// Little endian LSB (Least Significant Bit).
	Little Endian = 0

	// Big endian MSB (Most Significant Bit).
	Big Endian = 1
)

type Level added in v1.1.0

type Level int

Level compression level.

const (
	// NoCompression no compression.
	NoCompression Level = 0

	// BestSpeed best speed.
	BestSpeed Level = 1

	// BestCompression best compression.
	BestCompression Level = 9

	// DefaultCompression default compression.
	DefaultCompression Level = -1

	// HuffmanOnly huffman only.
	HuffmanOnly Level = -2
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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