Skip to content

Commit

Permalink
fix(fileutil): unzip Chinese garbled code during decompression (#279)
Browse files Browse the repository at this point in the history
解压时出现中文乱码
  • Loading branch information
hunantangke authored Dec 19, 2024
1 parent d4b425e commit 1f64e02
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion fileutil/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1f64e02

Please sign in to comment.