-
Notifications
You must be signed in to change notification settings - Fork 0
/
localfile.go
83 lines (75 loc) · 1.68 KB
/
localfile.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
74
75
76
77
78
79
80
81
82
83
package zlfile
import (
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
)
// 获取路径文件及其子目录文件列表
func GetDirAndSubDirFileList(pathname string) ([]string, error) {
var fileList []string
err := filepath.Walk(pathname,
func(path string, f os.FileInfo, err error) error {
if f == nil {
return err
}
if f.IsDir() {
fileList = append(fileList, path)
return nil
}
return nil
})
return fileList, err
}
// 获取路径下文件及文件夹名称列表
func GetDirFileList(path string) ([]string, error) {
var fileList []string
rd, err := ioutil.ReadDir(path)
if err != nil {
return fileList, err
}
for _, fi := range rd {
fileList = append(fileList, fi.Name())
}
return fileList, nil
}
// 判断本地路径是否为目录
func IsDir(path string) (bool, error) {
f, err := os.Stat(path)
if err != nil {
return false, err
}
return f.IsDir(), nil
}
// 将path文件权限为filePermission
func Chmod(path string, modeFile *ChmodFile) error {
if modeFile.Mode == 0 {
if len(modeFile.FilePermission) > 0 {
if modeFile.FilePermission[0] != '0' {
modeFile.FilePermission = "0" + modeFile.FilePermission
}
permNum, err := strconv.ParseInt(modeFile.FilePermission, 8, 10)
if err != nil {
return err
}
modeFile.Mode = os.FileMode(permNum)
}
}
return os.Chmod(path, modeFile.Mode)
}
// 判断文件或目录是否存在
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
// 获取路径的目录
func GetParentDirectory(path string) string {
return path[0:strings.LastIndex(path, "/")]
}