improve gzip feature for gcompress; add gzip compression for package gres

This commit is contained in:
John
2020-01-15 00:15:56 +08:00
parent 22af5be71f
commit edf2366296
13 changed files with 177 additions and 121 deletions

View File

@ -6,78 +6,3 @@
// Package gcompress provides kinds of compression algorithms for binary/bytes data.
package gcompress
import (
"bytes"
"compress/gzip"
"compress/zlib"
"io"
)
// Zlib compresses <data> with zlib algorithm.
func Zlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
var in bytes.Buffer
var err error
w := zlib.NewWriter(&in)
if _, err = w.Write(data); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return in.Bytes(), err
}
return in.Bytes(), nil
}
// UnZlib decompresses <data> with zlib algorithm.
func UnZlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
b := bytes.NewReader(data)
var out bytes.Buffer
var err error
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
if _, err = io.Copy(&out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}
// Gzip compresses <data> with gzip algorithm.
func Gzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
var err error
zip := gzip.NewWriter(&buf)
_, err = zip.Write(data)
if err != nil {
return nil, err
}
if err = zip.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnGzip decompresses <data> with gzip algorithm.
func UnGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
content := bytes.NewReader(data)
zipData, err := gzip.NewReader(content)
if err != nil {
return nil, err
}
if _, err = io.Copy(&buf, zipData); err != nil {
return nil, err
}
if err = zipData.Close(); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}

View File

@ -0,0 +1,55 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gcompress
import (
"bytes"
"compress/gzip"
"io"
)
// Gzip compresses <data> with gzip algorithm.
// The optional parameter <level> specifies the compression level from
// 1 to 9 which means from none to the best compression.
//
// Note that it returns error if given <level> is invalid.
func Gzip(data []byte, level ...int) ([]byte, error) {
var writer *gzip.Writer
var buf bytes.Buffer
var err error
if len(level) > 0 {
writer, err = gzip.NewWriterLevel(&buf, level[0])
if err != nil {
return nil, err
}
} else {
writer = gzip.NewWriter(&buf)
}
if _, err = writer.Write(data); err != nil {
return nil, err
}
if err = writer.Close(); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
// UnGzip decompresses <data> with gzip algorithm.
func UnGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
reader, err := gzip.NewReader(bytes.NewReader(data))
if err != nil {
return nil, err
}
if _, err = io.Copy(&buf, reader); err != nil {
return nil, err
}
if err = reader.Close(); err != nil {
return buf.Bytes(), err
}
return buf.Bytes(), nil
}

View File

@ -24,7 +24,8 @@ import (
// ZipPath compresses <paths> to <dest> using zip compressing algorithm.
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
//
// Note that parameter <paths> supports multiple paths join with ','.
// Note that the parameter <paths> can be either a directory or a file, which
// supports multiple paths join with ','.
func ZipPath(paths, dest string, prefix ...string) error {
writer, err := os.Create(dest)
if err != nil {
@ -37,7 +38,8 @@ func ZipPath(paths, dest string, prefix ...string) error {
// ZipPathWriter compresses <paths> to <writer> using zip compressing algorithm.
// The unnecessary parameter <prefix> indicates the path prefix for zip file.
//
// Note that parameter <paths> supports multiple paths join with ','.
// Note that the parameter <paths> can be either a directory or a file, which
// supports multiple paths join with ','.
func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
zipWriter := zip.NewWriter(writer)
defer zipWriter.Close()
@ -51,13 +53,19 @@ func ZipPathWriter(paths string, writer io.Writer, prefix ...string) error {
}
func doZipPathWriter(path string, zipWriter *zip.Writer, prefix ...string) error {
var err error
var files []string
realPath, err := gfile.Search(path)
if err != nil {
return err
}
files, err := gfile.ScanDir(path, "*", true)
if err != nil {
return err
if gfile.IsDir(path) {
files, err = gfile.ScanDir(path, "*", true)
if err != nil {
return err
}
} else {
files = []string{path}
}
headerPrefix := ""
if len(prefix) > 0 && prefix[0] != "" {

View File

@ -0,0 +1,50 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
// Package gcompress provides kinds of compression algorithms for binary/bytes data.
package gcompress
import (
"bytes"
"compress/zlib"
"io"
)
// Zlib compresses <data> with zlib algorithm.
func Zlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
var in bytes.Buffer
var err error
w := zlib.NewWriter(&in)
if _, err = w.Write(data); err != nil {
return nil, err
}
if err = w.Close(); err != nil {
return in.Bytes(), err
}
return in.Bytes(), nil
}
// UnZlib decompresses <data> with zlib algorithm.
func UnZlib(data []byte) ([]byte, error) {
if data == nil || len(data) < 13 {
return data, nil
}
b := bytes.NewReader(data)
var out bytes.Buffer
var err error
r, err := zlib.NewReader(b)
if err != nil {
return nil, err
}
if _, err = io.Copy(&out, r); err != nil {
return nil, err
}
return out.Bytes(), nil
}