add file rotation feature for package glog; improve gpool/gfpool; fix issue in gfile.MTimeMillisecond

This commit is contained in:
John
2020-03-15 19:32:26 +08:00
parent e9fba5a166
commit 74be9fac18
17 changed files with 593 additions and 196 deletions

View File

@ -9,10 +9,11 @@ package gcompress
import (
"bytes"
"compress/gzip"
"github.com/gogf/gf/os/gfile"
"io"
)
// Gzip compresses <data> with gzip algorithm.
// Gzip compresses <data> using gzip algorithm.
// The optional parameter <level> specifies the compression level from
// 1 to 9 which means from none to the best compression.
//
@ -38,6 +39,38 @@ func Gzip(data []byte, level ...int) ([]byte, error) {
return buf.Bytes(), nil
}
// GzipFile compresses the file <src> to <dst> using gzip algorithm.
func GzipFile(src, dst string, level ...int) error {
var writer *gzip.Writer
var err 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()
if len(level) > 0 {
writer, err = gzip.NewWriterLevel(dstFile, level[0])
if err != nil {
return err
}
} else {
writer = gzip.NewWriter(dstFile)
}
defer writer.Close()
_, err = io.Copy(writer, srcFile)
if err != nil {
return err
}
return nil
}
// UnGzip decompresses <data> with gzip algorithm.
func UnGzip(data []byte) ([]byte, error) {
var buf bytes.Buffer
@ -53,3 +86,28 @@ func UnGzip(data []byte) ([]byte, error) {
}
return buf.Bytes(), nil
}
// UnGzip decompresses file <src> to <dst> using gzip algorithm.
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 {
return err
}
defer reader.Close()
if _, err = io.Copy(dstFile, reader); err != nil {
return err
}
return nil
}

View File

@ -7,6 +7,9 @@
package gcompress_test
import (
"github.com/gogf/gf/debug/gdebug"
"github.com/gogf/gf/os/gfile"
"github.com/gogf/gf/os/gtime"
"testing"
"github.com/gogf/gf/encoding/gcompress"
@ -26,14 +29,37 @@ func Test_Gzip_UnGzip(t *testing.T) {
0x24, 0xa8, 0xd1, 0x0d, 0x00,
0x00, 0x00,
}
gtest.Case(t, func() {
arr := []byte(src)
data, _ := gcompress.Gzip(arr)
gtest.Assert(data, gzip)
arr := []byte(src)
data, _ := gcompress.Gzip(arr)
gtest.Assert(data, gzip)
data, _ = gcompress.UnGzip(gzip)
gtest.Assert(data, arr)
data, _ = gcompress.UnGzip(gzip)
gtest.Assert(data, arr)
data, _ = gcompress.UnGzip(gzip[1:])
gtest.Assert(data, nil)
data, _ = gcompress.UnGzip(gzip[1:])
gtest.Assert(data, nil)
})
}
func Test_Gzip_UnGzip_File(t *testing.T) {
srcPath := gfile.Join(gdebug.CallerDirectory(), "testdata", "gzip", "file.txt")
dstPath1 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "gzip.zip")
dstPath2 := gfile.Join(gfile.TempDir(), gtime.TimestampNanoStr(), "file.txt")
// Compress.
gtest.Case(t, func() {
err := gcompress.GzipFile(srcPath, dstPath1, 9)
gtest.Assert(err, nil)
defer gfile.Remove(dstPath1)
gtest.Assert(gfile.Exists(dstPath1), true)
// Decompress.
err = gcompress.UnGzipFile(dstPath1, dstPath2)
gtest.Assert(err, nil)
defer gfile.Remove(dstPath2)
gtest.Assert(gfile.Exists(dstPath2), true)
gtest.Assert(gfile.GetContents(srcPath), gfile.GetContents(dstPath2))
})
}

View File

@ -0,0 +1 @@
This is a test file for gzip compression.