-
Notifications
You must be signed in to change notification settings - Fork 0
/
zip.go
232 lines (193 loc) · 4.67 KB
/
zip.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package giawarc
import (
"bufio"
"bytes"
"compress/gzip"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"strings"
"strconv"
"unicode/utf8"
"github.com/ulikunitz/xz"
xzreader "github.com/xi2/xz"
)
type ZipWriter struct {
fp *os.File
compression string
}
type zipWire struct {
URI string
ContentType string
Lang string
}
func (zw ZipWriter) Close() (err error) {
return zw.fp.Close()
}
func (zw ZipWriter) Write(buf []byte) (n int, err error) {
return zw.fp.Write(buf)
}
func NewZipWriter(out string, compression string) (z ZipWriter, err error) {
fp, err := os.OpenFile(out, os.O_APPEND|os.O_CREATE|os.O_WRONLY, os.ModePerm)
if err != nil {
return
}
z = ZipWriter{fp: fp, compression: compression}
return
}
func (zw ZipWriter) WriteHashes(hashes map[uint32]struct{}) (n int, err error) {
var buf bytes.Buffer
var z io.WriteCloser
if zw.compression == "xz" {
z, err = xz.NewWriter(&buf)
} else {
z = gzip.NewWriter(&buf)
}
for hash, _ := range hashes{
fmt.Fprintf(z, "%d\n", hash)
}
err = z.Close()
if err != nil {
return
}
n, err = zw.Write(buf.Bytes())
return
}
func (zw ZipWriter) WriteText(page *TextRecord) (n int, err error) {
var buf bytes.Buffer
var z io.WriteCloser
if zw.compression == "xz" {
z, err = xz.NewWriter(&buf)
} else {
z = gzip.NewWriter(&buf)
}
// z.Name = page.RecordId
fmt.Fprintf(z, "Content-Location: %s\n", page.URI)
fmt.Fprintf(z, "Content-Type: %s\n", page.ContentType)
fmt.Fprintf(z, "Content-Language: %s\n", page.Lang)
fmt.Fprintf(z, "Content-Length: %d\n", len(page.Text))
fmt.Fprintf(z, "Date: %s\n", page.Date)
fmt.Fprintf(z, "X-WARC-Record-ID: <urn:uuid:%s>\n", page.RecordId)
fmt.Fprintf(z, "X-WARC-Filename: %s\n", page.Source)
fmt.Fprintf(z, "\n")
_, err = z.Write([]byte(page.Text))
if err != nil {
return
}
fmt.Fprintf(z, "\n")
err = z.Close()
if err != nil {
return
}
n, err = zw.Write(buf.Bytes())
return
}
// GZ OR XZ READER
type multiStreamReader interface {
io.Reader
Multistream(bool)
Reset(io.Reader) error
}
type GzOrXzReader struct {
reader multiStreamReader
buf io.Reader
fp *os.File
compression string
}
func NewGzOrXzReader(compressionType string, filename string) (z GzOrXzReader, err error){
fp, err := os.Open(filename)
if err != nil {
return
}
buf := bufio.NewReader(fp)
var msr multiStreamReader
if compressionType == "xz" {
msr, err= xzreader.NewReader(buf, 0)
} else if compressionType == "gz" {
msr, err = gzip.NewReader(buf)
}
if err != nil{
return
}
z = GzOrXzReader{reader: msr, buf: buf, fp: fp, compression: compressionType}
return
}
func (z GzOrXzReader) Close() {
z.fp.Close()
}
func (z GzOrXzReader) Multistream (ms bool) {
z.reader.Multistream(ms)
}
func (z GzOrXzReader) Reset () (error){
var err error
if z.compression == "xz" {
err = z.reader.Reset(nil)
} else if z.compression == "gz" {
err = z.reader.Reset(z.buf)
}
return err
}
func (z GzOrXzReader) GetReader () (io.Reader){
return z.reader
}
var Empty struct{}
func (z GzOrXzReader) ReadHashes() (map[uint32]struct{}) {
hashes := make (map[uint32]struct{})
reader := bufio.NewReader(z.reader)
var line string
var err error
for {
line, err = reader.ReadString('\n')
if err != nil{
break
}
hash, err := strconv.ParseInt(strings.TrimSpace(line), 10, 64)
if err != nil{
continue
}
// hashes = append(hashes, uint32(hash))
hashes[uint32(hash)]=Empty
}
return hashes
}
func (z GzOrXzReader) ReadText() (page *TextRecord, err error) {
// TODO make this more efficient, it copyies around data too much
var buf bytes.Buffer
buf.WriteString("HTTP/1.0 200 OK\n")
if _, err = buf.ReadFrom(z.reader); err != nil {
return
}
resp, err := http.ReadResponse(bufio.NewReader(&buf), nil)
if err != nil {
return
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
var t TextRecord
// t.URI = resp.Header.Get("Content-Location")
// t.ContentType = resp.Header.Get("Content-Type")
// t.Lang = resp.Header.Get("Content-Language")
// t.Date = resp.Header.Get("Date")
// t.RecordId = resp.Header.Get("X-WARC-Record-ID")
// t.Source = resp.Header.Get("X-WARC-Filename")
// t.Text = string(body)
t.URI = strings.Map(fixUtf, resp.Header.Get("Content-Location"))
t.ContentType = strings.Map(fixUtf, resp.Header.Get("Content-Type"))
t.Lang = strings.Map(fixUtf, resp.Header.Get("Content-Language"))
t.Date = strings.Map(fixUtf, resp.Header.Get("Date"))
t.RecordId = strings.Map(fixUtf, resp.Header.Get("X-WARC-Record-ID"))
t.Source = strings.Map(fixUtf, resp.Header.Get("X-WARC-Filename"))
t.Text = strings.Map(fixUtf, string(body))
page = &t
return
}
func fixUtf(r rune) rune {
if r == utf8.RuneError {
return -1
}
return r
}