Home >Backend Development >Golang >How to solve the Chinese garbled code in golang zip

How to solve the Chinese garbled code in golang zip

爱喝马黛茶的安东尼
爱喝马黛茶的安东尼Original
2019-12-10 10:36:534108browse

How to solve the Chinese garbled code in golang zip

Question

The zip package that comes with the go language can decompress zip files. However, if you use winrar to compress it into zip. If you use go to decompress it again, you will find that the file name is garbled. But when you use a number of domestic compression software to compress and then decompress it, it will not be garbled.

Cause

When winrar is compressed, the local encoding method is used for compression by default. In China, the local encoding method is generally GBK. And we know that Go language strings are in UTF-8 format, so garbled characters may appear.

How to solve the Chinese garbled code in golang zip

Solution

Determine the file name encoding method, if it is GBK, convert GBK=》utf-8

From the above picture, we know that if the 11 bit of the flags field is 1, it is UTF-8 encoding, and 0 is the local encoding.

Code

The following two packages are used in the code:

"golang.org/x/text/encoding/simplifiedchinese"

"golang.org/x/text/transform"

func Unzip(zipFile string, destDir string) error {
    zipReader, err := zip.OpenReader(zipFile)
    if err != nil {
        return err
    }
    defer zipReader.Close()
    var decodeName string
    for _, f := range zipReader.File {
        if f.Flags == 0{
            //如果标致位是0  则是默认的本地编码   默认为gbk
            i:= bytes.NewReader([]byte(f.Name))
            decoder := transform.NewReader(i, simplifiedchinese.GB18030.NewDecoder())
            content,_:= ioutil.ReadAll(decoder)
            decodeName = string(content)
        }else{
            //如果标志为是 1 << 11也就是 2048  则是utf-8编码
            decodeName = f.Name
        }
        fpath := filepath.Join(destDir, decodeName)
        if f.FileInfo().IsDir() {
            os.MkdirAll(fpath, os.ModePerm)
        } else {
            if err = os.MkdirAll(filepath.Dir(fpath), os.ModePerm); err != nil {
                return err
            }
            inFile, err := f.Open()
            if err != nil {
                return err
            }
            defer inFile.Close()
            outFile, err := os.OpenFile(fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
            if err != nil {
                return err
            }
            defer outFile.Close()
            _, err = io.Copy(outFile, inFile)
            if err != nil {
                return err
            }
        }
    }
    return nil
}

PHP Chinese website has a large number of free Golang introductory tutorials, everyone is welcome to learn!

The above is the detailed content of How to solve the Chinese garbled code in golang zip. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn