github.com/gogf/gf@v1.16.9/encoding/gcompress/gcompress_z_unit_gzip_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gcompress_test
     8  
     9  import (
    10  	"github.com/gogf/gf/debug/gdebug"
    11  	"github.com/gogf/gf/os/gfile"
    12  	"github.com/gogf/gf/os/gtime"
    13  	"testing"
    14  
    15  	"github.com/gogf/gf/encoding/gcompress"
    16  	"github.com/gogf/gf/test/gtest"
    17  )
    18  
    19  func Test_Gzip_UnGzip(t *testing.T) {
    20  	src := "Hello World!!"
    21  
    22  	gzip := []byte{
    23  		0x1f, 0x8b, 0x08, 0x00, 0x00,
    24  		0x00, 0x00, 0x00, 0x00, 0xff,
    25  		0xf2, 0x48, 0xcd, 0xc9, 0xc9,
    26  		0x57, 0x08, 0xcf, 0x2f, 0xca,
    27  		0x49, 0x51, 0x54, 0x04, 0x04,
    28  		0x00, 0x00, 0xff, 0xff, 0x9d,
    29  		0x24, 0xa8, 0xd1, 0x0d, 0x00,
    30  		0x00, 0x00,
    31  	}
    32  	gtest.C(t, func(t *gtest.T) {
    33  		arr := []byte(src)
    34  		data, _ := gcompress.Gzip(arr)
    35  		t.Assert(data, gzip)
    36  
    37  		data, _ = gcompress.UnGzip(gzip)
    38  		t.Assert(data, arr)
    39  
    40  		data, _ = gcompress.UnGzip(gzip[1:])
    41  		t.Assert(data, nil)
    42  	})
    43  }
    44  
    45  func Test_Gzip_UnGzip_File(t *testing.T) {
    46  	srcPath := gdebug.TestDataPath("gzip", "file.txt")
    47  	dstPath1 := gfile.TempDir(gtime.TimestampNanoStr(), "gzip.zip")
    48  	dstPath2 := gfile.TempDir(gtime.TimestampNanoStr(), "file.txt")
    49  
    50  	// Compress.
    51  	gtest.C(t, func(t *gtest.T) {
    52  		err := gcompress.GzipFile(srcPath, dstPath1, 9)
    53  		t.Assert(err, nil)
    54  		defer gfile.Remove(dstPath1)
    55  		t.Assert(gfile.Exists(dstPath1), true)
    56  
    57  		// Decompress.
    58  		err = gcompress.UnGzipFile(dstPath1, dstPath2)
    59  		t.Assert(err, nil)
    60  		defer gfile.Remove(dstPath2)
    61  		t.Assert(gfile.Exists(dstPath2), true)
    62  
    63  		t.Assert(gfile.GetContents(srcPath), gfile.GetContents(dstPath2))
    64  	})
    65  }