-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.go
54 lines (41 loc) · 993 Bytes
/
file.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
package main
import (
"archive/zip"
"fmt"
"io"
"os"
)
type File struct {
Path string
status string
}
// check if file exists
func (file *File) IsExists() bool {
if file.Path == "" {
return false
}
if _, err := os.Stat(file.Path); os.IsNotExist(err) {
return false
}
return true
}
// returns false if file can't be added to archive
func (file *File) CanAdd() bool {
return file.IsExists() && file.status != "D" && file.status != "R"
}
// add file to zip archive
func (file *File) AddToArchive(zipWriter *zip.Writer) error {
fileReader, err := os.Open(file.Path)
if err != nil {
return fmt.Errorf("Can't open file %s: %s", file.Path, err)
}
defer fileReader.Close()
fileWriter, err := zipWriter.Create(file.Path)
if err != nil {
return fmt.Errorf("Can't add file %s to archive: %s", file.Path, err)
}
if _, err := io.Copy(fileWriter, fileReader); err != nil {
return fmt.Errorf("Can't writer file %s to archive: %s", file.Path, err)
}
return nil
}