2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-01-15 00:15:56 +08:00
|
|
|
//
|
|
|
|
// 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"
|
2021-11-13 23:23:55 +08:00
|
|
|
|
2021-12-21 22:59:14 +08:00
|
|
|
"github.com/gogf/gf/v2/errors/gerror"
|
2021-11-13 23:23:55 +08:00
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
2020-01-15 00:15:56 +08:00
|
|
|
)
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// Gzip compresses `data` using gzip algorithm.
|
|
|
|
// The optional parameter `level` specifies the compression level from
|
2020-01-15 00:15:56 +08:00
|
|
|
// 1 to 9 which means from none to the best compression.
|
|
|
|
//
|
2021-10-21 18:22:47 +08:00
|
|
|
// Note that it returns error if given `level` is invalid.
|
2020-01-15 00:15:56 +08:00
|
|
|
func Gzip(data []byte, level ...int) ([]byte, error) {
|
2020-05-01 01:47:02 +08:00
|
|
|
var (
|
|
|
|
writer *gzip.Writer
|
|
|
|
buf bytes.Buffer
|
|
|
|
err error
|
|
|
|
)
|
2020-01-15 00:15:56 +08:00
|
|
|
if len(level) > 0 {
|
|
|
|
writer, err = gzip.NewWriterLevel(&buf, level[0])
|
|
|
|
if err != nil {
|
2021-12-23 00:09:00 +08:00
|
|
|
err = gerror.Wrapf(err, `gzip.NewWriterLevel failed for level "%d"`, level[0])
|
2020-01-15 00:15:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
writer = gzip.NewWriter(&buf)
|
|
|
|
}
|
|
|
|
if _, err = writer.Write(data); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `writer.Write failed`)
|
2020-01-15 00:15:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = writer.Close(); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `writer.Close failed`)
|
2020-01-15 00:15:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// GzipFile compresses the file `src` to `dst` using gzip algorithm.
|
2022-09-08 17:32:21 +08:00
|
|
|
func GzipFile(src, dst string, level ...int) (err error) {
|
|
|
|
dstFile, err := gfile.Create(dst)
|
2020-03-15 19:32:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 17:32:21 +08:00
|
|
|
defer dstFile.Close()
|
|
|
|
|
|
|
|
return GzipPathWriter(src, dstFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GzipPathWriter compresses `path` to `writer` using gzip compressing algorithm.
|
|
|
|
//
|
|
|
|
// Note that the parameter `path` can be either a directory or a file.
|
|
|
|
func GzipPathWriter(path string, writer io.Writer, level ...int) error {
|
|
|
|
var (
|
|
|
|
gzipWriter *gzip.Writer
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
srcFile, err := gfile.Open(path)
|
2020-03-15 19:32:26 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-09-08 17:32:21 +08:00
|
|
|
defer srcFile.Close()
|
2020-03-15 19:32:26 +08:00
|
|
|
|
|
|
|
if len(level) > 0 {
|
2022-09-08 17:32:21 +08:00
|
|
|
gzipWriter, err = gzip.NewWriterLevel(writer, level[0])
|
2020-03-15 19:32:26 +08:00
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `gzip.NewWriterLevel failed`)
|
2020-03-15 19:32:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-09-08 17:32:21 +08:00
|
|
|
gzipWriter = gzip.NewWriter(writer)
|
2020-03-15 19:32:26 +08:00
|
|
|
}
|
2022-09-08 17:32:21 +08:00
|
|
|
defer gzipWriter.Close()
|
2020-03-15 19:32:26 +08:00
|
|
|
|
2022-09-08 17:32:21 +08:00
|
|
|
if _, err = io.Copy(gzipWriter, srcFile); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `io.Copy failed`)
|
2020-03-15 19:32:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-21 18:22:47 +08:00
|
|
|
// UnGzip decompresses `data` with gzip algorithm.
|
2020-01-15 00:15:56 +08:00
|
|
|
func UnGzip(data []byte) ([]byte, error) {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
reader, err := gzip.NewReader(bytes.NewReader(data))
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `gzip.NewReader failed`)
|
2020-01-15 00:15:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if _, err = io.Copy(&buf, reader); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `io.Copy failed`)
|
2020-01-15 00:15:56 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = reader.Close(); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `reader.Close failed`)
|
2020-01-15 00:15:56 +08:00
|
|
|
return buf.Bytes(), err
|
|
|
|
}
|
|
|
|
return buf.Bytes(), nil
|
|
|
|
}
|
2020-03-15 19:32:26 +08:00
|
|
|
|
2021-11-13 23:23:55 +08:00
|
|
|
// UnGzipFile decompresses file `src` to `dst` using gzip algorithm.
|
2020-03-15 19:32:26 +08:00
|
|
|
func UnGzipFile(src, dst string) error {
|
|
|
|
srcFile, err := gfile.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer srcFile.Close()
|
|
|
|
dstFile, err := gfile.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer dstFile.Close()
|
|
|
|
|
|
|
|
reader, err := gzip.NewReader(srcFile)
|
|
|
|
if err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `gzip.NewReader failed`)
|
2020-03-15 19:32:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer reader.Close()
|
|
|
|
|
|
|
|
if _, err = io.Copy(dstFile, reader); err != nil {
|
2021-12-21 22:59:14 +08:00
|
|
|
err = gerror.Wrap(err, `io.Copy failed`)
|
2020-03-15 19:32:26 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|