compress

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2018 License: Apache-2.0 Imports: 4 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

View Source
const BufSize = 4096

BufSize in bytes of read buffer.

Variables

This section is empty.

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(writer io.Writer) (Encoder, error)
	NewDecoder(reader io.Reader) (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
}

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
}

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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