-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitextor.go
150 lines (121 loc) · 2.6 KB
/
bitextor.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package giawarc
import (
"compress/gzip"
"encoding/base64"
"io"
"os"
"path/filepath"
"github.com/ulikunitz/xz"
)
type Zip struct {
zip io.WriteCloser
fp io.WriteCloser
}
type XZip struct {
xzip io.WriteCloser
fp io.WriteCloser
}
func NewZippedFile(outdir string, name string) (z Zip, err error) {
var zz Zip
path := filepath.Join(outdir, name)
zz.fp, err = os.Create(path)
// zz.fp, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return
}
gz, _ := gzip.NewWriterLevel(zz.fp, gzip.BestCompression)
gz.Name = name
gz.Comment = "Written by giawarc"
zz.zip = gz
z = zz
return
}
func NewXZipFile(outdir string, name string) (x XZip, err error) {
var xx XZip
path := filepath.Join(outdir, name)
xx.fp, err = os.Create(path)
// xx.fp, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return xx, err
}
w, err := xz.NewWriter(xx.fp)
xx.xzip = w
return xx, err
}
func (z Zip) Write(buf []byte) (int, error) {
return z.zip.Write(buf)
}
func (x XZip) Write(buf []byte) (int, error) {
return x.xzip.Write(buf)
}
func (z Zip) Close() (err error) {
z.zip.Close()
return z.fp.Close()
}
func (x XZip) Close() (err error) {
x.xzip.Close()
return x.fp.Close()
}
func WriteLine(w io.Writer, s string) (err error) {
if _, err = w.Write([]byte(s)); err != nil {
return
}
_, err = w.Write([]byte("\n"))
return
}
type BitextorWriter struct {
mime io.WriteCloser
lang io.WriteCloser
url io.WriteCloser
plain io.WriteCloser
}
func NewBitextorWriter(outdir string, writeLang bool) (tw TextWriter, err error) {
mime, err := NewXZipFile(outdir, "mime.xz")
if err != nil {
return
}
var lang io.WriteCloser
lang = nil
if writeLang {
lang, err = NewXZipFile(outdir, "lang.xz")
if err != nil {
return
}
}
url, err := NewXZipFile(outdir, "url.xz")
if err != nil {
return
}
plain, err := NewXZipFile(outdir, "plain_text.xz")
if err != nil {
return
}
return BitextorWriter{mime: mime, lang: lang, url: url, plain: plain}, nil
}
func (bw BitextorWriter) WriteText(text *TextRecord) (n int, err error) {
if err = WriteLine(bw.mime, text.ContentType); err != nil {
return
}
if (bw.lang != nil){
if err = WriteLine(bw.lang, text.Lang); err != nil {
return
}
}
if err = WriteLine(bw.url, text.URI); err != nil {
return
}
b64 := base64.StdEncoding.EncodeToString([]byte(text.Text))
if err = WriteLine(bw.plain, b64); err != nil {
return
}
return
}
func (bw BitextorWriter) Close() (err error) {
bw.mime.Close()
if bw.lang != nil {
bw.lang.Close()
}
bw.url.Close()
bw.plain.Close()
return
}