123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- package main
- import (
- "fmt"
- "github.com/axgle/mahonia"
- )
- func decodeFilename(filename string) (string, error) {
- decoded, err := decodeUTF8(filename)
- if err == nil {
- return decoded, nil
- }
- // 如果解码失败,尝试其他编码
- // 在此处添加其他编码的处理逻辑,例如GBK、Big5等。
- // 如果您知道文件名的正确编码,请在此处进行相应的转换。
- return filename, nil
- }
- func decodeUTF8(s string) (string, error) {
- decoded, err := decodeString(s, "UTF-8")
- if err != nil {
- return "", err
- }
- return decoded, nil
- }
- func decodeString(s, enc string) (string, error) {
- encodings := []string{enc, "GBK", "Big5"} // 根据需要添加其他编码
- for _, encoding := range encodings {
- decoded, err := decode(s, encoding)
- if err == nil {
- return decoded, nil
- }
- }
- return s, fmt.Errorf("无法解码字符串:%s", s)
- }
- func decode(s, enc string) (string, error) {
- dec := mahonia.NewDecoder(enc)
- if dec == nil {
- return "", fmt.Errorf("不支持的编码:%s", enc)
- }
- return dec.ConvertString(s), nil
- }
|