-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
79 lines (60 loc) · 1.47 KB
/
utils.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
package main
import (
"archive/zip"
"fmt"
"io"
"io/ioutil"
"os"
)
// copyFile copies the src file to dest.
func copyFile(src string, dest string) error {
var srcFile, err = ioutil.ReadFile(src)
if err != nil {
return err
}
return writeFile(dest, &srcFile)
}
// deleteFile removes the given filename.
func deleteFile(filename string) error {
if err := os.Remove(filename); err != nil {
return err
}
return nil
}
// writeFile writes the given file name using buf content.
func writeFile(filename string, buf *[]byte) error {
if err := ioutil.WriteFile(filename, *buf, 0644); err != nil {
return err
}
return nil
}
// zipFile compress the given src file and write it to dest.
func zipFile(src string, dest string) error {
zFile, err := os.Create(dest)
if err != nil {
return fmt.Errorf("archive file creation error: %v", err)
}
defer zFile.Close()
zWriter := zip.NewWriter(zFile)
defer zWriter.Close()
srcFile, err := os.Open(src)
if err != nil {
return fmt.Errorf("file open error: %v", err)
}
defer srcFile.Close()
srcInfo, err := srcFile.Stat()
if err != nil {
return fmt.Errorf("file stat error: %v", err)
}
srcHeader, err := zip.FileInfoHeader(srcInfo)
if err != nil {
return fmt.Errorf("file header info error: %v", err)
}
srcHeader.Method = zip.Deflate
zWriter2, err := zWriter.CreateHeader(srcHeader)
if err != nil {
return fmt.Errorf("archive file append error: %v", err)
}
_, err = io.Copy(zWriter2, srcFile)
return err
}