-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex
executable file
·188 lines (180 loc) · 4.97 KB
/
ex
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
#!/usr/bin/env sh
# vim: ft=sh ts=4 sw=4 sts=4 et :
# Extract files from archive, wisely
# Check if a file/directory does not exist
checkne() {
if [ -e "$1" ]; then
echo "Error: '$1' already exists"
return 1
fi
return 0
}
print_help() {
# No local in POSIX sh
scriptname=$(basename "$0")
cat <<EOF
$scriptname - archive extractor
Usage: $scriptname <file>
Supported formats:
*.tar.Z - compressed tar archive
*.Z - compressed file
*.tar.bz2 - bzip2 tar archive
*.tbz2 - bzip2 tar archive
*.bz2 - bzip2 file
*.tar.gz - gzip tar archive
*.tgz - gzip tar archive
*.gz - gzip file
*.tar.xz - xz tar archive
*.txz - xz tar archive
*.xz - xz file
*.tar.lzma - lzma tar archive
*.lzma - lzma file
*.tar.zst - zstd tar archive
*.tzst - zstd tar archive
*.zst - zstd file
*.tar.lz4 - lz4 tar archive
*.lz4 - lz4 file
*.rar - rar archive
*.tar - tar archive
*.zip - zip archive
*.7z - 7-zip archive
EOF
}
main() {
if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
print_help
return 0
fi
if [ ! -f "$1" ]; then
echo "'$1' is not a valid file"
return 1
fi
# No local in POSIX sh
fname=$(basename "$1")
# Extract directly if the archive contains only one file/directory,
# else we extract it into a separate directory
case "$1" in
*.tar.Z)
froot="${fname%.tar.Z}"
if [ "$(tar tZf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar xZf "$1" -C "$froot" || return 1
else
tar xZf "$1"
fi
;;
*.Z) uncompress "$1" ;;
*.tar.bz2 | *.tbz2)
froot="${fname%.*}"
if [ "$(tar tjf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar xjf "$1" -C "$froot" || return 1
else
tar xjf "$1"
fi
;;
*.bz2) bunzip2 "$1" ;;
*.tar.gz | *.tgz)
case "$fname" in
*.tar.gz) froot="${fname%.tar.gz}" ;;
*) froot="${fname%.tgz}" ;;
esac
if [ "$(tar tzf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar xzf "$1" -C "$froot" || return 1
else
tar xzf "$1"
fi
;;
*.gz)
froot="${fname%.gz}"
checkne "$froot" && gunzip -c "$1" >"$froot" || return 1
;;
*.tar.xz | *.txz)
case "$fname" in
*.tar.xz) froot="${fname%.tar.xz}" ;;
*) froot="${fname%.txz}" ;;
esac
if [ "$(tar tJf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar xJf "$1" -C "$froot" || return 1
else
tar xJf "$1"
fi
;;
*.xz) unxz "$1" ;;
*.tar.lzma)
froot="${fname%.tar.lzma}"
if [ "$(tar --lzma -tf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar --lzma -xf "$1" -C "$froot" || return 1
else
tar --lzma -xf "$1"
fi
;;
*.lzma) unlzma "$1" ;;
*.tar.zst | *.tzst)
case "$fname" in
*.tar.zst) froot="${fname%.tar.zst}" ;;
*) froot="${fname%.tzst}" ;;
esac
if [ "$(tar --zstd -tf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar --zstd -xf "$1" -C "$froot" || return 1
else
tar --zstd -xf "$1"
fi
;;
*.zst) zstd -d "$1" ;;
*.tar.lz4)
froot="${fname%.tar.lz4}"
if [ "$(tar --use-compress-program=lz4 -tf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar --use-compress-program=lz4 -xf "$1" -C "$froot" || return 1
else
tar --use-compress-program=lz4 -xf "$1"
fi
;;
*.lz4) lz4 -d "$1" ;;
*.rar)
froot="${fname%.rar}"
if unrar l "$1" | grep -q '^.*/$'; then
checkne "$froot" && mkdir "$froot" &&
unrar x "$1" "$froot" || return 1
else
unrar x "$1"
fi
;;
*.tar)
froot="${fname%.tar}"
if [ "$(tar tf "$1" | wc -l)" -gt 1 ]; then
checkne "$froot" && mkdir "$froot" &&
tar xf "$1" -C "$froot" || return 1
else
tar xf "$1"
fi
;;
*.zip)
froot="${fname%.zip}"
if zipinfo -1 "$1" | grep -q '^.*/$'; then
checkne "$froot" && mkdir "$froot" &&
unzip "$1" -d "$froot" || return 1
else
unzip "$1"
fi
;;
*.7z)
froot="${fname%.7z}"
if 7z l "$1" | grep -q '^.*/$'; then
checkne "$froot" && mkdir "$froot" &&
7z x "$1" -o"$froot" || return 1
else
7z x "$1"
fi
;;
*)
echo "'$1' cannot be extracted"
;;
esac
}
main "$1"