-
Notifications
You must be signed in to change notification settings - Fork 0
/
compress.go
50 lines (46 loc) · 893 Bytes
/
compress.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
package resource
import (
"errors"
"fmt"
"os"
"path/filepath"
"github.com/sohaha/zlsgo/zfile"
)
func (r *Resource) Compress() (err error) {
if r.tmpPath == "" {
return errors.New("please execute the download method first")
}
tmp := zfile.RealPathMkdir(r.tmpPath+"/tmp", true)
switch r.Ext {
case "gz":
err = zfile.GzDeCompress(r.TmpFile, tmp)
case "zip":
err = zfile.ZipDeCompress(r.TmpFile, tmp)
default:
err = fmt.Errorf("compressed package format is not supported: %s", r.Ext)
}
if err != nil {
return err
}
r.tmpPath = fixPath(tmp)
return err
}
func fixPath(path string) string {
n := 0
dir := path
_ = filepath.Walk(path, func(p string, i os.FileInfo, err error) error {
if path == p {
return nil
}
n++
if i.IsDir() {
dir = p
return filepath.SkipDir
}
return nil
})
if n == 1 {
path = dir
}
return zfile.RealPath(path, true)
}