2021-11-25 13:08:40 +00:00
|
|
|
#!/bin/sh -e
|
2020-11-16 11:29:31 +00:00
|
|
|
# ex - archive extractor
|
2021-12-14 18:49:33 +00:00
|
|
|
# adapted and improved from the commonly circulating version
|
|
|
|
# detects whether unpacking into a subfolder is sensible
|
|
|
|
# and shows progress indications for some operations
|
2022-06-27 11:03:11 +00:00
|
|
|
# optdepends: rewrite(part of my dotfiles, for unzip line rewriting)
|
2021-02-24 11:16:28 +00:00
|
|
|
for arg do
|
2022-06-13 13:49:14 +00:00
|
|
|
case $arg in
|
|
|
|
(-d) del=$(expr ${del:-0} + 1);;
|
|
|
|
(-*) param="$arg";;
|
|
|
|
(*)
|
2021-11-25 13:08:40 +00:00
|
|
|
if test -r "$arg"; then
|
2022-01-30 21:17:45 +00:00
|
|
|
fullpath="$(realpath "$arg")"
|
2022-01-31 13:39:25 +00:00
|
|
|
name="$(basename "${fullpath%.*}")"
|
2022-01-05 16:53:06 +00:00
|
|
|
namepart="$name.part"
|
2021-12-02 11:50:18 +00:00
|
|
|
(
|
2022-06-27 11:03:11 +00:00
|
|
|
# Create temporary subfolder if current folder contains stuff
|
2021-11-25 13:08:40 +00:00
|
|
|
if test "$(ls -U | wc -l)" -gt 2; then
|
2022-01-05 16:53:06 +00:00
|
|
|
mkdir -p "$namepart"
|
|
|
|
cd "$namepart"
|
2021-11-25 13:08:40 +00:00
|
|
|
fi
|
2021-12-02 11:50:18 +00:00
|
|
|
echo "Extracting $arg into $PWD"
|
2021-11-25 13:08:40 +00:00
|
|
|
case "$arg" in
|
2022-01-30 21:17:45 +00:00
|
|
|
(*.tar.*|*.tar) tar --extract --file "$fullpath";;
|
|
|
|
(*.tbz2) tar xjf "$fullpath" ;;
|
|
|
|
(*.tgz) tar xzf "$fullpath" ;;
|
2022-01-25 12:54:50 +00:00
|
|
|
(*.7z|*.z01|*.zip|*.jar)
|
|
|
|
if which 7z >/dev/null
|
2022-06-13 13:49:14 +00:00
|
|
|
then 7z x $param "$fullpath"
|
2022-07-18 10:56:48 +00:00
|
|
|
else which p7zip >/dev/null &&
|
|
|
|
p7zip --decompress $param "$fullpath" ||
|
|
|
|
unzip "$fullpath" | rewrite
|
2022-01-25 12:54:50 +00:00
|
|
|
fi;;
|
2022-01-30 21:17:45 +00:00
|
|
|
(*.gz) gunzip "$fullpath" ;;
|
|
|
|
(*.bz2) bunzip2 "$fullpath" ;;
|
|
|
|
(*.rar) unrar x "$fullpath" ;;
|
|
|
|
(*.deb) ar x "$fullpath" ;;
|
|
|
|
(*.zst) unzstd "$fullpath" ;;
|
|
|
|
(*.Z) uncompress "$fullpath";;
|
2021-12-02 11:50:18 +00:00
|
|
|
(*) echo "'$arg' cannot be extracted by ex" >&2;;
|
2021-11-25 13:08:40 +00:00
|
|
|
esac
|
2022-04-06 14:54:41 +00:00
|
|
|
# If we created a temporary subfolder, check if it can be eliminated
|
|
|
|
if test "$(basename "$PWD")" = "$namepart"
|
|
|
|
then if test $# -lt 2 -a "$(ls -U | wc -l)" -lt 3
|
2022-06-27 11:03:11 +00:00
|
|
|
then test "$(ls)" && mv -iv * ..
|
2022-02-10 15:02:45 +00:00
|
|
|
cd .. && rm -d "$namepart"
|
2022-06-27 11:03:11 +00:00
|
|
|
else cd .. && mv -iv "$namepart" "$name"
|
2022-01-05 16:53:06 +00:00
|
|
|
fi
|
2022-04-06 14:54:41 +00:00
|
|
|
fi
|
2022-06-13 13:49:14 +00:00
|
|
|
case $del in
|
|
|
|
(2) rm "$arg";;
|
|
|
|
(1) mv -v "$arg" "/tmp";;
|
|
|
|
esac
|
2021-12-02 11:50:18 +00:00
|
|
|
)
|
2021-11-25 13:08:40 +00:00
|
|
|
else
|
|
|
|
echo "'$1' is not a readable file"
|
2022-06-13 13:49:14 +00:00
|
|
|
fi;;
|
|
|
|
esac
|
2021-02-24 11:16:28 +00:00
|
|
|
done
|