ambevar-dotfiles/.scripts/extract

99 lines
1.9 KiB
Bash
Executable File

#!/usr/bin/env zsh
## zsh is needed for the password.
_usage () {
cat<<EOF
Usage: ${1##*/} [OPTIONS] FILES|FOLDERS
Wrapper around various archive extraction tools. The archives are recognized
according to their file extension. Folders will be searched recursively for
archives.
Extract password-protected 7z archives in one run. 7z archives are found in all
subfolders.
If password is provided, it will be used on all archives. Password will not show
up on screen. This requires a shell with 'read -s' support. It will not work for
the Bourne Shell.
WARNING: this script is rather weak, most notably regarding existing files.
Options.
-f: Remove archive if extraction was succesful.
-p: Ask for password.
EOF
}
OPT_DELAFTER=false
PASS=''
while getopts ":hfp" opt; do
case $opt in
h)
_usage "$0"
exit 1 ;;
f)
OPT_DELAFTER=true ;;
p)
echo -n "Password: "
read -s PASS
PASS="-p$PASS" ;;
\?)
_usage "$0"
exit 1 ;;
esac
done
shift $(($OPTIND - 1))
if [ $# -eq 0 ]; then
_usage "$0"
exit 1
fi
command -v atool >/dev/null 2>&1 && echo "You should use atool instead." && echo
_extract () {
echo "[$1]"
case "$1" in
*/*) FOLDER="${1%/*}";;
*) FOLDER="." ;;
esac
case "$1" in
*.tar*|*.tbz2|*.tbz|*.tgz|*.txz) tar xf "$1" -C "$FOLDER" ;;
*.7z) 7z x "$PASS" -o"$FOLDER" "$1";;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1" ;;
*.xz) unxz "$1" ;;
*.exe) cabextract "$1" ;;
*)
case "$(file "$1")" in
*7-zip*) 7z x "$PASS" -o"$FOLDER" "$1" ;;
*gzip*|*bzip2*|*XZ*) tar xf "$1" -C "$FOLDER" ;;
*)
echo "[$1]: unrecognized archive format." ;;
esac
esac
$OPT_DELAFTER && rm -v "$1"
}
for i ; do
if [ -d "$i" ]; then
while IFS= read -r FILE; do
_extract "$FILE"
done <<EOF
$(find "$i" -type f)
EOF
else
_extract "$i"
fi
done