-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathu_file.go
73 lines (69 loc) · 1.5 KB
/
u_file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package kerbalwzygo
import (
"bufio"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"unicode/utf8"
)
// 判断文件或者文件夹路径是否存在
func PathOk(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
return false, err
}
// 按行读取文件内容, 并判断是否为UTF8编码
func ValidFileUTF8(filepath string, checkLines int) (bool, error) {
if checkLines <= 0 {
checkLines = 10
}
isUTF8 := true
fp, err := os.Open(filepath)
if nil != err {
return false, err
}
defer fp.Close()
buf := bufio.NewReader(fp)
for i := 0; i < checkLines; {
line, err := buf.ReadBytes('\n')
if len(line) > 0 {
isUTF8 = utf8.Valid(line)
i++
}
if io.EOF == err {
break
}
if nil != err {
return false, err
}
}
return isUTF8, nil
}
// 获取目录下是所有文件的绝对路径(不含文件夹, 并且可以通过suffix过滤, 当suffix为空字符串或者"*"时表示配匹所有文件尾缀)
func ListDirFiles(dirPath, suffix string) ([]string, error) {
res := make([]string, 0)
_, err := PathOk(dirPath)
if nil != err {
return res, err
}
filterFunc := func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() {
res = append(res, path)
}
return err
}
if suffix != "*" && suffix != "" {
filterFunc = func(path string, d fs.DirEntry, err error) error {
if !d.IsDir() && strings.HasSuffix(path, suffix) {
res = append(res, path)
}
return err
}
}
err = filepath.WalkDir(dirPath, filterFunc)
return res, err
}