-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
executable file
·248 lines (213 loc) · 4.63 KB
/
util.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package main
import (
"os"
"path/filepath"
"unicode/utf8"
"archive/zip"
"log"
"io"
"strings"
)
var txtExt = map[string]bool{
".js": true,
".json": true,
".html": true,
".md": true,
".rst": true,
".php": true,
".conf": true,
".go": true,
".css": true,
".py": true,
".log": true,
".pl": true,
".cofee": true,
".dart": true,
".sql": true,
}
// Take from here
// https://code.google.com/p/go/source/browse/godoc/util/util.go?repo=tools
func getTrimDir(s string) string {
return strings.TrimRight(s, "/") + "/"
}
func IsTextFile(filename string) bool {
if istxt, found := txtExt[filepath.Ext(filename)]; found {
return istxt
}
f, err := os.Open(filename)
if err != nil {
return false
}
defer f.Close()
var buf [1024]byte
n, err := f.Read(buf[0:])
if err != nil {
return false
}
return IsText(buf[0:n])
}
// IsText reports whether a significant prefix of s looks like correct UTF-8;
// that is, if it is likely that s is human-readable text.
func IsText(s []byte) bool {
const max = 1024 // at least utf8.UTFMax
if len(s) > max {
s = s[0:max]
}
for i, c := range string(s) {
if i+utf8.UTFMax > len(s) {
// last char may be incomplete - ignore
break
}
if c == 0xFFFD || c < ' ' && c != '\n' && c != '\t' && c != '\f' {
// decoding error or control character - not a text file
return false
}
}
return true
}
func DeCompress(tarFile, dest string) error {
if strings.HasSuffix(tarFile,".zip"){
return zipDeCompress(tarFile,dest)
}
return nil
}
func zipDeCompressCurrentPath(zipFile, dest string) error {
or ,err := zip.OpenReader(zipFile)
defer or.Close()
if err!=nil {return err}
log.Print(" 压缩文件",zipFile," 解压到",dest )
for _,item := range or.File {
log.Print(dest+item.Name)
names := strings.Split(item.Name, "/")
log.Print(len(names))
log.Print(names)
if !strings.EqualFold(strings.Join(names[:1],""),
strings.Join(strings.Split(filepath.Base(zipFile),".")[:1],"")) {
if item.FileInfo().IsDir() {
os.Mkdir(dest+item.Name, 0777)
continue
}
rc, _ := item.Open()
dst, _ := createFile(dest + item.Name)
_, err := io.Copy(dst, rc)
if err != nil {
log.Print(err)
}
} else {
filename := strings.Join(names[1:], "/")
if item.FileInfo().IsDir() {
os.Mkdir(dest+filename, 0777)
continue
}
rc, _ := item.Open()
dst, _ := createFile(dest + filename)
_, err := io.Copy(dst, rc)
if err != nil {
log.Print(err)
}
}
}
return nil
}
func zipDeCompress(zipFile, dest string) error {
or ,err := zip.OpenReader(zipFile)
defer or.Close()
if err!=nil {return err}
log.Print(" 压缩文件",zipFile," 解压到",dest )
for _,item := range or.File {
log.Print(dest+item.Name)
if item.FileInfo().IsDir() {
os.Mkdir(dest+item.Name, 0777)
continue
}
rc, _ := item.Open()
dst,_ := createFile(dest+item.Name)
_,err :=io.Copy(dst,rc)
if err!=nil { log.Print(err) }
}
return nil
}
func DeCompress1(zipFile, dest string) error {
reader, err := zip.OpenReader(zipFile)
if err != nil {
return err
}
defer reader.Close()
for _, file := range reader.File {
rc, err := file.Open()
if err != nil {
return err
}
defer rc.Close()
filename := dest + file.Name
err = os.MkdirAll(getDir(filename), 0755)
if err != nil {
return err
}
w, err := os.Create(filename)
if err != nil {
return err
}
defer w.Close()
_, err = io.Copy(w, rc)
if err != nil {
return err
}
w.Close()
rc.Close()
}
return nil
}
func getDir(path string) string {
return subString(path, 0, strings.LastIndex(path, "/"))
}
func subString(str string, start, end int) string {
rs := []rune(str)
length := len(rs)
if start < 0 || start > length {
panic("start is wrong")
}
if end < start || end > length {
panic("end is wrong")
}
return string(rs[start:end])
}
func createFile(name string) (*os.File, error) {
err := os.MkdirAll(string([]rune(name)[0:strings.LastIndex(name, "/")]), 0755)
if err != nil {
return nil, err
}
return os.Create(name)
}
// 判断路径文件/文件夹是否存在
func Exists(path string) bool {
_, err := os.Stat(path) //os.Stat获取文件信息
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
func isDirExists(path string) bool {
fi, err := os.Stat(path)
if err != nil {
return os.IsExist(err)
} else {
return fi.IsDir()
}
panic("not reached")
}
// 判断所给路径是否为文件夹
func IsDir(path string) bool {
s, err := os.Stat(path)
if err != nil {
return false
}
return s.IsDir()
}
// 判断所给路径是否为文件
func IsFile(path string) bool {
return !IsDir(path)
}