-
Notifications
You must be signed in to change notification settings - Fork 1
/
unpack
executable file
·53 lines (47 loc) · 1.02 KB
/
unpack
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
#!/bin/sh
# unpack - unarchive archive files
# this file in public domain
err()
{
printf 'unpack: %s\n' "$1" >&2
exit 1
}
usage()
{
echo "usage: unpack file [destdir]" >&2
exit 1
}
case "$#" in
(1) set -- "$1" "$PWD" ;;
(2) ;;
(*) usage ;;
esac
test -f "$1" || err "$1: not a regular file"
test -d "$2" || err "$2: not a regular directory"
case "$(file -ib -- "$1" | cut -d';' -f1 | cut -d'/' -f2)" in
gzip|x-gzip)
tar xzvf "$1" -C "$2" >/dev/null || err "extraction failed"
;;
bzip2|x-bzip2)
tar xjvf "$1" -C "$2" >/dev/null || err "extraction failed"
;;
tar|x-tar)
tar xvf "$1" -C "$2" >/dev/null || err "extraction failed"
;;
zip)
unzip -d "$2" -- "$1" >/dev/null || err "extraction failed"
;;
x-rar)
unrar x "$1" "$2" >/dev/null || err "extraction failed"
;;
x-7z-compressed)
7z x -o"$2" "$1" >/dev/null || err "extraction failed"
;;
x-xz)
xzcat "$1" | tar xvf - -C "$2" >/dev/null || err "extraction failed"
;;
*)
err "%s: unknown archive file"
;;
esac
printf "%s -> %s\n" "$1" "$2"