2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
2020-03-06 15:38:32 +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.
|
|
|
|
|
|
|
|
package gcompress_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/debug/gdebug"
|
|
|
|
"github.com/gogf/gf/v2/encoding/gcompress"
|
|
|
|
"github.com/gogf/gf/v2/os/gfile"
|
|
|
|
"github.com/gogf/gf/v2/os/gtime"
|
2020-03-06 15:38:32 +08:00
|
|
|
"testing"
|
|
|
|
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
2020-03-06 15:38:32 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
func Test_ZipPath(t *testing.T) {
|
|
|
|
// file
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-21 19:41:05 +08:00
|
|
|
srcPath := gdebug.TestDataPath("zip", "path1", "1.txt")
|
|
|
|
dstPath := gdebug.TestDataPath("zip", "zip.zip")
|
2020-03-06 15:38:32 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Exists(dstPath), false)
|
2020-05-01 00:18:45 +08:00
|
|
|
t.Assert(gcompress.ZipPath(srcPath, dstPath), nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Exists(dstPath), true)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(dstPath)
|
|
|
|
|
2020-05-01 00:18:45 +08:00
|
|
|
// unzip to temporary dir.
|
2020-03-28 00:41:12 +08:00
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-05-01 00:18:45 +08:00
|
|
|
t.Assert(gfile.Mkdir(tempDirPath), nil)
|
|
|
|
t.Assert(gcompress.UnZipFile(dstPath, tempDirPath), nil)
|
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
|
|
|
t.Assert(
|
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
|
|
|
|
gfile.GetContents(srcPath),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// multiple files
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
|
|
|
srcPath1 = gdebug.TestDataPath("zip", "path1", "1.txt")
|
|
|
|
srcPath2 = gdebug.TestDataPath("zip", "path2", "2.txt")
|
|
|
|
dstPath = gfile.TempDir(gtime.TimestampNanoStr(), "zip.zip")
|
|
|
|
)
|
|
|
|
if p := gfile.Dir(dstPath); !gfile.Exists(p) {
|
|
|
|
t.Assert(gfile.Mkdir(p), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Assert(gfile.Exists(dstPath), false)
|
|
|
|
err := gcompress.ZipPath(srcPath1+","+srcPath2, dstPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-05-01 00:18:45 +08:00
|
|
|
t.Assert(gfile.Exists(dstPath), true)
|
|
|
|
defer gfile.Remove(dstPath)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
2020-05-01 00:18:45 +08:00
|
|
|
// unzip to another temporary dir.
|
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
|
|
|
t.Assert(gfile.Mkdir(tempDirPath), nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "1.txt")),
|
2020-05-01 00:18:45 +08:00
|
|
|
gfile.GetContents(srcPath1),
|
|
|
|
)
|
|
|
|
t.Assert(
|
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "2.txt")),
|
|
|
|
gfile.GetContents(srcPath2),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
// one dir and one file.
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
|
|
|
srcPath1 = gdebug.TestDataPath("zip", "path1")
|
|
|
|
srcPath2 = gdebug.TestDataPath("zip", "path2", "2.txt")
|
|
|
|
dstPath = gfile.TempDir(gtime.TimestampNanoStr(), "zip.zip")
|
|
|
|
)
|
|
|
|
if p := gfile.Dir(dstPath); !gfile.Exists(p) {
|
|
|
|
t.Assert(gfile.Mkdir(p), nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Assert(gfile.Exists(dstPath), false)
|
|
|
|
err := gcompress.ZipPath(srcPath1+","+srcPath2, dstPath)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gfile.Exists(dstPath), true)
|
|
|
|
defer gfile.Remove(dstPath)
|
|
|
|
|
|
|
|
// unzip to another temporary dir.
|
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
|
|
|
t.Assert(gfile.Mkdir(tempDirPath), nil)
|
|
|
|
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
|
|
|
t.Assert(err, nil)
|
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
|
|
|
t.Assert(
|
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath1, "1.txt")),
|
|
|
|
)
|
|
|
|
t.Assert(
|
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "2.txt")),
|
|
|
|
gfile.GetContents(srcPath2),
|
2020-03-06 15:38:32 +08:00
|
|
|
)
|
|
|
|
})
|
2020-05-01 00:18:45 +08:00
|
|
|
// directory.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-03-21 19:41:05 +08:00
|
|
|
srcPath := gdebug.TestDataPath("zip")
|
|
|
|
dstPath := gdebug.TestDataPath("zip", "zip.zip")
|
2020-03-06 15:38:32 +08:00
|
|
|
|
|
|
|
pwd := gfile.Pwd()
|
|
|
|
err := gfile.Chdir(srcPath)
|
|
|
|
defer gfile.Chdir(pwd)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Exists(dstPath), false)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.ZipPath(srcPath, dstPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gfile.Exists(dstPath), true)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(dstPath)
|
|
|
|
|
2020-03-28 00:41:12 +08:00
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gfile.Mkdir(tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
|
|
|
err = gcompress.UnZipFile(dstPath, tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path1", "1.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
|
|
|
)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "zip", "path2", "2.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
|
|
|
)
|
|
|
|
})
|
2020-05-01 00:18:45 +08:00
|
|
|
// multiple directory paths joined using char ','.
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-01 00:18:45 +08:00
|
|
|
var (
|
|
|
|
srcPath = gdebug.TestDataPath("zip")
|
|
|
|
srcPath1 = gdebug.TestDataPath("zip", "path1")
|
|
|
|
srcPath2 = gdebug.TestDataPath("zip", "path2")
|
|
|
|
dstPath = gdebug.TestDataPath("zip", "zip.zip")
|
|
|
|
)
|
2020-03-06 15:38:32 +08:00
|
|
|
pwd := gfile.Pwd()
|
|
|
|
err := gfile.Chdir(srcPath)
|
|
|
|
defer gfile.Chdir(pwd)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(gfile.Exists(dstPath), false)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.ZipPath(srcPath1+", "+srcPath2, dstPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.Assert(gfile.Exists(dstPath), true)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(dstPath)
|
|
|
|
|
2020-03-28 00:41:12 +08:00
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gfile.Mkdir(tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
|
|
|
zipContent := gfile.GetBytes(dstPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(zipContent), 0)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
|
|
|
)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_ZipPathWriter(t *testing.T) {
|
2020-03-19 22:56:12 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
2020-05-01 00:18:45 +08:00
|
|
|
var (
|
|
|
|
srcPath = gdebug.TestDataPath("zip")
|
|
|
|
srcPath1 = gdebug.TestDataPath("zip", "path1")
|
|
|
|
srcPath2 = gdebug.TestDataPath("zip", "path2")
|
|
|
|
)
|
2020-03-06 15:38:32 +08:00
|
|
|
pwd := gfile.Pwd()
|
|
|
|
err := gfile.Chdir(srcPath)
|
|
|
|
defer gfile.Chdir(pwd)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
|
|
|
writer := bytes.NewBuffer(nil)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(writer.Len(), 0)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.ZipPathWriter(srcPath1+", "+srcPath2, writer)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
|
|
|
t.AssertGT(writer.Len(), 0)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
2020-03-28 00:41:12 +08:00
|
|
|
tempDirPath := gfile.TempDir(gtime.TimestampNanoStr())
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gfile.Mkdir(tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
|
|
|
|
zipContent := writer.Bytes()
|
2020-03-19 22:56:12 +08:00
|
|
|
t.AssertGT(len(zipContent), 0)
|
2020-03-06 15:38:32 +08:00
|
|
|
err = gcompress.UnZipContent(zipContent, tempDirPath)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(err, nil)
|
2020-03-06 15:38:32 +08:00
|
|
|
defer gfile.Remove(tempDirPath)
|
|
|
|
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "path1", "1.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path1", "1.txt")),
|
|
|
|
)
|
2020-03-19 22:56:12 +08:00
|
|
|
t.Assert(
|
2020-03-06 15:38:32 +08:00
|
|
|
gfile.GetContents(gfile.Join(tempDirPath, "path2", "2.txt")),
|
|
|
|
gfile.GetContents(gfile.Join(srcPath, "path2", "2.txt")),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|