/
backend.go
94 lines (86 loc) · 1.74 KB
/
backend.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
package backend
import (
"archive/zip"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
)
var Inputf, Outputf string
var fileCount = 0
var fileWritten = 0
type File struct {
Name, Body string
}
func getfiles(status func(msg string), progressPercent func(p float64)) []*File {
var files []*File
progressPercent(-1.00)
fmt.Println(Outputf)
walkfunc := func(path string, info os.FileInfo, err error) error {
if err != nil {
log.Fatal(err)
return err
}
if !info.IsDir() {
files = append(files, &File{
Name: strings.Replace(path, Inputf, "", 1),
Body: path,
})
status(info.Name())
fileCount++
//log.Println(path) //
}
return nil
}
status("********Reading files from the directory**************")
err := filepath.Walk(Inputf, walkfunc)
if err != nil {
log.Panic(err)
}
return files
}
func readFromFile(filePath string) string {
info, err := os.Stat(filePath)
if err != nil {
log.Println(err.Error())
return ""
}
if !info.IsDir() {
r, er := ioutil.ReadFile(filePath)
if er != nil {
log.Printf(er.Error())
return ""
}
return string(r)
}
return ""
}
func StartTakingBackup(status func(msg string), progressPercent func(p float64)) {
Outputf, e := os.Create(Outputf)
if e != nil {
panic(e)
}
w := zip.NewWriter(Outputf)
files := getfiles(status, progressPercent)
for _, file := range files {
f, err := w.Create(file.Name)
if err != nil {
log.Fatal(err)
}
status(file.Body)
//log.Println(file.Body)
_, err = f.Write([]byte(readFromFile(file.Body)))
fileWritten++
progressPercent(float64((fileWritten * 100.0) / fileCount))
if err != nil {
log.Fatal(err)
}
}
// Make sure to check the error on Close.
err := w.Close()
if err != nil {
log.Fatal(err)
}
}