Files
gf/encoding/gcompress/gcompress_z_unit_gzip_test.go

69 lines
1.6 KiB
Go
Raw Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-04-09 19:12:48 +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.
2019-04-09 19:12:48 +08:00
package gcompress_test
import (
2019-06-21 22:23:07 +08:00
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/encoding/gcompress"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/os/gfile"
"github.com/gogf/gf/v2/os/gtime"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/test/gtest"
2019-04-09 19:12:48 +08:00
)
2020-03-06 15:38:32 +08:00
func Test_Gzip_UnGzip(t *testing.T) {
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,
}
)
2019-04-18 12:34:01 +08:00
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
arr := []byte(src)
data, _ := gcompress.Gzip(arr)
2020-03-19 22:56:12 +08:00
t.Assert(data, gzip)
2019-04-18 12:34:01 +08:00
data, _ = gcompress.UnGzip(gzip)
2020-03-19 22:56:12 +08:00
t.Assert(data, arr)
data, _ = gcompress.UnGzip(gzip[1:])
2020-03-19 22:56:12 +08:00
t.Assert(data, nil)
})
}
func Test_Gzip_UnGzip_File(t *testing.T) {
var (
srcPath = gtest.DataPath("gzip", "file.txt")
dstPath1 = gfile.Temp(gtime.TimestampNanoStr(), "gzip.zip")
dstPath2 = gfile.Temp(gtime.TimestampNanoStr(), "file.txt")
)
// Compress.
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
err := gcompress.GzipFile(srcPath, dstPath1, 9)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
defer gfile.Remove(dstPath1)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Exists(dstPath1), true)
// Decompress.
err = gcompress.UnGzipFile(dstPath1, dstPath2)
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
defer gfile.Remove(dstPath2)
2020-03-19 22:56:12 +08:00
t.Assert(gfile.Exists(dstPath2), true)
2019-04-18 12:34:01 +08:00
2020-03-19 22:56:12 +08:00
t.Assert(gfile.GetContents(srcPath), gfile.GetContents(dstPath2))
})
2019-04-09 19:12:48 +08:00
}