Просмотр исходного кода

修复解压文件的中有多层压缩文件到问题,目前最多支持4层(实现的傻一点,改动少)

Signed-off-by: 716 <[email protected]>
716 3 лет назад
Родитель
Сommit
7fefa76867

+ 41 - 8
internal/pkg/archive_helper/unarchiveFile.go

@@ -25,20 +25,53 @@ func UnArchiveFileEx(fileFullPath, desRootPath string) error {
 	if err != nil {
 		return err
 	}
-	err = filepath.Walk(desRootPath, func(path string, info fs.FileInfo, err error) error {
+	// --------------------------------------------------
+	doUnzipFun := func() error {
+		// 判断一次
+		needUnzipFileFPaths := make([]string, 0)
+		err = filepath.Walk(desRootPath, func(path string, info fs.FileInfo, err error) error {
 
-		// 然后对于解压的内容再次进行判断,如果有压缩包,那么就继续解压
-		if nowExt != ".zip" && nowExt != ".tar" && nowExt != ".rar" && nowExt != ".7z" {
-
-		} else {
-			err = UnArchiveFile(path, desRootPath)
+			if info.IsDir() == true {
+				return nil
+			}
+			nowExt := filepath.Ext(path)
+			// 然后对于解压的内容再次进行判断
+			if nowExt != ".zip" && nowExt != ".tar" && nowExt != ".rar" && nowExt != ".7z" {
+				return nil
+			} else {
+				needUnzipFileFPaths = append(needUnzipFileFPaths, path)
+			}
+			return nil
+		})
+		if err != nil {
+			return err
+		}
+		// 如果有压缩包,那么就继续解压,然后删除压缩包
+		for _, needUnzipFileFPath := range needUnzipFileFPaths {
+			err = UnArchiveFile(needUnzipFileFPath, desRootPath)
+			if err != nil {
+				return err
+			}
+			err = os.Remove(needUnzipFileFPath)
 			if err != nil {
 				return err
 			}
-			os.Remove(path)
 		}
+
 		return nil
-	})
+	}
+	// 第二次解压
+	err = doUnzipFun()
+	if err != nil {
+		return err
+	}
+	// 第三次解压
+	err = doUnzipFun()
+	if err != nil {
+		return err
+	}
+	// 第四次解压
+	err = doUnzipFun()
 	if err != nil {
 		return err
 	}

+ 28 - 0
internal/pkg/archive_helper/unarchiveFile_test.go

@@ -35,3 +35,31 @@ func testUnArchive(t *testing.T, testRootDir string, missionName string) {
 
 const subASS = "oslo.2021.1080p.web.h264-naisu.繁体&英文.ass"
 const subSRT = "oslo.2021.1080p.web.h264-naisu.繁体&英文.srt"
+
+func TestUnArchiveFileEx(t *testing.T) {
+
+	testRootDir := unit_test_helper.GetTestDataResourceRootPath([]string{"zips"}, 4, true)
+
+	type args struct {
+		fileFullPath string
+		desRootPath  string
+	}
+	tests := []struct {
+		name    string
+		args    args
+		wantErr bool
+	}{
+		{
+			name: "[zimuku]_0_Inside No 9_S7E1.zip", args: args{
+				fileFullPath: filepath.Join(testRootDir, "[zimuku]_0_Inside No 9_S7E1.zip"),
+				desRootPath:  filepath.Join(testRootDir, "[zimuku]_0_Inside No 9_S7E1"),
+			}, wantErr: false},
+	}
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			if err := UnArchiveFileEx(tt.args.fileFullPath, tt.args.desRootPath); (err != nil) != tt.wantErr {
+				t.Errorf("UnArchiveFileEx() error = %v, wantErr %v", err, tt.wantErr)
+			}
+		})
+	}
+}

+ 1 - 1
internal/pkg/sub_helper/sub_helper.go

@@ -70,7 +70,7 @@ func OrganizeDlSubFiles(log *logrus.Logger, tmpFolderName string, subInfos []sup
 			if err != nil {
 				return nil, err
 			}
-			err = archive_helper.UnArchiveFile(nowFileSaveFullPath, unzipTmpFolder)
+			err = archive_helper.UnArchiveFileEx(nowFileSaveFullPath, unzipTmpFolder)
 			// 解压完成后,遍历受支持的字幕列表,加入缓存列表
 			if err != nil {
 				log.Errorln("archiver.UnArchive", subInfos[i].FromWhere, subInfos[i].Name, subInfos[i].TopN, err)