github.com/isyscore/isc-gobase@v1.5.3-0.20231218061332-cbc7451899e9/compress/test/compress_test.go (about)

     1  package test
     2  
     3  import (
     4  	"github.com/isyscore/isc-gobase/compress"
     5  	"github.com/isyscore/isc-gobase/file"
     6  	"github.com/isyscore/isc-gobase/isc"
     7  	"testing"
     8  )
     9  
    10  func TestGZip(t *testing.T) {
    11  	str := []byte("Hello World!")
    12  	if b, err := compress.GzipCompress(str); err == nil {
    13  		t.Logf("%v", b)
    14  	} else {
    15  		t.Logf("GzipCompress err: %v", err)
    16  	}
    17  }
    18  
    19  func TestUnGzip(t *testing.T) {
    20  	b := []byte{
    21  		0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
    22  		0x00, 0xff, 0xf2, 0x48, 0xcd, 0xc9, 0xc9, 0x57,
    23  		0x08, 0xcf, 0x2f, 0xca, 0x49, 0x51, 0x04, 0x00,
    24  		0x00, 0x00, 0xff, 0xff}
    25  	if s, err := compress.GzipDecompress(b); err == nil {
    26  		t.Logf("%s", string(s))
    27  	} else {
    28  		t.Logf("GzipDecompress err: %v", err)
    29  	}
    30  }
    31  
    32  func TestGZipFile(t *testing.T) {
    33  	file.AppendFile("./zip/test3.txt", "cc")
    34  	_ = compress.GzipCompressFile("./zip/test3.txt", "./zip/test3.txt.gz")
    35  	file.DeleteDirs("./zip/")
    36  }
    37  
    38  func TestUnGZipFile(t *testing.T) {
    39  	file.AppendFile("./zip/test3.txt", "cc")
    40  	_ = compress.GzipCompressFile("./zip/test3.txt", "./zip/test3.txt.gz")
    41  	_ = compress.GzipDeCompressFile("./zip/test3.txt.gz", "./zip/test3_1.txt")
    42  	file.DeleteDirs("./zip/")
    43  }
    44  
    45  func TestZip(t *testing.T) {
    46  	f1 := "./zip/test1.txt"
    47  	f2 := "./zip/test2.txt"
    48  	f3 := "./zip/test3.txt"
    49  	var files = []string{f1, f2, f3}
    50  	dest := "./zip/test.zip"
    51  	srcSize := isc.FormatSize(file.SizeList(files))
    52  	if err := compress.Zip(dest, files); err == nil {
    53  		dstSize := isc.FormatSize(file.Size("./zip/test.zip"))
    54  		// Compress success, size: 714.55KB -> 5.88KB
    55  		t.Logf("Compress success, zip size: %s -> %s", srcSize, dstSize)
    56  	} else {
    57  		t.Logf("Compress err: %v", err)
    58  	}
    59  	file.DeleteFile("./zip/test.zip")
    60  }
    61  
    62  func TestUnzip(t *testing.T) {
    63  	// zip
    64  	f1 := "./zip/test1.txt"
    65  	f2 := "./zip/test2.txt"
    66  	f3 := "./zip/test3.txt"
    67  	var files = []string{f1, f2, f3}
    68  
    69  	zipFile := "./zip/test.zip"
    70  	_ = compress.Zip(zipFile, files)
    71  
    72  	// unzip
    73  	dest := "./zip/uncomp"
    74  	err := compress.Unzip(zipFile, dest)
    75  	if err == nil {
    76  		srcSize := isc.FormatSize(file.Size("./zip/test.zip"))
    77  		dstSize := isc.FormatSize(file.SizeList(files))
    78  
    79  		// Decompress success, unzip size: 5.88KB -> 714.55KB
    80  		t.Logf("Decompress success, unzip size: %s -> %s", srcSize, dstSize)
    81  	} else {
    82  		t.Logf("Decompress err: %v", err)
    83  	}
    84  	file.DeleteFile("./zip/test.zip")
    85  	file.DeleteDirs("./zip/uncomp")
    86  }