Fix: handle none-utf8 encoding in decompression

This commit is contained in:
HFO4
2020-02-07 14:01:53 +08:00
parent 16613a6113
commit 14982f5e70
3 changed files with 16 additions and 1 deletions

View File

@ -2,13 +2,17 @@ package filesystem
import (
"archive/zip"
"bytes"
"context"
"fmt"
model "github.com/HFO4/cloudreve/models"
"github.com/HFO4/cloudreve/pkg/filesystem/fsctx"
"github.com/HFO4/cloudreve/pkg/util"
"github.com/gin-gonic/gin"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
@ -256,7 +260,16 @@ func (fs *FileSystem) Decompress(ctx context.Context, src, dst string) error {
}
for _, f := range r.File {
rawPath := util.FormSlash(f.Name)
fileName := f.Name
// 处理非UTF-8编码
if f.NonUTF8 {
i := bytes.NewReader([]byte(fileName))
decoder := transform.NewReader(i, simplifiedchinese.GB18030.NewDecoder())
content, _ := ioutil.ReadAll(decoder)
fileName = string(content)
}
rawPath := util.FormSlash(fileName)
savePath := path.Join(dst, rawPath)
// 路径是否合法
if !strings.HasPrefix(savePath, util.FillSlash(path.Clean(dst))) {