-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.go
More file actions
77 lines (62 loc) · 1.96 KB
/
extract.go
File metadata and controls
77 lines (62 loc) · 1.96 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
package main
import (
"os"
"strings"
"sync"
)
// The information needed to extract a zipfile to it's destination in .minecraft.
type ExtractBundle struct {
zipfile string
destination string
}
// A slice of ExtractBundle structs.
type ExtractRecord []ExtractBundle
// includesZipFile returns true if the record contains the given zipfile.
func (ea ExtractRecord) includesZipfile(zipfile string) bool {
for _, v := range ea {
if v.zipfile == zipfile {
return true
}
}
return false
}
// Extract extracts the record's zipfiles to their destination.
// src corresponds to the source directory, workDir corresponds to the temporary directory, and dest corresponds to the destination directory.
func (ea ExtractRecord) extract(src, workDir, dest string, res chan<- string) {
wg := sync.WaitGroup{}
for _, eb := range ea {
wg.Add(1)
go func(eb ExtractBundle) {
defer wg.Done()
eb.extractFile(src, workDir, dest, res)
}(eb)
}
wg.Wait()
}
func (eb ExtractBundle) extractFile(src, workDir, dest string, res chan<- string) {
err := Unzip(src+"\\"+eb.zipfile, workDir)
CheckPanic(err)
os.MkdirAll(dest+"\\"+eb.destination, 0755)
unzippedDir := workDir + "\\" + strings.Split(eb.zipfile, ".zip")[0]
wg := sync.WaitGroup{}
files, err := os.ReadDir(unzippedDir)
CheckPanic(err)
for _, file := range files {
wg.Add(1)
go func(file string) {
defer wg.Done()
moveExtractedFile(file, unzippedDir, eb.destination, dest, res)
}(file.Name())
}
wg.Wait()
}
// moveExtractedFiles moves the files from the workingdirectory to the targetFolder in the target directory.
func moveExtractedFile(file, workDir, targetFolder, targetDir string, res chan<- string) {
if !PathExists(targetDir + "\\" + targetFolder + "\\" + file) {
err := CopyFile(workDir+"\\"+file, targetDir+"\\"+targetFolder+"\\"+file)
CheckPanic(err)
res <- ("Successfully added " + file + " to " + targetFolder + ".")
} else {
res <- (file + " already exists in " + targetFolder + ".")
}
}