forked from borislav-angelov/wpress-archiver
/
wpress.go
156 lines (125 loc) · 2.79 KB
/
wpress.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package main
import (
"archive/zip"
"strings"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"github.com/yani-/wpress"
)
func usage() {
// Create our usage string
usage := fmt.Sprintf(
"Usage: %s COMMAND file\n" +
"\n" +
"WPRESS utility by ServMask Inc.\n" +
"\n" +
"Commands:\n" +
" extract Extract a wpress file\n" +
" compress Compress a folder or file to wpress archive\n" +
" convert Convert a zip archive to wpress archive\n", filepath.Base(os.Args[0]))
// Display the usage string
fmt.Println(usage)
// Exit normally
os.Exit(0)
}
func main() {
// Do we have the exact number of arguments?
if len(os.Args) != 3 {
usage()
}
task := os.Args[1]
file := os.Args[2]
switch task {
case "extract":
fmt.Println("Not implemented yet")
case "compress":
fmt.Println("Not implemented yet")
case "convert":
// Set destination folder
dest := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file))
// Make destination folder
os.MkdirAll(dest, 0777)
// Extract zip archive
unzip(file, dest)
// Compress into wpress format
compress(dest)
// Remove destination folder
os.RemoveAll(dest)
// Print OK
fmt.Println("OK")
default:
usage()
}
}
func compress(src string) {
archiver, _ := wpress.NewWriter(fmt.Sprintf("%s.wpress", src))
// Get current working folder
cwd, _ := os.Getwd()
// Go to destination folder
os.Chdir(src)
// List folder
folder := ""
files, _ := ioutil.ReadDir(".")
for _, f := range files {
if folder != "" {
folder = ""
break
} else {
folder = f.Name()
}
}
// Go to nested directory if necessary
if folder != "" {
os.Chdir(folder)
}
// Add directory recursively
archiver.AddDirectory(".")
archiver.Close()
// Go to previous folder
os.Chdir(cwd)
}
func unzip(zipfile string, dest string) {
reader, err := zip.OpenReader(zipfile)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer reader.Close()
for _, f := range reader.Reader.File {
zipped, err := f.Open()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer zipped.Close()
// Get current path
path := filepath.Join(dest, f.Name)
// Ignore files and folders
if strings.Contains(path, "__MACOSX") || strings.Contains(path, ".DS_Store") {
continue
}
if f.FileInfo().IsDir() {
// Create current directory
os.MkdirAll(path, f.Mode())
} else {
// Get sub directory
dir := filepath.Join(dest, filepath.Dir(f.Name))
// Create sub directory
os.MkdirAll(dir, f.Mode())
writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode())
if err != nil {
fmt.Println(err)
os.Exit(1)
}
if _, err = io.Copy(writer, zipped); err != nil {
fmt.Println(err)
os.Exit(1)
}
writer.Close()
fmt.Println("Decompressing : ", path)
}
}
}