/
files_zip_pack.go
77 lines (67 loc) · 1.49 KB
/
files_zip_pack.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
package elem
import (
"archive/zip"
"bytes"
"github.com/Bitspark/slang/pkg/core"
"github.com/google/uuid"
)
var filesZIPPackCfg = &builtinConfig{
safe: false,
blueprint: core.Blueprint{
Id: uuid.MustParse("dc5325bc-a816-47c8-8a8a-f741497459f7"),
Meta: core.BlueprintMetaDef{
Name: "pack ZIP",
ShortDescription: "packs a stream of binary content into a zip archive",
Icon: "file-archive",
Tags: []string{"zip", "compression"},
DocURL: "https://bitspark.de/slang/docs/operator/pack-zip",
},
ServiceDefs: map[string]*core.ServiceDef{
core.MAIN_SERVICE: {
In: core.TypeDef{
Type: "stream",
Stream: &core.TypeDef{
Type: "map",
Map: map[string]*core.TypeDef{
"path": {
Type: "string",
},
"file": {
Type: "binary",
},
},
},
},
Out: core.TypeDef{
Type: "binary",
},
},
},
},
opFunc: func(op *core.Operator) {
in := op.Main().In()
out := op.Main().Out()
for !op.CheckStop() {
i := in.Stream().Pull()
if !in.OwnBOS(i) {
out.Push(i)
continue
}
buf := new(bytes.Buffer)
zipWriter := zip.NewWriter(buf)
for {
i = in.Pull()
if in.OwnEOS(i) {
break
}
im := i.(map[string]interface{})
path := im["path"].(string)
file := im["file"].(core.Binary)
fileWriter, _ := zipWriter.Create(path)
fileWriter.Write(file)
}
zipWriter.Close()
out.Push(core.Binary(buf.Bytes()))
}
},
}