-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutils.go
90 lines (71 loc) · 1.53 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
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"io"
"os"
"archive/zip"
"encoding/base64"
"path/filepath"
"golang.org/x/text/encoding/unicode"
)
func shouldSkip(substr string) bool {
for _, v := range skips {
if v == substr {
return true
}
}
return false
}
func convertUtf8ToUtf16LE(message string) (string, error) {
utf16le := unicode.UTF16(unicode.LittleEndian, unicode.IgnoreBOM)
utfEncoder := utf16le.NewEncoder()
ut16LeEncodedMessage, err := utfEncoder.String(message)
return ut16LeEncodedMessage, err
}
func powershellEncode(message string) (string, error) {
utf16LEEncodedMessage, err := convertUtf8ToUtf16LE(message)
if err != nil {
return "", err
}
input := []uint8(utf16LEEncodedMessage)
return base64.StdEncoding.EncodeToString(input), nil
}
func zipSource(src, dst string) error {
f, err := os.Create(dst)
if err != nil {
return err
}
defer f.Close()
writer := zip.NewWriter(f)
defer writer.Close()
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
header, err := zip.FileInfoHeader(info)
if err != nil {
return err
}
header.Method = zip.Deflate
header.Name, err = filepath.Rel(filepath.Dir(src), path)
if err != nil {
return err
}
if info.IsDir() {
header.Name += "/"
}
headerWriter, err := writer.CreateHeader(header)
if err != nil {
return err
}
if info.IsDir() {
return nil
}
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(headerWriter, f)
return err
})
}