-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathutil.go
91 lines (80 loc) · 1.7 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
package main
import (
"archive/zip"
"encoding/json"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
"github.com/brookshi/Hitchhiker-Node/hlog"
)
func readConfig() (config, error) {
var c config
file, err := ioutil.ReadFile("./config.json")
if err != nil {
hlog.Error.Println("read config failed: ", err)
return c, err
}
err = json.Unmarshal(file, &c)
return c, err
}
func saveFile(data []byte) error {
if _, err := os.Stat(dataFilePath); !os.IsNotExist(err) {
os.Remove(dataFilePath)
}
f, err := os.OpenFile(dataFilePath, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
n, err := f.Write(data)
if err == nil && n < len(data) {
err = io.ErrShortWrite
}
if err1 := f.Close(); err == nil {
err = err1
}
return err
}
func unzip(src, dest string) ([]string, error) {
var filenames []string
r, err := zip.OpenReader(src)
if err != nil {
return filenames, err
}
defer r.Close()
for _, f := range r.File {
rc, err := f.Open()
if err != nil {
return filenames, err
}
defer rc.Close()
fpath := filepath.Join(dest, f.Name)
filenames = append(filenames, fpath)
if f.FileInfo().IsDir() {
os.MkdirAll(fpath, os.ModePerm)
} else {
var fdir string
if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 {
fdir = fpath[:lastIndex]
}
err = os.MkdirAll(fdir, os.ModePerm)
if err != nil {
log.Fatal(err)
return filenames, err
}
f, err := os.OpenFile(
fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return filenames, err
}
defer f.Close()
_, err = io.Copy(f, rc)
if err != nil {
return filenames, err
}
}
}
return filenames, nil
}