animeic-cd hai 1 ano
pai
achega
dd8f3efe52
Modificáronse 2 ficheiros con 45 adicións e 40 borrados
  1. 4 0
      main.go
  2. 41 40
      utils/zip.go

+ 4 - 0
main.go

@@ -4,6 +4,7 @@ import (
 	"app-uploader/huawei"
 	"app-uploader/utils"
 	"fmt"
+	"os"
 	"runtime"
 	"strconv"
 	"strings"
@@ -105,5 +106,8 @@ func main() {
 		return
 	}
 	fmt.Println("上传成功")
+	// !压缩后的文件在windows上用软件打开有问题,但在linux上解压或者unzip正常
+	// utils.Unzip(zipPath,filepath.Base(zipFile),nil)
+	os.Remove(zipPath)
 
 }

+ 41 - 40
utils/zip.go

@@ -5,7 +5,9 @@ import (
 	"fmt"
 	"io"
 	"io/fs"
+	"log"
 	"os"
+	"path"
 	"path/filepath"
 	"strings"
 )
@@ -65,7 +67,6 @@ func walkFunc(rootPath string, zipWriter *zip.Writer) filepath.WalkFunc {
 			return err
 		}
 		if info.IsDir() {
-			// header.Name += string(os.PathSeparator)
 			header.Name += string(os.PathSeparator)
 		}
 
@@ -88,57 +89,57 @@ func walkFunc(rootPath string, zipWriter *zip.Writer) filepath.WalkFunc {
 	}
 }
 
-func ZipFile(srcFile, destFile string) error {
-	// 创建目标文件,即zip文件
-	zipFile, err := os.Create(destFile)
+func Unzip(zipPath, dstDir string, cb func(per1000 int)) error {
+	// open zip file
+	reader, err := zip.OpenReader(zipPath)
 	if err != nil {
 		return err
 	}
-	defer zipFile.Close()
-	// 创建writer,准备向目标文件中写入数据
-	writer := zip.NewWriter(zipFile)
-	defer writer.Close()
-
-	// 遍历文件(如果srcFile是个目录的话)
-	err = filepath.Walk(srcFile, func(path string, info os.FileInfo, err error) error {
-		// 遍历发生错误,退出
-		if err != nil {
-			return err
+	defer reader.Close()
+
+	total := len(reader.File)
+	for index, file := range reader.File {
+		per1000 := int(float32(index+1) / float32(total) * 1000)
+		// log.Println("unzip=>", file.Name, index, "/", len(reader.File))
+		log.Println("unzip=>", file.Name)
+		if cb != nil {
+			cb(per1000)
 		}
-
-		// 创建zip文件信息头
-		header, err := zip.FileInfoHeader(info)
-		if err != nil {
+		if err := unzipFile(file, dstDir); err != nil {
 			return err
 		}
-		header.Name = path
-		if info.IsDir() {
-			header.Name += "\\"
-		} else {
-			header.Method = zip.Deflate
-		}
+	}
+	return nil
+}
 
-		// 将信息头写入到writer中
-		writer, err := writer.CreateHeader(header)
-		if err != nil {
+func unzipFile(file *zip.File, dstDir string) error {
+	// create the directory of file
+	filePath := path.Join(dstDir, file.Name)
+	if file.FileInfo().IsDir() {
+		if err := os.MkdirAll(filePath, os.ModePerm); err != nil {
 			return err
 		}
+		return nil
+	}
+	if err := os.MkdirAll(filepath.Dir(filePath), os.ModePerm); err != nil {
+		return err
+	}
 
-		// 如果是目录,直接写个头就可以了,filepath.Walk会继续遍历目录
-		if info.IsDir() {
-			return nil
-		}
-
-		// 如果是文件,则将文件内容写入到writer中
-		file, err := os.Open(path)
-		if err != nil {
-			return err
-		}
-		defer file.Close()
-		_, err = io.Copy(writer, file)
+	// open the file
+	rc, err := file.Open()
+	if err != nil {
+		return err
+	}
+	defer rc.Close()
 
+	// create the file
+	w, err := os.Create(filePath)
+	if err != nil {
 		return err
-	})
+	}
+	defer w.Close()
 
+	// save the decompressed file content
+	_, err = io.Copy(w, rc)
 	return err
 }