-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile.go
More file actions
125 lines (109 loc) · 2.19 KB
/
file.go
File metadata and controls
125 lines (109 loc) · 2.19 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package fiputil
import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
func IsHidden(name string) bool {
//todo: windows
if len(name) != 0 && name[0] == '.' {
return true
} else {
return false
}
}
func CopyDir(src string, dest string, filter func(string) bool) (err error) {
if err := os.MkdirAll(dest, 0755); err != nil {
return err
}
children, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, child := range children {
name := child.Name()
if filter != nil && !filter(name) {
continue
}
childSrc := filepath.Join(src, name)
childDest := filepath.Join(dest, name)
if child.IsDir() {
err = CopyDir(childSrc, childDest, filter)
} else {
err = CopyFile(childSrc, childDest)
}
if err != nil {
return
}
}
return
}
func CopyFile(src string, dest string) (err error) {
//log.Println("copy file,src:",src,"dest:",dest)
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755) //os.Create(dest)
if err != nil {
return
}
defer out.Close()
_, err = io.Copy(out, in)
return
}
func ReadFile(path string) (content []byte, len uint32, err error) {
file, err := os.Open(path)
if err != nil {
return
}
defer file.Close()
bwCon := new(bytes.Buffer)
l, err := io.Copy(bwCon, file)
content = bwCon.Bytes()
len = uint32(l)
return
}
func WriteFile(path string, content []byte) (err error) {
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0755)
defer file.Close()
file.Write(content)
return
}
func GetExtension(path string) string {
idx := strings.LastIndex(path, ".")
if idx == -1 {
return ""
} else {
return path[idx+1:]
}
}
func RemoveExtension(path string) string {
idx := strings.LastIndex(path, ".")
if idx == -1 {
return path
} else {
return path[:idx]
}
}
func CombinePath(base string, extra ...string) {
/*for i:=1;i<len(extra);i++ {
if os.IsPathSeparator(base[len(base)-1]) {
if os.IsPathSeparator(extra[i][0]) {
base += extra[1:]
} else {
base += extra
}
} else {
if os.IsPathSeparator(extra[i][0]) {
base += extra
} else {
base += os.PathSeparator + extra
}
}
}*/
}