-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.go
70 lines (55 loc) · 1.22 KB
/
util.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
package jambon
import (
"archive/zip"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
func openReadableTacView(path string) (io.ReadCloser, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
if strings.HasSuffix(path, ".zip.acmi") {
stat, err := os.Stat(path)
if err != nil {
return nil, err
}
reader, err := zip.NewReader(file, stat.Size())
if err != nil {
return nil, err
}
if len(reader.File) != 1 {
return nil, fmt.Errorf("Too many entries in zip file, is it a valid tacview ACMI?")
}
return reader.Open(reader.File[0].Name)
}
return file, nil
}
// Wraps a zip writer with an unrelated closer
type zipWriteCloser struct {
io.Writer
file io.Closer
}
func (z *zipWriteCloser) Close() error {
return z.file.Close()
}
func openWritableTacView(path string) (io.WriteCloser, error) {
file, err := os.Create(path)
if err != nil {
return nil, err
}
if strings.HasSuffix(path, ".zip.acmi") {
writer := zip.NewWriter(file)
name := filepath.Base(path)
name = name[0:len(name)-len(".zip.acmi")] + ".txt.acmi"
zipFileWriter, err := writer.Create(name)
if err != nil {
return nil, err
}
return &zipWriteCloser{zipFileWriter, writer}, nil
}
return file, nil
}