-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathdirzip.go
54 lines (43 loc) · 1.02 KB
/
dirzip.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
package main
import (
"archive/zip"
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
)
// For handling compress dir structures...
type DirZip struct {
w http.ResponseWriter
d string
}
func (t *DirZip) Get() error {
zipFileName := fmt.Sprintf("%s.zip", filepath.Base(t.d))
t.w.Header().Set("Content-Type", "application/zip")
t.w.Header().Set("Content-Disposition", `attachment; filename="`+zipFileName+`"`)
zw := zip.NewWriter(t.w)
defer zw.Close()
filepath.Walk(t.d, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
zipPath := path[len(t.d):]
zipPath = strings.TrimLeft(strings.Replace(zipPath, `\`, "/", -1), `/`)
ze, err := zw.Create(zipPath)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot create zip entry <%s>: %s\n", zipPath, err)
return err
}
file, err := os.Open(path)
if err != nil {
fmt.Fprintf(os.Stderr, "Cannot open file <%s>: %s\n", path, err)
return err
}
defer file.Close()
io.Copy(ze, file)
return nil
})
return nil
}