-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
80 lines (64 loc) · 1.47 KB
/
main.go
File metadata and controls
80 lines (64 loc) · 1.47 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"archive/zip"
"fmt"
"io/fs"
"io/ioutil"
"os"
"runtime"
"sync"
"time"
)
func main() {
PrintMemoryUsage()
var await sync.WaitGroup
const count = 1
fmt.Printf("Fazendo a operação %d vezes\n", count)
start := time.Now()
for _, _ = range [count]int{} {
await.Add(1)
go WriteZip(&await)
}
await.Wait()
totalTime := time.Since(start)
fmt.Println("Tempo de execução:" + totalTime.String())
}
func WriteZip(await *sync.WaitGroup) {
defer await.Done()
basePathPdf := "pdf2/"
basePathZip := "outFile/"
if _, err := os.Stat(basePathZip); os.IsNotExist(err) {
_ = os.Mkdir(basePathZip, 0700)
}
outFile, err := os.CreateTemp(basePathZip, "compress*.zip")
if err != nil {
fmt.Println(err)
}
//defer os.Remove(outFile.Name())
w := zip.NewWriter(outFile)
files, err := ioutil.ReadDir(basePathPdf)
if err != nil {
fmt.Println(err)
}
for _, file := range files {
AddFile(file, w, basePathPdf, basePathZip)
}
PrintMemoryUsage()
//err = w.Close()
if err != nil {
fmt.Println(err)
}
}
func AddFile(file fs.FileInfo, w *zip.Writer, basePathPdf, basePathZip string) {
if !file.IsDir() {
data, _ := ioutil.ReadFile(basePathPdf + file.Name())
f, _ := w.Create(basePathPdf + file.Name())
_, _ = f.Write(data)
}
}
func PrintMemoryUsage() {
var memory runtime.MemStats
runtime.ReadMemStats(&memory)
fmt.Printf("Memory usage: %vMB\n", memory.Alloc/1024/1024)
fmt.Printf("Total Memory usage: %vMB\n", memory.TotalAlloc/1024/1024)
}