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

huangapple go评论119阅读模式
英文:

Why file's name get messy using archive/zip in golang, linux?

问题

我正在使用golang的标准包archive/zip将多个文件打包成一个zip文件。以下是我的测试代码:

  1. package main
  2. import (
  3. "archive/zip"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. archive, _ := os.Create("/tmp/测试file.zip")
  9. w := zip.NewWriter(archive)
  10. // 向压缩文件中添加一些文件。
  11. var files = []struct {
  12. Name, Body string
  13. }{
  14. {"测试.txt", "test content: 测试"},
  15. {"test.txt", "test content: test"},
  16. }
  17. for _, file := range files {
  18. f, err := w.Create(file.Name)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. _, err = f.Write([]byte(file.Body))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. }
  27. err := w.Close()
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. }

结果:
我得到了一个名为测试file.zip的zip文件,位于/tmp目录下,与预期一致。
解压后,我得到了两个文件:test.txtц╡ЛшпХ.txt,这是一团糟。
两个文件的内容都是正常的,与预期一致。

为什么会出现这种情况,如何解决?

英文:

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

  1. package main
  2. import (
  3. "archive/zip"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. archive, _ := os.Create("/tmp/测试file.zip")
  9. w := zip.NewWriter(archive)
  10. // Add some files to the archive.
  11. var files = []struct {
  12. Name, Body string
  13. }{
  14. {"测试.txt", "test content: 测试"},
  15. {"test.txt", "test content: test"},
  16. }
  17. for _, file := range files {
  18. f, err := w.Create(file.Name)
  19. if err != nil {
  20. log.Fatal(err)
  21. }
  22. _, err = f.Write([]byte(file.Body))
  23. if err != nil {
  24. log.Fatal(err)
  25. }
  26. }
  27. err := w.Close()
  28. if err != nil {
  29. log.Fatal(err)
  30. }
  31. }

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?

答案1

得分: 2

这可能是unzip在处理UTF8名称时出现问题的一个问题。对我来说,明确使用中文区域设置起作用:

  1. $ LANG=zh_ZH unzip 测试file.zip
  2. Archive: 测试file.zip
  3. inflating: 测试.txt
  4. inflating: test.txt
  5. $ cat *.txt
  6. test content: testtest content: 测试
英文:

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

  1. $ LANG=zh_ZH unzip 测试file.zip
  2. Archive: 测试file.zip
  3. inflating: 测试.txt
  4. inflating: test.txt
  5. $ cat *.txt
  6. test content: testtest content: 测试

答案2

得分: 0

import {
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
}

filename, _, err = transform.String(simplifiedchinese.GBK.NewEncoder(), "测试.txt")

英文:
  1. import {
  2. "golang.org/x/text/encoding/simplifiedchinese"
  3. "golang.org/x/text/transform"
  4. }
  5. filename, _, err = transform.String(simplifiedchinese.GBK.NewEncoder(), "测试.txt")

huangapple
  • 本文由 发表于 2014年11月15日 18:43:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/26944962.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定