Skip to content

Commit

Permalink
add GzipPathWriter for package gcompress (#2116)
Browse files Browse the repository at this point in the history
* add GzipPathWriter for package gcompress

* UT case updates for package package gclient
  • Loading branch information
gqcn authored Sep 8, 2022
1 parent c866b50 commit faf09c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
35 changes: 21 additions & 14 deletions encoding/gcompress/gcompress_gzip.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,35 +47,42 @@ func Gzip(data []byte, level ...int) ([]byte, error) {
}

// GzipFile compresses the file `src` to `dst` using gzip algorithm.
func GzipFile(src, dst string, level ...int) error {
var (
writer *gzip.Writer
err error
)
srcFile, err := gfile.Open(src)
func GzipFile(src, dst string, level ...int) (err error) {
dstFile, err := gfile.Create(dst)
if err != nil {
return err
}
defer srcFile.Close()
dstFile, err := gfile.Create(dst)
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)
if err != nil {
return err
}
defer dstFile.Close()
defer srcFile.Close()

if len(level) > 0 {
writer, err = gzip.NewWriterLevel(dstFile, level[0])
gzipWriter, err = gzip.NewWriterLevel(writer, level[0])
if err != nil {
err = gerror.Wrap(err, `gzip.NewWriterLevel failed`)
return err
}
} else {
writer = gzip.NewWriter(dstFile)
gzipWriter = gzip.NewWriter(writer)
}
defer writer.Close()
defer gzipWriter.Close()

_, err = io.Copy(writer, srcFile)
if err != nil {
if _, err = io.Copy(gzipWriter, srcFile); err != nil {
err = gerror.Wrap(err, `io.Copy failed`)
return err
}
Expand Down
32 changes: 18 additions & 14 deletions encoding/gcompress/gcompress_z_unit_gzip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,20 @@ import (
)

func Test_Gzip_UnGzip(t *testing.T) {
src := "Hello World!!"
var (
src = "Hello World!!"
gzip = []byte{
0x1f, 0x8b, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff,
0xf2, 0x48, 0xcd, 0xc9, 0xc9,
0x57, 0x08, 0xcf, 0x2f, 0xca,
0x49, 0x51, 0x54, 0x04, 0x04,
0x00, 0x00, 0xff, 0xff, 0x9d,
0x24, 0xa8, 0xd1, 0x0d, 0x00,
0x00, 0x00,
}
)

gzip := []byte{
0x1f, 0x8b, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xff,
0xf2, 0x48, 0xcd, 0xc9, 0xc9,
0x57, 0x08, 0xcf, 0x2f, 0xca,
0x49, 0x51, 0x54, 0x04, 0x04,
0x00, 0x00, 0xff, 0xff, 0x9d,
0x24, 0xa8, 0xd1, 0x0d, 0x00,
0x00, 0x00,
}
gtest.C(t, func(t *gtest.T) {
arr := []byte(src)
data, _ := gcompress.Gzip(arr)
Expand All @@ -42,9 +44,11 @@ func Test_Gzip_UnGzip(t *testing.T) {
}

func Test_Gzip_UnGzip_File(t *testing.T) {
srcPath := gtest.DataPath("gzip", "file.txt")
dstPath1 := gfile.Temp(gtime.TimestampNanoStr(), "gzip.zip")
dstPath2 := gfile.Temp(gtime.TimestampNanoStr(), "file.txt")
var (
srcPath = gtest.DataPath("gzip", "file.txt")
dstPath1 = gfile.Temp(gtime.TimestampNanoStr(), "gzip.zip")
dstPath2 = gfile.Temp(gtime.TimestampNanoStr(), "file.txt")
)

// Compress.
gtest.C(t, func(t *gtest.T) {
Expand Down
3 changes: 2 additions & 1 deletion net/gclient/gclient_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,8 @@ func Test_WebSocketClient(t *testing.T) {
s.SetPort(p)
s.SetDumpRouterMap(false)
s.Start()
defer s.Shutdown()
// No closing in case of DATA RACE due to keep alive connection of WebSocket.
//defer s.Shutdown()

time.Sleep(100 * time.Millisecond)
gtest.C(t, func(t *gtest.T) {
Expand Down

0 comments on commit faf09c5

Please sign in to comment.