-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract.sh
36 lines (32 loc) · 985 Bytes
/
extract.sh
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
#!/usr/bin/env bash
# universal extraction function
extract () {
if [[ ! -f $1 ]]; then
printf '%q is not a valid file\n' "$1"
else
if [[ $1 =~ \.t((ar\.)?(bz2|gz|lz|xz|Z)|b2|bz|ar\.lzma)$ ]]; then
(set -o xtrace; tar xf "$1";)
else
case "$1" in
*.Z) (set -o xtrace; uncompress "$1";);;
*.gz) (set -o xtrace; gunzip "$1";);;
*.bz2) (set -o xtrace; bunzip2 "$1";);;
*.zip) (set -o xtrace; unzip "$1";);;
*.rar) (set -o xtrace; rar x "$1";);;
*.7z) (set -o xtrace; 7z x "$1";);;
*) printf '%q cannot be extracted via extract()\n' "$1";;
esac
fi
fi
}
if iscommand unzip; then
# extract a "split zip" archive (.zip, .z01, .z02, ...)
unzplit() {
file=${1:?input *.zip required}
tmp=$(mktemp --tmpdir unzplit-XXXXX.zip)
# shellcheck disable=SC2064
trap "rm -fv '${tmp:?tempfile}'" EXIT RETURN
zip -s- "$file" --out "$tmp"
unzip "$tmp"
}
fi