github.com/gogf/gf/v2@v2.7.4/encoding/gcompress/gcompress_z_unit_zip_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  	"bytes"
    11  	"testing"
    12  
    13  	"github.com/gogf/gf/v2/encoding/gcompress"
    14  	"github.com/gogf/gf/v2/os/gfile"
    15  	"github.com/gogf/gf/v2/os/gtime"
    16  	"github.com/gogf/gf/v2/test/gtest"
    17  )
    18  
    19  func Test_ZipPath(t *testing.T) {
    20  	// file
    21  	gtest.C(t, func(t *gtest.T) {
    22  		srcPath := gtest.DataPath("zip", "path1", "1.txt")
    23  		dstPath := gtest.DataPath("zip", "zip.zip")
    24  
    25  		t.Assert(gfile.Exists(dstPath), false)
    26  		t.Assert(gcompress.ZipPath(srcPath, dstPath), nil)
    27  		t.Assert(gfile.Exists(dstPath), true)
    28  		defer gfile.Remove(dstPath)
    29  
    30  		// unzip to temporary dir.
    31  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
    32  		t.Assert(gfile.Mkdir(tempDirPath), nil)
    33  		t.Assert(gcompress.UnZipFile(dstPath, tempDirPath), nil)
    34  		defer gfile.Remove(tempDirPath)
    35  
    36  		t.Assert(
    37  			gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
    38  			gfile.GetContents(srcPath),
    39  		)
    40  	})
    41  	// multiple files
    42  	gtest.C(t, func(t *gtest.T) {
    43  		var (
    44  			srcPath1 = gtest.DataPath("zip", "path1", "1.txt")
    45  			srcPath2 = gtest.DataPath("zip", "path2", "2.txt")
    46  			dstPath  = gfile.Temp(gtime.TimestampNanoStr(), "zip.zip")
    47  		)
    48  		if p := gfile.Dir(dstPath); !gfile.Exists(p) {
    49  			t.Assert(gfile.Mkdir(p), nil)
    50  		}
    51  
    52  		t.Assert(gfile.Exists(dstPath), false)
    53  		err := gcompress.ZipPath(srcPath1+","+srcPath2, dstPath)
    54  		t.AssertNil(err)
    55  		t.Assert(gfile.Exists(dstPath), true)
    56  		defer gfile.Remove(dstPath)
    57  
    58  		// unzip to another temporary dir.
    59  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
    60  		t.Assert(gfile.Mkdir(tempDirPath), nil)
    61  		err = gcompress.UnZipFile(dstPath, tempDirPath)
    62  		t.AssertNil(err)
    63  		defer gfile.Remove(tempDirPath)
    64  
    65  		t.Assert(
    66  			gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
    67  			gfile.GetContents(srcPath1),
    68  		)
    69  		t.Assert(
    70  			gfile.GetContents(gfile.Join(tempDirPath, "2.txt")),
    71  			gfile.GetContents(srcPath2),
    72  		)
    73  	})
    74  	// one dir and one file.
    75  	gtest.C(t, func(t *gtest.T) {
    76  		var (
    77  			srcPath1 = gtest.DataPath("zip", "path1")
    78  			srcPath2 = gtest.DataPath("zip", "path2", "2.txt")
    79  			dstPath  = gfile.Temp(gtime.TimestampNanoStr(), "zip.zip")
    80  		)
    81  		if p := gfile.Dir(dstPath); !gfile.Exists(p) {
    82  			t.Assert(gfile.Mkdir(p), nil)
    83  		}
    84  
    85  		t.Assert(gfile.Exists(dstPath), false)
    86  		err := gcompress.ZipPath(srcPath1+","+srcPath2, dstPath)
    87  		t.AssertNil(err)
    88  		t.Assert(gfile.Exists(dstPath), true)
    89  		defer gfile.Remove(dstPath)
    90  
    91  		// unzip to another temporary dir.
    92  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
    93  		t.Assert(gfile.Mkdir(tempDirPath), nil)
    94  		err = gcompress.UnZipFile(dstPath, tempDirPath)
    95  		t.AssertNil(err)
    96  		defer gfile.Remove(tempDirPath)
    97  
    98  		t.Assert(
    99  			gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
   100  			gfile.GetContents(gfile.Join(srcPath1, "1.txt")),
   101  		)
   102  		t.Assert(
   103  			gfile.GetContents(gfile.Join(tempDirPath, "2.txt")),
   104  			gfile.GetContents(srcPath2),
   105  		)
   106  	})
   107  	// directory.
   108  	gtest.C(t, func(t *gtest.T) {
   109  		srcPath := gtest.DataPath("zip")
   110  		dstPath := gtest.DataPath("zip", "zip.zip")
   111  
   112  		pwd := gfile.Pwd()
   113  		err := gfile.Chdir(srcPath)
   114  		defer gfile.Chdir(pwd)
   115  		t.AssertNil(err)
   116  
   117  		t.Assert(gfile.Exists(dstPath), false)
   118  		err = gcompress.ZipPath(srcPath, dstPath)
   119  		t.AssertNil(err)
   120  		t.Assert(gfile.Exists(dstPath), true)
   121  		defer gfile.Remove(dstPath)
   122  
   123  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
   124  		err = gfile.Mkdir(tempDirPath)
   125  		t.AssertNil(err)
   126  
   127  		err = gcompress.UnZipFile(dstPath, tempDirPath)
   128  		t.AssertNil(err)
   129  		defer gfile.Remove(tempDirPath)
   130  
   131  		t.Assert(
   132  			gfile.GetContents(gfile.Join(tempDirPath, "zip", "path1", "1.txt")),
   133  			gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
   134  		)
   135  		t.Assert(
   136  			gfile.GetContents(gfile.Join(tempDirPath, "zip", "path2", "2.txt")),
   137  			gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
   138  		)
   139  	})
   140  	// multiple directory paths joined using char ','.
   141  	gtest.C(t, func(t *gtest.T) {
   142  		var (
   143  			srcPath  = gtest.DataPath("zip")
   144  			srcPath1 = gtest.DataPath("zip", "path1")
   145  			srcPath2 = gtest.DataPath("zip", "path2")
   146  			dstPath  = gtest.DataPath("zip", "zip.zip")
   147  		)
   148  		pwd := gfile.Pwd()
   149  		err := gfile.Chdir(srcPath)
   150  		defer gfile.Chdir(pwd)
   151  		t.AssertNil(err)
   152  
   153  		t.Assert(gfile.Exists(dstPath), false)
   154  		err = gcompress.ZipPath(srcPath1+", "+srcPath2, dstPath)
   155  		t.AssertNil(err)
   156  		t.Assert(gfile.Exists(dstPath), true)
   157  		defer gfile.Remove(dstPath)
   158  
   159  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
   160  		err = gfile.Mkdir(tempDirPath)
   161  		t.AssertNil(err)
   162  
   163  		zipContent := gfile.GetBytes(dstPath)
   164  		t.AssertGT(len(zipContent), 0)
   165  		err = gcompress.UnZipContent(zipContent, tempDirPath)
   166  		t.AssertNil(err)
   167  		defer gfile.Remove(tempDirPath)
   168  
   169  		t.Assert(
   170  			gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
   171  			gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
   172  		)
   173  		t.Assert(
   174  			gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
   175  			gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
   176  		)
   177  	})
   178  }
   179  
   180  func Test_ZipPathWriter(t *testing.T) {
   181  	gtest.C(t, func(t *gtest.T) {
   182  		var (
   183  			srcPath  = gtest.DataPath("zip")
   184  			srcPath1 = gtest.DataPath("zip", "path1")
   185  			srcPath2 = gtest.DataPath("zip", "path2")
   186  		)
   187  		pwd := gfile.Pwd()
   188  		err := gfile.Chdir(srcPath)
   189  		defer gfile.Chdir(pwd)
   190  		t.AssertNil(err)
   191  
   192  		writer := bytes.NewBuffer(nil)
   193  		t.Assert(writer.Len(), 0)
   194  		err = gcompress.ZipPathWriter(srcPath1+", "+srcPath2, writer)
   195  		t.AssertNil(err)
   196  		t.AssertGT(writer.Len(), 0)
   197  
   198  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
   199  		err = gfile.Mkdir(tempDirPath)
   200  		t.AssertNil(err)
   201  
   202  		zipContent := writer.Bytes()
   203  		t.AssertGT(len(zipContent), 0)
   204  		err = gcompress.UnZipContent(zipContent, tempDirPath)
   205  		t.AssertNil(err)
   206  		defer gfile.Remove(tempDirPath)
   207  
   208  		t.Assert(
   209  			gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
   210  			gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
   211  		)
   212  		t.Assert(
   213  			gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
   214  			gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
   215  		)
   216  	})
   217  }
   218  
   219  func Test_ZipPathContent(t *testing.T) {
   220  	gtest.C(t, func(t *gtest.T) {
   221  		var (
   222  			srcPath  = gtest.DataPath("zip")
   223  			srcPath1 = gtest.DataPath("zip", "path1")
   224  			srcPath2 = gtest.DataPath("zip", "path2")
   225  		)
   226  		pwd := gfile.Pwd()
   227  		err := gfile.Chdir(srcPath)
   228  		defer gfile.Chdir(pwd)
   229  		t.AssertNil(err)
   230  
   231  		tempDirPath := gfile.Temp(gtime.TimestampNanoStr())
   232  		err = gfile.Mkdir(tempDirPath)
   233  		t.AssertNil(err)
   234  
   235  		zipContent, err := gcompress.ZipPathContent(srcPath1 + ", " + srcPath2)
   236  		t.AssertGT(len(zipContent), 0)
   237  		err = gcompress.UnZipContent(zipContent, tempDirPath)
   238  		t.AssertNil(err)
   239  		defer gfile.Remove(tempDirPath)
   240  
   241  		t.Assert(
   242  			gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
   243  			gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
   244  		)
   245  		t.Assert(
   246  			gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
   247  			gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
   248  		)
   249  	})
   250  }