compress

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2020 License: MIT Imports: 5 Imported by: 0

README

compress

GoDoc Build Status codecov Go Report Card GitHub release LICENSE

Package compress implements compressing and uncompressing in golang.

Feature

  • flate
  • zlib
  • gzip

Get started

Install
go get github.com/hslam/compress
Import
import "github.com/hslam/compress"
Usage
Example
package main

import (
	"fmt"
	"github.com/hslam/compress"
)

var readyBytes = []byte("DhPNExsTOYfCZrzQ2wPcHDHqtSJAS7gMLeqejz6ZvLf9QIZxoYGy8ooaJfS12mbUTeg0bCiwpdukKbYPP1LgxNulu7qQJMtr1WQ0boTtnJiRXsN6r0W7q7f3dh5lRZVVhwlVOupxD4D7i0YWKvsJGkSIATQtZGsPcOBKspKN6vu3ugxaO8Jv2jYtm3MB2RwokaXlRjr37whZyr8Tam3rM9OGinGfSKCwUeTzLhg2dz83slIsCdO3lqzkx9iBWyaBtDebfYN465CAndAJN1JqFzhRfaXcw9KNPBUHn3CGioQTTrWzrIWiHTi70zPDSiFRjmLQdJKKgCpVgtk20DHFcFme7dUlV4l5RFWHUoNea9FUN1cFvnyZVYpCnZz3uPgpa1dE3DkSodQMAYJZOpeB29EnyYj4UKvDehxqVAwgDyMS82PXbbi4H6UPiwOX9IomXGvL8jUt441ENrfXObR6kEjSstjnQ6pEX9VchH66cMlFE4qgRUkvKFIycfw5F208")

func main() {
	Flate()
	Zlib()
	Gzip()
}

//Flate Example
func Flate() {
	compressor := &compress.Compressor{Type: compress.Flate, Level: compress.DefaultCompression}
	compressBytes := compressor.Compress(readyBytes)
	uncompressBytes := compressor.Uncompress(compressBytes)
	fmt.Printf("flat %d->%d->%d\n", len(readyBytes), len(compressBytes), len(uncompressBytes))
}

//Zlib Example
func Zlib() {
	compressor := &compress.Compressor{Type: compress.Zlib, Level: compress.DefaultCompression}
	compressBytes := compressor.Compress(readyBytes)
	uncompressBytes := compressor.Uncompress(compressBytes)
	fmt.Printf("zlib %d->%d->%d\n", len(readyBytes), len(compressBytes), len(uncompressBytes))
}

//Gzip Example
func Gzip() {
	compressor := &compress.Compressor{Type: compress.Gzip, Level: compress.DefaultCompression}
	compressBytes := compressor.Compress(readyBytes)
	uncompressBytes := compressor.Uncompress(compressBytes)
	fmt.Printf("gzip %d->%d->%d\n", len(readyBytes), len(compressBytes), len(uncompressBytes))
}
Output
flat 512->410->512
zlib 512->412->512
gzip 512->420->512
Benchmark

go test -v -run="none" -bench=. -benchtime=1s -benchmem

goos: darwin
goarch: amd64
pkg: github.com/hslam/compress
BenchmarkFlate-4   	    6006	    194793 ns/op	  857312 B/op	      27 allocs/op
BenchmarkZlib-4    	    6276	    199555 ns/op	  858042 B/op	      33 allocs/op
BenchmarkGzip-4    	    6139	    197678 ns/op	  858720 B/op	      30 allocs/op
PASS
ok  	github.com/hslam/compress	4.902s
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Authors

compress was written by Meng Huang.

Documentation

Overview

Package compress implements compressing and uncompressing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Compressor

type Compressor struct {
	Type  Type
	Level Level
}

Compressor defines the struct of compressor.

func (*Compressor) Compress

func (c *Compressor) Compress(data []byte) []byte

Compress returns the compressed data.

func (*Compressor) Uncompress

func (c *Compressor) Uncompress(data []byte) []byte

Uncompress returns the uncompressed data.

type Level

type Level int

Level defines the level for compression. Higher levels typically run slower but compress more.

const (
	//NoCompression does not attempt any compression.
	NoCompression Level = 0
	//BestSpeed defines the level of best speed.
	BestSpeed Level = 1
	//Level2 defines the level 2.
	Level2 Level = 2
	//Level3 defines the level 3.
	Level3 Level = 3
	//Level4 defines the level 4.
	Level4 Level = 4
	//Level5 defines the level 5.
	Level5 Level = 5
	//Level6 defines the level 6.
	Level6 Level = 6
	//Level7 defines the level 7.
	Level7 Level = 7
	//Level8 defines the level 8.
	Level8 Level = 8
	//BestCompression defines the level of best compression.
	BestCompression Level = 9
	//DefaultCompression uses the default compression level.
	DefaultCompression Level = -1
	// HuffmanOnly will use Huffman compression only, giving
	// a very fast compression for all types of input,
	// but sacrificing considerable compression efficiency.
	HuffmanOnly Level = -2
)

type Reader

type Reader interface {
	Read(p []byte) (n int, err error)
	Close() error
}

Reader defines the reader interface for uncompressing data.

type Type

type Type string

Type defines the type for compression.

const (
	//Flate defines the compressor type of flate .
	Flate Type = "flate"
	//Zlib defines the compressor type of zlib .
	Zlib Type = "zlib"
	//Gzip defines the compressor type of gzip .
	Gzip Type = "gzip"
)

type Writer

type Writer interface {
	Write(data []byte) (n int, err error)
	Flush() error
	Close() error
}

Writer defines the writer interface for compressing data.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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