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条)

报告相同问题?

悬赏问题

  • ¥50 汇编语言除法溢出问题
  • ¥65 C++实现删除N个数据列表共有的元素
  • ¥15 Visual Studio问题
  • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗