2013-10-23 14:03:42 +02:00
|
|
|
#!/usr/bin/env zsh
|
|
|
|
## zsh is needed for the password.
|
2013-03-03 11:19:50 +01:00
|
|
|
|
2013-10-23 14:03:42 +02:00
|
|
|
_printhelp ()
|
|
|
|
{
|
2013-10-23 10:47:37 +02:00
|
|
|
cat<<EOF
|
2013-10-23 14:03:42 +02:00
|
|
|
Usage: ${1##*/} [OPTIONS] FILES|FOLDERS
|
2013-03-03 11:19:50 +01:00
|
|
|
|
2013-10-23 10:47:37 +02:00
|
|
|
Wrapper around various archive extraction tools. The archives are recognized
|
2013-10-23 14:03:42 +02:00
|
|
|
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.
|
|
|
|
|
|
|
|
Synopsis.
|
|
|
|
|
|
|
|
-f: Remove archive if extraction was succesful.
|
|
|
|
-p: Ask for password.
|
|
|
|
|
2013-10-23 10:47:37 +02:00
|
|
|
EOF
|
2013-10-23 14:03:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
OPT_DELAFTER=false
|
|
|
|
PASS=''
|
|
|
|
|
|
|
|
while getopts ":hfp" opt; do
|
|
|
|
case $opt in
|
|
|
|
h) _printhelp "$0"
|
|
|
|
exit 1 ;;
|
|
|
|
f) OPT_DELAFTER=true ;;
|
|
|
|
p)
|
|
|
|
echo -n "Password: "
|
|
|
|
read -s PASS
|
|
|
|
PASS="-p$PASS"
|
|
|
|
;;
|
|
|
|
?)
|
|
|
|
_printhelp "$0"
|
|
|
|
exit 1 ;;
|
|
|
|
:) _printhelp "$0"
|
|
|
|
exit 1 ;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
|
|
|
if [ $# -eq 0 ]; then
|
|
|
|
_printhelp "$0"
|
|
|
|
exit 1
|
2013-03-03 11:19:50 +01:00
|
|
|
fi
|
2013-10-23 10:47:37 +02:00
|
|
|
|
2013-10-27 19:50:23 +01:00
|
|
|
command -v atool >/dev/null && echo "You should use atool instead." && echo
|
2013-10-23 10:47:37 +02:00
|
|
|
|
2013-10-23 14:03:42 +02:00
|
|
|
_extract()
|
|
|
|
{
|
|
|
|
echo "[$1]"
|
|
|
|
FOLDER="$(realpath "$1")"
|
|
|
|
FOLDER="${FOLDER%/*}"
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2013-10-23 10:47:37 +02:00
|
|
|
for i ; do
|
2013-10-23 14:03:42 +02:00
|
|
|
|
|
|
|
if [ -d "$i" ]; then
|
2013-10-23 17:44:23 +02:00
|
|
|
while IFS= read -r FILE; do
|
2013-10-23 14:03:42 +02:00
|
|
|
_extract "$FILE"
|
|
|
|
done <<EOF
|
|
|
|
$(find "$i" -type f)
|
|
|
|
EOF
|
2013-10-23 10:47:37 +02:00
|
|
|
else
|
2013-10-23 14:03:42 +02:00
|
|
|
_extract "$i"
|
2013-10-23 10:47:37 +02:00
|
|
|
fi
|
|
|
|
done
|