dqpfkzu360216 2014-11-15 10:43
浏览 245
已采纳

为什么在golang和Linux中使用archive / zip会使文件名混乱?

I'm using golang's standard package archive/zip to wrap several files into a zipfile.
Here is my code for test:

package main

import (    
    "archive/zip"
    "log"
    "os"
)

func main() {
    archive, _ := os.Create("/tmp/测试file.zip")
    w := zip.NewWriter(archive)

    // Add some files to the archive.
    var files = []struct {
        Name, Body string
    }{
        {"测试.txt", "test content: 测试"},
        {"test.txt", "test content: test"},
    }

    for _, file := range files {
        f, err := w.Create(file.Name)
        if err != nil {
            log.Fatal(err)
        }

        _, err = f.Write([]byte(file.Body))
        if err != nil {
            log.Fatal(err)
        }
    }

    err := w.Close()
    if err != nil {
        log.Fatal(err)
    }
}

results:
I get a zip file named 测试file.zip under /tmp as expected.
After unzip it, I get two files: test.txt, ц╡ЛшпХ.txt, and that is a mess.
The contents in both of the two files are normal as expected.

Why does this happen and how to fix this?

  • 写回答

2条回答 默认 最新

  • doudui2229 2014-11-15 13:57
    关注

    This might be an issue with unzip not handling UTF8 names properly. Explicitly using the Chinese locale worked for me:

    $ LANG=zh_ZH unzip 测试file.zip
    Archive:  测试file.zip
      inflating: 测试.txt              
      inflating: test.txt
    $ cat *.txt
    test content: testtest content: 测试
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?