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
|
|
|
|
2014-11-12 16:15:39 +01:00
|
|
|
_usage () {
|
|
|
|
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.
|
|
|
|
|
2014-01-06 11:24:43 +01:00
|
|
|
Options.
|
2013-10-23 14:03:42 +02:00
|
|
|
|
|
|
|
-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
|
2014-11-12 16:15:39 +01:00
|
|
|
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
|
2013-10-23 14:03:42 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
shift $(($OPTIND - 1))
|
|
|
|
|
|
|
|
if [ $# -eq 0 ]; then
|
2014-11-12 16:15:39 +01:00
|
|
|
_usage "$0"
|
|
|
|
exit 1
|
2013-03-03 11:19:50 +01:00
|
|
|
fi
|
2013-10-23 10:47:37 +02:00
|
|
|
|
2014-01-03 19:13:07 +01:00
|
|
|
command -v atool >/dev/null 2>&1 && echo "You should use atool instead." && echo
|
2013-10-23 10:47:37 +02:00
|
|
|
|
2014-11-12 16:15:39 +01:00
|
|
|
_extract () {
|
|
|
|
echo "[$1]"
|
|
|
|
case "$1" in
|
|
|
|
*/*) FOLDER="${1%/*}";;
|
|
|
|
*) FOLDER="." ;;
|
|
|
|
esac
|
2013-12-27 11:17:55 +01:00
|
|
|
|
2013-10-23 14:03:42 +02:00
|
|
|
case "$1" in
|
2014-11-12 16:15:39 +01:00
|
|
|
*.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" ;;
|
2013-10-23 14:03:42 +02:00
|
|
|
*)
|
2014-11-12 16:15:39 +01:00
|
|
|
echo "[$1]: unrecognized archive format." ;;
|
|
|
|
esac
|
2013-10-23 14:03:42 +02:00
|
|
|
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
|
|
|
|
2014-11-12 16:15:39 +01:00
|
|
|
if [ -d "$i" ]; then
|
|
|
|
while IFS= read -r FILE; do
|
|
|
|
_extract "$FILE"
|
|
|
|
done <<EOF
|
2013-10-23 14:03:42 +02:00
|
|
|
$(find "$i" -type f)
|
|
|
|
EOF
|
2014-11-12 16:15:39 +01:00
|
|
|
else
|
|
|
|
_extract "$i"
|
|
|
|
fi
|
2013-10-23 10:47:37 +02:00
|
|
|
done
|