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

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

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

问题

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

package main

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

func main() {
	archive, _ := os.Create("/tmp/测试file.zip")
	w := zip.NewWriter(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)
	}
}

结果:
我得到了一个名为测试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:

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?

答案1

得分: 2

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

$ LANG=zh_ZH unzip 测试file.zip
Archive:  测试file.zip
  inflating: 测试.txt              
  inflating: test.txt
$ cat *.txt
test content: testtest content: 测试
英文:

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: 测试

答案2

得分: 0

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

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

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

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:

确定