-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharchiveManager.go
More file actions
63 lines (53 loc) · 1.14 KB
/
archiveManager.go
File metadata and controls
63 lines (53 loc) · 1.14 KB
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
package main
import (
ziplib "archive/zip"
"fmt"
"io"
"os"
"path/filepath"
)
func zip(source, destination string) {
files, err := listFiles(source)
if err != nil {
panic(err)
}
if len(files) == 0 {
fmt.Println("No files to zip.")
os.Exit(1)
}
archive, err := os.Create(destination)
if err != nil {
panic(err)
}
defer archive.Close()
zipWriter := ziplib.NewWriter(archive)
defer zipWriter.Close()
for _, file := range files {
fileToZip, _ := os.Open(file)
defer fileToZip.Close()
zipFilePath, _ := filepath.Rel(source, file)
writer, _ := zipWriter.Create(zipFilePath)
_, copyErr := io.Copy(writer, fileToZip)
if copyErr != nil {
panic(copyErr)
}
}
}
func unzip(source, destination string) {
reader, err := ziplib.OpenReader(source)
if err != nil {
panic(err)
}
defer reader.Close()
os.MkdirAll(destination, os.ModeDir)
for _, file := range reader.File {
fileToWrite, _ := os.Create(filepath.Join(destination, file.Name))
defer fileToWrite.Close()
fileToRead, _ := file.Open()
defer fileToRead.Close()
_, copyErr := io.Copy(fileToWrite, fileToRead)
if copyErr != nil {
panic(copyErr)
}
}
}