fix: fix Chinese filename garbled in zip file list (GBK to UTF-8)
This commit is contained in:
@@ -12,6 +12,7 @@ import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"verification-platform-backend/internal/database"
|
||||
"verification-platform-backend/internal/middleware"
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"verification-platform-backend/pkg/response"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"golang.org/x/text/encoding/simplifiedchinese"
|
||||
)
|
||||
|
||||
func SetupVersionRoutes(r *gin.RouterGroup) {
|
||||
@@ -952,11 +954,20 @@ func parseZipFile(zipPath string) ([]VersionFileInfo, error) {
|
||||
rc.Close()
|
||||
|
||||
fileHash := hex.EncodeToString(hash.Sum(nil))
|
||||
fileType := getFileType(f.Name)
|
||||
|
||||
// 修复中文文件名乱码:如果文件名不是有效 UTF-8,尝试 GBK 解码
|
||||
fileName := f.Name
|
||||
if !utf8.ValidString(fileName) {
|
||||
if decoded, err := simplifiedchinese.GBK.NewDecoder().String(fileName); err == nil {
|
||||
fileName = decoded
|
||||
}
|
||||
}
|
||||
|
||||
fileType := getFileType(fileName)
|
||||
|
||||
files = append(files, VersionFileInfo{
|
||||
FilePath: f.Name,
|
||||
FileName: filepath.Base(f.Name),
|
||||
FilePath: fileName,
|
||||
FileName: filepath.Base(fileName),
|
||||
FileSize: int64(f.UncompressedSize64),
|
||||
FileHash: fileHash,
|
||||
FileType: fileType,
|
||||
|
||||
Reference in New Issue
Block a user