-
Notifications
You must be signed in to change notification settings - Fork 0
/
goio.go
80 lines (74 loc) · 1.63 KB
/
goio.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
package goio
import (
"archive/zip"
"compress/bzip2"
"compress/gzip"
"fmt"
"io"
"log"
"os"
"path/filepath"
)
func Reader(f *os.File) (io.Reader, error) {
switch filepath.Ext(f.Name()) {
case ".gz":
//fmt.Println("Use gzip NewReader")
return gzip.NewReader(f)
case ".bz2":
return bzip2.NewReader(f), nil
case ".zip":
fi, err := f.Stat()
if err != nil {
return nil, err
}
size := fi.Size()
r, err := zip.NewReader(f, size)
if err != nil {
return nil, err
}
var files []io.Reader
for _, file := range r.File {
f, err := file.Open()
if err != nil {
return nil, err
}
files = append(files, f)
}
return io.MultiReader(files...), nil
default:
return f, nil
}
}
func Writer(f *os.File) (io.Writer, error) {
switch filepath.Ext(f.Name()) {
case ".gz":
fmt.Println("Use gzip NewWriter")
return gzip.NewWriter(f), nil
default:
return f, nil
}
}
func OpenReader(inputFname string) io.Reader {
//fmt.Println("Opening input file", inputFname, "...")
//fmt.Println("suffix:", filepath.Ext(inputFname))
inFile, err := os.Open(inputFname)
if err != nil {
log.Fatalln("ERROR: can't open input file", inputFname)
}
//defer inFile.Close()
reader, err := Reader(inFile)
if err != nil {
log.Fatalln("ERROR: can't open input reader", inputFname)
}
return reader
}
func OpenGzipWriter(outputFname string) *gzip.Writer {
fmt.Println("Opening output file", outputFname, "...")
outputFile, outErr := os.Create(outputFname)
if outErr != nil {
log.Fatalln("ERROR: can't open output file", outputFname)
}
//defer outputFile.Close()
outputWriter := gzip.NewWriter(outputFile)
return outputWriter
}