unzip gbk package

This commit is contained in:
Van 2015-03-28 12:15:44 +08:00
parent fb22dbd273
commit 78fcc0d19a
2 changed files with 23 additions and 2 deletions

View File

@ -16,10 +16,16 @@ package util
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"unicode/utf8"
"golang.org/x/text/encoding/simplifiedchinese"
"golang.org/x/text/transform"
)
type myzip struct{}
@ -144,7 +150,22 @@ func (z *ZipFile) AddDirectory(path, dirName string) error {
func cloneZipItem(f *zip.File, dest string) error {
// create full directory path
path := filepath.Join(dest, f.Name)
fileName := f.Name
fmt.Println(fileName)
if !utf8.ValidString(fileName) {
fmt.Println("!utf8")
data, err := ioutil.ReadAll(transform.NewReader(bytes.NewReader([]byte(fileName)), simplifiedchinese.GB18030.NewDecoder()))
if nil == err {
fileName = string(data)
} else {
fmt.Println(err)
}
}
fmt.Println(fileName)
path := filepath.Join(dest, fileName)
err := os.MkdirAll(filepath.Dir(path), os.ModeDir|os.ModePerm)
if nil != err {

View File

@ -58,7 +58,7 @@ func TestMain(m *testing.M) {
// clean test data
os.RemoveAll(packageName + ".zip")
os.RemoveAll(packageName)
//os.RemoveAll(packageName)
os.Exit(retCode)
}