-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_functions.go
180 lines (154 loc) · 3.33 KB
/
basic_functions.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
package basic_functions
import (
"archive/zip"
"crypto/md5"
"encoding/gob"
"encoding/hex"
"fmt"
"io"
"io/fs"
"log"
"net/http"
"os"
"path/filepath"
"strings"
)
func Read_file(file string) ([]byte, error) {
if _, err := os.Stat(file); err == nil {
if err != nil {
return []byte(""), err
}
}
return os.ReadFile(file)
}
func Write_file(file string, data string) error {
// save the file to disk
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.ModePerm)
if err != nil {
return err
}
defer f.Close()
_, err = f.WriteString(data)
if err != nil {
print(err.Error())
return err
}
return nil
}
func Delete_file(file string) error {
err := os.Remove(file)
if err != nil {
return err
}
return nil
}
func Download_file(filepath string, url string) (err error) {
// Create the file
out, err := os.Create(filepath)
if err != nil {
return err
}
defer out.Close()
// Get the data
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
// Check server response
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("bad status: %s", resp.Status)
}
// Writer the body to file
_, err = io.Copy(out, resp.Body)
if err != nil {
return err
}
return nil
}
func Current_directory() string {
ex, err := os.Executable()
if err != nil {
panic(err)
}
// the executable directory
directory := filepath.Dir(ex)
return directory
}
func Read_file_in_zip(zip_path string, file_path string) (string, error) {
r, err := zip.OpenReader(zip_path)
if err != nil {
return "", err
}
defer r.Close()
file_data := ""
for _, f := range r.File {
if !strings.Contains(f.Name, file_path) {
continue
}
buffer := new(strings.Builder)
rc, err := f.Open()
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(buffer, rc)
if err != nil {
log.Fatal(err)
}
file_data = buffer.String()
break
}
return file_data, err
}
func Serialize(filePath string, object interface{}) error {
file, err := os.Create(filePath)
if err == nil {
dataEncoder := gob.NewEncoder(file)
dataEncoder.Encode(object)
file.Close()
}
return err
}
func Deserialize(filePath string, object interface{}) error {
file, err := os.Open(filePath)
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(object)
}
file.Close()
return err
}
func File_or_directory_exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
// File or directory does not exist
return false
} else {
// Some other error. The file may or may not exist
return false
}
}
return true
}
func List_files_in_dir(dir string) ([]fs.DirEntry, error) {
return os.ReadDir(dir)
}
func List_files_by_extension(dir string, extension string) []string {
var extfiles []string
var dirfiles, err = List_files_in_dir(dir)
if err != nil {
log.Fatalln("List_files_by_extension - Error: " + err.Error())
}
for _, file := range dirfiles {
if strings.HasSuffix(file.Name(), extension) {
extfiles = append(extfiles, file.Name())
}
}
return extfiles
}
func Calc_md5(data string) string {
hash := md5.Sum([]byte(data))
md5_string := hex.EncodeToString(hash[:])
return md5_string
}