/
main.go
353 lines (312 loc) · 8.44 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"archive/tar"
"archive/zip"
"bytes"
"compress/flate"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/mholt/archiver/v3"
"github.com/nwaples/rardecode"
)
var (
compressionLevel int
overwriteExisting bool
mkdirAll bool
selectiveCompression bool
implicitTopLevelFolder bool
continueOnError bool
)
func init() {
flag.IntVar(&compressionLevel, "level", flate.DefaultCompression, "Compression level")
flag.BoolVar(&overwriteExisting, "overwrite", false, "Overwrite existing files")
flag.BoolVar(&mkdirAll, "mkdirs", false, "Make all necessary directories")
flag.BoolVar(&selectiveCompression, "smart", true, "Only compress files which are not already compressed (zip only)")
flag.BoolVar(&implicitTopLevelFolder, "folder-safe", true, "If an archive does not have a single top-level folder, create one implicitly")
flag.BoolVar(&continueOnError, "allow-errors", true, "Log errors and continue processing")
}
func main() {
if len(os.Args) >= 2 &&
(os.Args[1] == "-h" || os.Args[1] == "--help" || os.Args[1] == "help") {
fmt.Println(usageString())
os.Exit(0)
}
if len(os.Args) < 3 {
fatal(usageString())
}
flag.Parse()
subcommand := flag.Arg(0)
// get the format we're working with
iface, err := getFormat(subcommand)
if err != nil {
fatal(err)
}
// run the desired command
switch subcommand {
case "archive":
a, ok := iface.(archiver.Archiver)
if !ok {
fatalf("the archive command does not support the %s format", iface)
}
var sources []string
for _, src := range flag.Args()[2:] {
srcs, err := filepath.Glob(src)
if err != nil {
fatalf(err.Error())
}
sources = append(sources, srcs...)
}
err = a.Archive(sources, flag.Arg(1))
case "unarchive":
u, ok := iface.(archiver.Unarchiver)
if !ok {
fatalf("the unarchive command does not support the %s format", iface)
}
err = u.Unarchive(flag.Arg(1), flag.Arg(2))
case "extract":
e, ok := iface.(archiver.Extractor)
if !ok {
fatalf("the extract command does not support the %s format", iface)
}
err = e.Extract(flag.Arg(1), flag.Arg(2), flag.Arg(3))
case "ls":
w, ok := iface.(archiver.Walker)
if !ok {
fatalf("the ls command does not support the %s format", iface)
}
var count int
err = w.Walk(flag.Arg(1), func(f archiver.File) error {
count++
switch h := f.Header.(type) {
case zip.FileHeader:
fmt.Printf("%s\t%d\t%d\t%s\t%s\n",
f.Mode(),
h.Method,
f.Size(),
f.ModTime(),
h.Name,
)
case *tar.Header:
fmt.Printf("%s\t%s\t%s\t%d\t%s\t%s\n",
f.Mode(),
h.Uname,
h.Gname,
f.Size(),
f.ModTime(),
h.Name,
)
case *rardecode.FileHeader:
fmt.Printf("%s\t%d\t%d\t%s\t%s\n",
f.Mode(),
int(h.HostOS),
f.Size(),
f.ModTime(),
h.Name,
)
default:
fmt.Printf("%s\t%d\t%s\t?/%s\n",
f.Mode(),
f.Size(),
f.ModTime(),
f.Name(), // we don't know full path from this
)
}
return nil
})
fmt.Printf("total %d\n", count)
case "compress":
c, ok := iface.(archiver.Compressor)
if !ok {
fatalf("the compress command does not support the %s format", iface)
}
fc := archiver.FileCompressor{Compressor: c}
in := flag.Arg(1)
out := flag.Arg(2)
var deleteWhenDone bool
if cs, ok := c.(fmt.Stringer); ok && out == cs.String() {
out = in + "." + out
deleteWhenDone = true
}
err = fc.CompressFile(in, out)
if err == nil && deleteWhenDone {
err = os.Remove(in)
}
case "decompress":
c, ok := iface.(archiver.Decompressor)
if !ok {
fatalf("the compress command does not support the %s format", iface)
}
fc := archiver.FileCompressor{Decompressor: c}
in := flag.Arg(1)
out := flag.Arg(2)
var deleteWhenDone bool
if cs, ok := c.(fmt.Stringer); ok && out == "" {
out = strings.TrimSuffix(in, "."+cs.String())
deleteWhenDone = true
}
err = fc.DecompressFile(in, out)
if err == nil && deleteWhenDone {
err = os.Remove(in)
}
default:
fatalf("unrecognized command: %s", flag.Arg(0))
}
if err != nil {
fatal(err)
}
}
func getFormat(subcommand string) (interface{}, error) {
// prepare the filename, with which we will find a suitable format
formatPos := 1
if subcommand == "compress" {
formatPos = 2
}
filename := flag.Arg(formatPos)
if subcommand == "compress" && !strings.Contains(filename, ".") {
filename = "." + filename // leading dot needed for extension matching
}
// get the format by filename extension
f, err := archiver.ByExtension(filename)
if err != nil {
return nil, err
}
// prepare a single Tar, in case it's needed
mytar := &archiver.Tar{
OverwriteExisting: overwriteExisting,
MkdirAll: mkdirAll,
ImplicitTopLevelFolder: implicitTopLevelFolder,
ContinueOnError: continueOnError,
}
// fully configure the new value
switch v := f.(type) {
case *archiver.Rar:
v.OverwriteExisting = overwriteExisting
v.MkdirAll = mkdirAll
v.ImplicitTopLevelFolder = implicitTopLevelFolder
v.ContinueOnError = continueOnError
v.Password = os.Getenv("ARCHIVE_PASSWORD")
case *archiver.Tar:
v = mytar
case *archiver.TarBrotli:
v.Tar = mytar
v.Quality = compressionLevel
case *archiver.TarBz2:
v.Tar = mytar
v.CompressionLevel = compressionLevel
case *archiver.TarGz:
v.Tar = mytar
v.CompressionLevel = compressionLevel
case *archiver.TarLz4:
v.Tar = mytar
v.CompressionLevel = compressionLevel
case *archiver.TarSz:
v.Tar = mytar
case *archiver.TarXz:
v.Tar = mytar
case *archiver.TarZstd:
v.Tar = mytar
case *archiver.Zip:
v.CompressionLevel = compressionLevel
v.OverwriteExisting = overwriteExisting
v.MkdirAll = mkdirAll
v.SelectiveCompression = selectiveCompression
v.ImplicitTopLevelFolder = implicitTopLevelFolder
v.ContinueOnError = continueOnError
case *archiver.Gz:
v.CompressionLevel = compressionLevel
case *archiver.Brotli:
v.Quality = compressionLevel
case *archiver.Bz2:
v.CompressionLevel = compressionLevel
case *archiver.Lz4:
v.CompressionLevel = compressionLevel
case *archiver.Snappy:
// nothing to customize
case *archiver.Xz:
// nothing to customize
case *archiver.Zstd:
// nothing to customize
default:
return nil, fmt.Errorf("format does not support customization: %s", f)
}
return f, nil
}
func fatal(v ...interface{}) {
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}
func fatalf(s string, v ...interface{}) {
fmt.Fprintf(os.Stderr, s+"\n", v...)
os.Exit(1)
}
func usageString() string {
buf := new(bytes.Buffer)
buf.WriteString(usage)
flag.CommandLine.SetOutput(buf)
flag.CommandLine.PrintDefaults()
return buf.String()
}
const usage = `Usage: arc {archive|unarchive|extract|ls|compress|decompress|help} [arguments...]
archive
Create a new archive file. List the files/folders
to include in the archive; at least one required.
unarchive
Extract an archive file. Provide the archive to
open and the destination folder to extract into.
extract
Extract a single file or folder (recursively) from
an archive. First argument is the source archive,
second is the file to extract (exact path within the
archive is required), and third is destination.
ls
List the contents of the archive.
compress
Compresses a file, destination optional.
decompress
Decompresses a file, destination optional.
help
Display this help text. Also -h or --help.
SPECIFYING THE ARCHIVE FORMAT
The format of the archive is determined by its
file extension. Supported extensions:
.zip
.tar
.tar.br
.tbr
.tar.gz
.tgz
.tar.bz2
.tbz2
.tar.xz
.txz
.tar.lz4
.tlz4
.tar.sz
.tsz
.zst
.tar.zst
.rar (open only)
.bz2
.gz
.lz4
.sz
.xz
(DE)COMPRESSING SINGLE FILES
Some formats are compression-only, and can be used
with the compress and decompress commands on a
single file; they do not bundle multiple files.
To replace a file when compressing, specify the
source file name for the first argument, and the
compression format (without leading dot) for the
second argument. To replace a file when decompressing,
specify only the source file and no destination.
PASSWORD-PROTECTED RAR FILES
Export the ARCHIVE_PASSWORD environment variable
to be able to open password-protected rar archives.
GLOBAL FLAG REFERENCE
The following global flags may be used before the
sub-command (some flags are format-specific):
`