Skip to content

Commit

Permalink
🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
whatwewant committed May 31, 2022
0 parents commit 589625d
Show file tree
Hide file tree
Showing 18 changed files with 370 additions and 0 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Go

on:
push:
branches: [ master ]
tags:
- v*
pull_request:
branches: [ master ]

jobs:

ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18

- name: install deps
run: |
go mod tidy
go install golang.org/x/tools/cmd/goimports@latest
go install golang.org/x/lint/golint@latest
go install github.com/mattn/goveralls@latest
- name: static analysis
run: |
golint -set_exit_status
go vet
test -z "$(goimports -l .)"
- name: Test
run: goveralls -service=github
env:
COVERALLS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28 changes: 28 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@


# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work

# Common
.env

*.db
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 GoZooX

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# compress
> Simply compressor, support zlib/gzip/flate/lzw.
[![PkgGoDev](https://pkg.go.dev/badge/github.com/go-zoox/compress)](https://pkg.go.dev/github.com/go-zoox/compress)
[![Build Status](https://github.com/go-zoox/compress/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/go-zoox/compress/actions/workflows/ci.yml)
[![Go Report Card](https://goreportcard.com/badge/github.com/go-zoox/compress)](https://goreportcard.com/report/github.com/go-zoox/compress)
[![Coverage Status](https://coveralls.io/repos/github/go-zoox/compress/badge.svg?branch=master)](https://coveralls.io/github/go-zoox/compress?branch=master)
[![GitHub issues](https://img.shields.io/github/issues/go-zoox/compress.svg)](https://github.com/go-zoox/compress/issues)
[![Release](https://img.shields.io/github/tag/go-zoox/compress.svg?label=Release)](https://github.com/go-zoox/compress/tags)

## Installation
To install the package, run:
```bash
go get github.com/go-zoox/compress
```

## Getting Started

```go
import (
"testing"
"github.com/go-zoox/compress"
)

func main(t *testing.T) {
gzip := compress.NewGzip()
bytes := gzip.Compress([]byte("hello world"))
fmt.Println(string(gzip.Decompress(bytes)))
}
```

## Inspired By
* [cosiner/gohper](https://github.com/cosiner/gohper/blob/master/encoding/encoding.go) - common libs here

## License
GoZoox is released under the [MIT License](./LICENSE).
29 changes: 29 additions & 0 deletions compress.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package compress

import (
"github.com/go-zoox/compress/compressor"
"github.com/go-zoox/compress/flate"
"github.com/go-zoox/compress/gzip"
"github.com/go-zoox/compress/lzw"
"github.com/go-zoox/compress/zlib"
)

// NewZlib creates a zlib compressor.
func NewZlib(bufSize ...int) *compressor.Compressor {
return zlib.New(bufSize...)
}

// NewGzip creates a gzip compressor.
func NewGzip(bufSize ...int) *compressor.Compressor {
return gzip.New(bufSize...)
}

// NewFlate creates a flate compressor.
func NewFlate(bufSize ...int) *compressor.Compressor {
return flate.New(bufSize...)
}

// NewLZW creates a lzw compressor.
func NewLZW(bufSize ...int) *compressor.Compressor {
return lzw.New(bufSize...)
}
37 changes: 37 additions & 0 deletions compressor/compressor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package compressor

import (
"bytes"
"io"
"io/ioutil"
)

// Compressor is a common interface for all compressions.
type Compressor struct {
BufSize int
NewWriter func(io.Writer) io.WriteCloser
NewReader func(io.Reader) (io.ReadCloser, error)
}

// Compress encodes the raw bytes.
func (c *Compressor) Compress(src []byte) []byte {
buf := bytes.NewBuffer(make([]byte, 0, c.BufSize))
w := c.NewWriter(buf)
w.Write(src)
w.Close()

return buf.Bytes()
}

// Decompress decodes the encoded bytes.
func (c *Compressor) Decompress(src []byte) ([]byte, error) {
r, err := c.NewReader(bytes.NewReader(src))
if err != nil {
return nil, err
}

dst, err := ioutil.ReadAll(r)
r.Close()

return dst, err
}
1 change: 1 addition & 0 deletions compressor/compressor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package compressor
31 changes: 31 additions & 0 deletions flate/flate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package flate

import (
goflate "compress/flate"
"io"

"github.com/go-zoox/compress/compressor"
)

// New creates a flate compressor.
func New(bufSize ...int) *compressor.Compressor {
bufSizeX := 64
if len(bufSize) > 0 {
bufSizeX = bufSize[0]
}

return &compressor.Compressor{
BufSize: bufSizeX,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
closer := goflate.NewReader(r)
return closer, nil
},
NewWriter: func(w io.Writer) io.WriteCloser {
closer, err := goflate.NewWriter(w, goflate.BestCompression)
if err != nil {
panic(err)
}
return closer
},
}
}
16 changes: 16 additions & 0 deletions flate/flate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package flate

import "testing"

func TestFlate(t *testing.T) {
c := New()

src := "Hello, World!"
dst := c.Compress([]byte(src))

if decoded, err := c.Decompress(dst); err != nil {
t.Error(err)
} else if string(decoded) != src {
t.Errorf("%s != %s", string(decoded), src)
}
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/go-zoox/compress

go 1.18
Empty file added go.sum
Empty file.
26 changes: 26 additions & 0 deletions gzip/gzip.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gzip

import (
gogzip "compress/gzip"
"io"

"github.com/go-zoox/compress/compressor"
)

// New creates a gzip compressor.
func New(bufSize ...int) *compressor.Compressor {
bufSizeX := 64
if len(bufSize) > 0 {
bufSizeX = bufSize[0]
}

return &compressor.Compressor{
BufSize: bufSizeX,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
return gogzip.NewReader(r)
},
NewWriter: func(w io.Writer) io.WriteCloser {
return gogzip.NewWriter(w)
},
}
}
16 changes: 16 additions & 0 deletions gzip/gzip_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package gzip

import "testing"

func TestGzip(t *testing.T) {
c := New()

src := "Hello, World!"
dst := c.Compress([]byte(src))

if decoded, err := c.Decompress(dst); err != nil {
t.Error(err)
} else if string(decoded) != src {
t.Errorf("%s != %s", string(decoded), src)
}
}
27 changes: 27 additions & 0 deletions lzw/lzw.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package lzw

import (
golzw "compress/lzw"
"io"

"github.com/go-zoox/compress/compressor"
)

// New creates a lzw compressor.
func New(bufSize ...int) *compressor.Compressor {
bufSizeX := 64
if len(bufSize) > 0 {
bufSizeX = bufSize[0]
}

return &compressor.Compressor{
BufSize: bufSizeX,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
closer := golzw.NewReader(r, golzw.LSB, 8)
return closer, nil
},
NewWriter: func(w io.Writer) io.WriteCloser {
return golzw.NewWriter(w, golzw.LSB, 8)
},
}
}
16 changes: 16 additions & 0 deletions lzw/lzw_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package lzw

import "testing"

func TestLzw(t *testing.T) {
c := New()

src := "Hello, World!"
dst := c.Compress([]byte(src))

if decoded, err := c.Decompress(dst); err != nil {
t.Error(err)
} else if string(decoded) != src {
t.Errorf("%s != %s", string(decoded), src)
}
}
4 changes: 4 additions & 0 deletions version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package compress

// Version is the current version of the package.
var Version = "1.0.0"
26 changes: 26 additions & 0 deletions zlib/zlib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package zlib

import (
gozlib "compress/zlib"
"io"

"github.com/go-zoox/compress/compressor"
)

// New creates a zlib compressor.
func New(bufSize ...int) *compressor.Compressor {
bufSizeX := 64
if len(bufSize) > 0 {
bufSizeX = bufSize[0]
}

return &compressor.Compressor{
BufSize: bufSizeX,
NewReader: func(r io.Reader) (io.ReadCloser, error) {
return gozlib.NewReader(r)
},
NewWriter: func(w io.Writer) io.WriteCloser {
return gozlib.NewWriter(w)
},
}
}
16 changes: 16 additions & 0 deletions zlib/zlib_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package zlib

import "testing"

func TestZlib(t *testing.T) {
c := New()

src := "Hello, World!"
dst := c.Compress([]byte(src))

if decoded, err := c.Decompress(dst); err != nil {
t.Error(err)
} else if string(decoded) != src {
t.Errorf("%s != %s", string(decoded), src)
}
}

0 comments on commit 589625d

Please sign in to comment.