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

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分