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