-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
zipTools.go
90 lines (81 loc) · 2.08 KB
/
zipTools.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 go_utils
import (
zip1 "archive/zip"
"bytes"
"compress/gzip"
"log"
)
/*
压缩多个文件
d 为 map[string][]byte{文件名1:文件内容1,文件名2:文件内容2,}
var mJar = map[string][]byte{
"buildServerResources/" + pluginName + ".jsp": X3Jsp}
// pluginName+".zip"
var mZip = map[string][]byte{
"buildServerResources/" + pluginName + ".jsp": X3Jsp,
"server/" + pluginName + ".jar": CreateZip(mJar),
"teamcity-plugin.xml": []byte(`?xml version="1.0" encoding="UTF-8"?>
<teamcity-plugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:schemas-jetbrains-com:teamcity-plugin-v1-xml">
<info>
<name>` + pluginName + `</name>
<display-name>` + pluginName + `</display-name>
<description>South rise such since land project enough.</description>
<version>1.0</version>
<vendor>
<name>Salas, Smith and Williams</name>
<url>http://meza-smith.biz/</url>
</vendor>
</info>
<deployment use-separate-classloader="true" node-responsibilities-aware="true"/>
</teamcity-plugin>`),
}
var data = CreateZip(mZip)
*/
func CreateZip(d map[string][]byte) []byte {
buf := bytes.NewBuffer(nil)
if zw := zip1.NewWriter(buf); nil != zw {
for k, v := range d {
// 创建文件写入器
w, err := zw.Create(k)
if err == nil { // 写入文件内容
_, err = w.Write(v)
if err != nil {
log.Println(k, "Write", err)
}
} else {
log.Println(k, "Create", err)
}
}
zw.Close()
}
data := buf.Bytes()
buf = nil
return data
}
func GzipBytes(data []byte) []byte {
var buf bytes.Buffer
gz := gzip.NewWriter(&buf)
if _, err := gz.Write(data); err != nil {
return nil
}
if err := gz.Close(); err != nil {
return nil
}
return buf.Bytes()
}
//func RmZipFile(zipFile string, rmFs ...string) {
// jarWriter, err := zip33.OpenReader(zipFile)
// if err != nil {
// log.Println(err)
// return
// }
// defer jarWriter.Close()
// for _, file := range jarWriter.File {
// for _, x := range rmFs {
// if file.Name == x {
// file.Remove()
//
// }
// }
// }
//}