-
Notifications
You must be signed in to change notification settings - Fork 18
/
writer_file.go
58 lines (47 loc) · 1.21 KB
/
writer_file.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
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
package iplibrary
import (
"compress/gzip"
"os"
)
type FileWriter struct {
fp *os.File
gzWriter *gzip.Writer
rawWriter *Writer
}
func NewFileWriter(path string, meta *Meta) (*FileWriter, error) {
fp, err := os.OpenFile(path, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
if err != nil {
return nil, err
}
gzWriter, err := gzip.NewWriterLevel(fp, gzip.BestCompression)
if err != nil {
return nil, err
}
var writer = &FileWriter{
fp: fp,
gzWriter: gzWriter,
rawWriter: NewWriter(gzWriter, meta),
}
return writer, nil
}
func (this *FileWriter) WriteMeta() error {
return this.rawWriter.WriteMeta()
}
func (this *FileWriter) Write(ipFrom string, ipTo string, countryId int64, provinceId int64, cityId int64, townId int64, providerId int64) error {
return this.rawWriter.Write(ipFrom, ipTo, countryId, provinceId, cityId, townId, providerId)
}
func (this *FileWriter) Sum() string {
return this.rawWriter.Sum()
}
func (this *FileWriter) Close() error {
err1 := this.gzWriter.Close()
err2 := this.fp.Close()
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return nil
}