From a86f663ebe01b10cfb01d90aa4ce4e85cfbfade3 Mon Sep 17 00:00:00 2001 From: tangke Date: Thu, 19 Dec 2024 17:44:00 +0800 Subject: [PATCH] fix(fileutil): unzip Chinese garbled code during decompression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 解压时出现中文乱码 --- fileutil/file.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/fileutil/file.go b/fileutil/file.go index b131ee72..46465f02 100644 --- a/fileutil/file.go +++ b/fileutil/file.go @@ -25,6 +25,8 @@ import ( "sync" "github.com/duke-git/lancet/v2/validator" + "golang.org/x/text/encoding/simplifiedchinese" + "golang.org/x/text/transform" ) // FileReader is a reader supporting offset seeking and reading one @@ -422,8 +424,15 @@ func UnZip(zipFile string, destPath string) error { defer zipReader.Close() for _, f := range zipReader.File { + decodeName := f.Name + if f.Flags == 0 { + i := bytes.NewReader([]byte(f.Name)) + decoder := transform.NewReader(i, simplifiedchinese.GB18030.NewDecoder()) + content, _ := io.ReadAll(decoder) + decodeName = string(content) + } // issue#62: fix ZipSlip bug - path, err := safeFilepathJoin(destPath, f.Name) + path, err := safeFilepathJoin(destPath, decodeName) if err != nil { return err }