|
@@ -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
|
|
|
|
+}
|