-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
135 lines (120 loc) · 2.58 KB
/
main.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
package main
import (
"archive/zip"
"bytes"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"golang.org/x/text/transform"
)
var zipPath string
var distDirPath string
var mode int
var encode string
const (
ZIP_MODE int = 1
UNZIP_MODE int = 2
)
type SrcFiles []string
func (i *SrcFiles) String() string {
return ""
}
func (i *SrcFiles) Set(value string) error {
*i = append(*i, value)
return nil
}
var srcFiles SrcFiles
func init() {
flag.StringVar(&encode, "code", "", "support GBK,UTF-8")
flag.StringVar(&zipPath, "zipPath", "", "zip file path")
flag.StringVar(&distDirPath, "distDirPath", "", "dir path")
flag.Var(&srcFiles, "file", "-file 1.txt -file 2.txt -file 3.txt")
flag.IntVar(&mode, "mode", ZIP_MODE, "1=zip, 2=unzip")
}
func main() {
flag.Parse()
if mode == UNZIP_MODE {
fmt.Println("UNZIP:", zipPath, "-->", distDirPath, "code:", encode)
err := UnZip(distDirPath, zipPath)
if err != nil {
fmt.Println(err)
os.Exit(-3)
}
return
}
if mode == ZIP_MODE {
fmt.Println("ZIP:", srcFiles, "-->", zipPath, "code:", encode)
err := Zip(srcFiles, zipPath)
if err != nil {
fmt.Println(err)
os.Exit(-2)
}
return
}
fmt.Println("Err Mode Params!")
os.Exit(-1)
}
func Zip(srcFiles []string, dest string) error {
var files []*os.File
for _, fileName := range srcFiles {
file, err := os.Open(fileName)
if err != nil {
log.Fatalln(err)
return err
}
files = append(files, file)
}
d, _ := os.Create(dest)
defer d.Close()
w := zip.NewWriter(d)
defer w.Close()
for _, file := range files {
err := fileToZipWriter(file, "", w)
if err != nil {
return err
}
}
return nil
}
func UnZip(distDirPath, zipPath string) error {
zipFile, err := zip.OpenReader(zipPath)
if err != nil {
return err
}
defer zipFile.Close()
prefix := ""
if distDirPath != "" && distDirPath != "." {
prefix = distDirPath + "/"
}
for _, f := range zipFile.File {
nameReader := bytes.NewReader([]byte(f.Name))
decoder := transform.NewReader(nameReader, getDecoderByCoder(encode))
content, _ := ioutil.ReadAll(decoder)
filePath := string(content)
if f.FileInfo().IsDir() {
_ = os.MkdirAll(prefix+filePath, os.ModePerm)
continue
}
if err := os.MkdirAll(prefix+filepath.Dir(filePath), os.ModePerm); err != nil {
return err
}
dstFile, err := os.OpenFile(prefix+filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
return err
}
file, err := f.Open()
if err != nil {
return err
}
if _, err := io.Copy(dstFile, file); err != nil {
return err
}
dstFile.Close()
file.Close()
}
return nil
}