-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
75 lines (67 loc) · 1.31 KB
/
example_test.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
package zipserve_test
import (
"github.com/martin-sucha/zipserve"
"hash/crc32"
"io"
"log"
"net/http"
"os"
"path/filepath"
)
func templateFromDir(root string) (*zipserve.Template, error) {
t := &zipserve.Template{}
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if path == root || !(info.Mode().IsRegular() || info.Mode().IsDir()) {
return nil
}
header, err := zipserve.FileInfoHeader(info)
if err != nil {
return err
}
relpath, err := filepath.Rel(root, path)
if err != nil {
return err
}
header.Name = relpath
if info.Mode().IsRegular() {
file, err := os.Open(path)
if err != nil {
return err
}
header.Content = file
crc := crc32.NewIEEE()
_, err = io.Copy(crc, file)
if err != nil {
return err
}
header.CRC32 = crc.Sum32()
} else {
header.Name = header.Name + "/"
}
t.Entries = append(t.Entries, header)
return nil
})
if err != nil {
return nil, err
}
return t, nil
}
func Example() {
cwd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
t, err := templateFromDir(cwd)
if err != nil {
log.Fatal(err)
}
archive, err := zipserve.NewArchive(t)
if err != nil {
log.Fatal(err)
}
http.Handle("/", archive)
log.Fatal(http.ListenAndServe(":8080", nil))
}