我使用golang的标准软件包归档/ zip将几个文件包装成一个zip文件。
这是我的testing代码:
package main import ( "archive/zip" "log" "os" ) func main() { archive, _ := os.Create("/tmp/testingfile.zip") w := zip.NewWriter(archive) // Add some files to the archive. var files = []struct { Name, Body string }{ {"testing.txt", "test content: testing"}, {"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) } }
结果:
如预期的那样,我在/tmp
下面得到一个名为testingfile.zip
的压缩文件。
unzip
后,我得到两个文件: test.txt
, ц╡ЛшпХ.txt
,这是一个烂摊子。
两个文件中的内容都是正常的。
为什么会发生这种情况,如何解决这个问题?
这可能是unzip
不正确处理UTF8名称的问题 。 明确地使用中文语言环境对我有用:
$ LANG=zh_ZH unzip 测试file.zip Archive: 测试file.zip inflating: 测试.txt inflating: test.txt $ cat *.txt test content: testtest content: 测试
import { "golang.org/x/text/encoding/simplifiedchinese" "golang.org/x/text/transform" } filename, _, err = transform.String(simplifiedchinese.GBK.NewEncoder(), "测试.txt")