-
Notifications
You must be signed in to change notification settings - Fork 14
/
folder.go
138 lines (113 loc) · 2.76 KB
/
folder.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
package main
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
)
////////////////////////////////////////////////////////////////////////////////
type FxWalk func(path string) error
type VirtualFolder interface {
OpenFile(path string) (io.ReadCloser, error)
Walk(fnWalk FxWalk) error
ReadDirNames() ([]string, error)
Name() string
}
////////////////////////////////////////////////////////////////////////////////
type SystemFolder struct {
path string
}
func OpenSystemFolder(path string) *SystemFolder {
return &SystemFolder{path: path}
}
func (this *SystemFolder) OpenFile(path string) (io.ReadCloser, error) {
return os.Open(filepath.Join(this.path, path))
}
func (this *SystemFolder) Name() string {
return this.path
}
func (this *SystemFolder) Walk(fnWalk FxWalk) error {
walk := func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
path, _ = filepath.Rel(this.path, path)
return fnWalk(path)
}
return filepath.Walk(this.path, walk)
}
func (this *SystemFolder) ReadDirNames() ([]string, error) {
f, e := os.Open(this.path)
if e != nil {
return nil, e
}
defer f.Close()
return f.Readdirnames(-1)
}
////////////////////////////////////////////////////////////////////////////////
type ZipFolder struct {
zr *zip.Reader
name string
}
func NewZipFolder(data []byte) (*ZipFolder, error) {
r := bytes.NewReader(data)
if zr, e := zip.NewReader(r, int64(len(data))); e != nil {
return nil, e
} else {
return &ZipFolder{zr: zr, name: "<memory>"}, nil
}
}
func OpenZipFolder(path string) (*ZipFolder, error) {
if data, e := ioutil.ReadFile(path); e != nil {
return nil, e
} else if zf, e := NewZipFolder(data); e != nil {
return nil, e
} else {
zf.name = path
return zf, nil
}
}
func (this *ZipFolder) Name() string {
return this.name
}
func (this *ZipFolder) OpenFile(path string) (io.ReadCloser, error) {
for _, f := range this.zr.File {
if strings.ToLower(f.Name) == path {
return f.Open()
}
}
return nil, os.ErrNotExist
}
func (this *ZipFolder) Walk(fnWalk FxWalk) error {
for _, f := range this.zr.File {
if e := fnWalk(f.Name); e != nil {
return e
}
}
return nil
}
func (this *ZipFolder) ReadDirNames() ([]string, error) {
names := make([]string, len(this.zr.File))
for i, f := range this.zr.File {
names[i] = f.Name
}
return names, nil
}
////////////////////////////////////////////////////////////////////////////////
func OpenVirtualFolder(path string) (VirtualFolder, error) {
stat, e := os.Stat(path)
if e != nil {
return nil, e
}
if stat.IsDir() {
return OpenSystemFolder(path), nil
}
return OpenZipFolder(path)
}
////////////////////////////////////////////////////////////////////////////////