Ver Fonte

添加fileutils及其测试

yjp há 3 meses atrás
pai
commit
a0fd86bd2f

+ 20 - 0
.gitignore

@@ -0,0 +1,20 @@
+# Binaries for programs and plugins
+*.exe
+*.exe~
+*.dll
+*.so
+*.dylib
+
+# Test binary, build with `go test -c`
+*.test
+
+# Output of the go coverage tool, specifically when used with LiteIDE
+*.out
+
+*.zip
+.idea/
+.DS_Store
+**/bin
+
+/vendor/
+**/logs

+ 177 - 0
fileutils/fileutils.go

@@ -0,0 +1,177 @@
+package fileutils
+
+import (
+	"archive/zip"
+	"bytes"
+	"io"
+	"log"
+	"os"
+	"path/filepath"
+	"strings"
+)
+
+func PathExists(path string) bool {
+	_, err := os.Stat(path)
+	if err == nil {
+		return true
+	}
+
+	if os.IsNotExist(err) {
+		return false
+	}
+
+	return false
+}
+
+func GetDirFiles(dir string) ([]string, error) {
+	dirList, err := os.ReadDir(dir)
+	if err != nil {
+		return nil, err
+	}
+
+	filesRet := make([]string, 0)
+
+	for _, file := range dirList {
+		if file.IsDir() {
+			files, err := GetDirFiles(filepath.Join(dir, file.Name()))
+			if err != nil {
+				return nil, err
+			}
+
+			filesRet = append(filesRet, files...)
+		} else {
+			filesRet = append(filesRet, filepath.Join(dir, file.Name()))
+		}
+	}
+
+	return filesRet, nil
+}
+
+func ZipDir(dirPath string, savePath string) error {
+	archive, err := os.Create(savePath)
+	if err != nil {
+		return err
+	}
+
+	defer func(archive *os.File) {
+		err := archive.Close()
+		if err != nil {
+			log.Println(err)
+			return
+		}
+	}(archive)
+
+	zipWriter := zip.NewWriter(archive)
+
+	defer func(zipWriter *zip.Writer) {
+		err := zipWriter.Close()
+		if err != nil {
+			log.Println(err)
+			return
+		}
+	}(zipWriter)
+
+	err = filepath.Walk(dirPath,
+		func(path string, f os.FileInfo, err error) error {
+			if err != nil {
+				return err
+			}
+
+			if f.IsDir() {
+				return nil
+			}
+
+			zipPath := strings.TrimPrefix(path, dirPath)
+			err = writeZipFile(path, zipPath, zipWriter)
+			if err != nil {
+				return err
+			}
+
+			return nil
+		})
+	if err != nil {
+		return err
+	}
+
+	return nil
+}
+
+func UnzipFile(srcFilePath string, destDir string) error {
+	fileBytes, err := os.ReadFile(srcFilePath)
+	if err != nil {
+		return err
+	}
+
+	return UnzipBytes(fileBytes, destDir)
+}
+
+func UnzipBytes(zipFileBytes []byte, destDir string) error {
+	zipReader, err := zip.NewReader(bytes.NewReader(zipFileBytes), int64(len(zipFileBytes)))
+	if err != nil {
+		return err
+	}
+
+	for _, f := range zipReader.File {
+		fPath := filepath.Join(destDir, f.Name)
+		if f.FileInfo().IsDir() {
+			if err = os.MkdirAll(fPath, os.ModePerm); err != nil {
+				return err
+			}
+		} else {
+			if err = os.MkdirAll(filepath.Dir(fPath), os.ModePerm); err != nil {
+				return err
+			}
+
+			inFile, err := f.Open()
+			if err != nil {
+				return err
+			}
+
+			outFile, err := os.OpenFile(fPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
+			if err != nil {
+				return err
+			}
+
+			if _, err = io.Copy(outFile, inFile); err != nil {
+				return err
+			}
+
+			if err = inFile.Close(); err != nil {
+				return err
+			}
+
+			if err = outFile.Close(); err != nil {
+				return err
+			}
+		}
+	}
+
+	return nil
+}
+
+func writeZipFile(filePath string, zipPath string, zipWriter *zip.Writer) error {
+	file, err := os.Open(filePath)
+	if err != nil {
+		return err
+	}
+
+	defer func() {
+		err := file.Close()
+		if err != nil {
+			log.Println(err)
+			return
+		}
+	}()
+
+	writer, err := zipWriter.Create(zipPath)
+	if err != nil {
+		return err
+	}
+
+	_, err = io.Copy(writer, file)
+	if err != nil {
+		return err
+	}
+
+	return nil
+}

+ 88 - 0
fileutils/fileutils_test.go

@@ -0,0 +1,88 @@
+package fileutils
+
+import (
+	"os"
+	"path/filepath"
+	"testing"
+)
+
+const (
+	testDirRelativePath = "./test_dir"
+)
+
+func TestPathExists(t *testing.T) {
+	exist := PathExists("../fileutils")
+	if !exist {
+		t.Fatal("应该存在的目录不存在")
+	}
+
+	exist = PathExists("foo")
+	if exist {
+		t.Fatal("不应该存在的目录存在")
+	}
+}
+
+func TestGetDirFiles(t *testing.T) {
+	checkTestDir(t, testDirRelativePath)
+}
+
+func TestZip(t *testing.T) {
+	zipFileRelativePath := "test.zip"
+	destUnzipFileDir := "./unzip_file_test_dir"
+	destUnzipBytesDir := "./unzip_bytes_test_dir"
+
+	err := ZipDir(testDirRelativePath, zipFileRelativePath)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	defer func() {
+		err := os.RemoveAll(zipFileRelativePath)
+		if err != nil {
+			t.Fatal(err)
+		}
+	}()
+
+	err = UnzipFile(zipFileRelativePath, destUnzipFileDir)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	defer func() {
+		err := os.RemoveAll(destUnzipFileDir)
+		if err != nil {
+			t.Fatal(err)
+		}
+	}()
+
+	checkTestDir(t, filepath.Join(destUnzipFileDir, "test_dir"))
+
+	fileBytes, err := os.ReadFile(zipFileRelativePath)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	err = UnzipBytes(fileBytes, destUnzipBytesDir)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	checkTestDir(t, filepath.Join(destUnzipBytesDir, "test_dir"))
+}
+
+func checkTestDir(t *testing.T, dirPath string) {
+	filePaths, err := GetDirFiles(dirPath)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	if len(filePaths) != 2 {
+		t.Fatal("文件数量不正确")
+	}
+
+	for _, filePath := range filePaths {
+		if filePath != filepath.Join(dirPath, "test.txt") && filePath != filepath.Join(dirPath, "test_sub_dir/test_sub.txt") {
+			t.Fatal("出现不存在的文件")
+		}
+	}
+}

+ 0 - 0
fileutils/test_dir/test.txt


+ 0 - 0
fileutils/test_dir/test_sub_dir/test_sub.txt


+ 0 - 0
fileutils/unzip_bytes_test_dir/test_dir/test.txt


+ 0 - 0
fileutils/unzip_bytes_test_dir/test_dir/test_sub_dir/test_sub.txt


+ 1 - 0
go.mod

@@ -6,6 +6,7 @@ require (
 	github.com/fatih/structs v1.1.0
 	github.com/mitchellh/mapstructure v1.5.0
 	github.com/redis/go-redis/v9 v9.4.0
+	github.com/satori/go.uuid v1.2.0
 	gopkg.in/yaml.v3 v3.0.1
 )
 

+ 2 - 0
go.sum

@@ -12,6 +12,8 @@ github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyua
 github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
 github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk=
 github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
+github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
+github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

+ 32 - 0
strutils/strutils.go

@@ -0,0 +1,32 @@
+package strutils
+
+import (
+	uuid "github.com/satori/go.uuid"
+	"strings"
+)
+
+func AllBlank(str ...string) bool {
+	for _, s := range str {
+		if !IsStringEmpty(s) {
+			return false
+		}
+	}
+
+	return true
+}
+
+func IsStringEmpty(s string) bool {
+	return strings.Trim(s, " ") == ""
+}
+
+func IsStringNotEmpty(s string) bool {
+	return strings.Trim(s, " ") != ""
+}
+
+func GetUUID() string {
+	return uuid.NewV4().String()
+}
+
+func SimpleUUID() string {
+	return strings.ReplaceAll(GetUUID(), "-", "")
+}