diff --git a/encoding/gcompress/gcompress_gzip.go b/encoding/gcompress/gcompress_gzip.go index ac05e83a6..f748cbb98 100644 --- a/encoding/gcompress/gcompress_gzip.go +++ b/encoding/gcompress/gcompress_gzip.go @@ -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) - if err != nil { - return err - } - defer srcFile.Close() +func GzipFile(src, dst string, level ...int) (err error) { dstFile, err := gfile.Create(dst) if err != nil { return err } 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 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 } diff --git a/encoding/gcompress/gcompress_z_unit_gzip_test.go b/encoding/gcompress/gcompress_z_unit_gzip_test.go index 3239e2976..81983bb51 100644 --- a/encoding/gcompress/gcompress_z_unit_gzip_test.go +++ b/encoding/gcompress/gcompress_z_unit_gzip_test.go @@ -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) @@ -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) { diff --git a/net/gclient/gclient_z_unit_test.go b/net/gclient/gclient_z_unit_test.go index 599ce889c..8a485aba5 100644 --- a/net/gclient/gclient_z_unit_test.go +++ b/net/gclient/gclient_z_unit_test.go @@ -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) {