ambevar-dotfiles/.scripts/pp7z-extract

80 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env zsh
_printhelp ()
{
cat<<EOF
Usage: ${1##*/} [-p] [-f] [-i INPUTPATH] [-o OUTPUTPATH]
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.
Synopsis.
-i: Specify input folder (default to PWD).
-f: Remove archive if extraction was succesful.
-o: Specify ouput folder (default to PWD).
-p: Ask for password.
EOF
}
OPT_DELAFTER=false
PASS=''
INPUTPATH="$PWD"
OUTPUTPATH="$PWD"
while getopts ":hi:o:fp" opt; do
case $opt in
h) _printhelp "$0"
return 1 ;;
i) INPUTPATH="$OPTARG" ;;
o) OUTPUTPATH="$OPTARG" ;;
f) OPT_DELAFTER=true ;;
p)
echo -n "Password: "
read -s PASS ;;
?)
_printhelp "$0"
return 1
;;
:)
_printhelp "$0"
return 1
;;
esac
done
shift $(($OPTIND - 1))
if [ $# -ne 0 ]; then
_printhelp "$0"
return 1
fi
if [ -z "$(command -v 7z)" ]; then
echo "7z needed."
exit
fi
while read -r FILE; do
[ -z "$(file "$FILE" | grep '7-zip')" ] && continue
echo "================================================================================"
echo "Decompressing $FILE"
## We remove SOURCEFOLDER from the FILE path.
DESTFOLDER="$(echo "$FILE" | sed "s|$INPUTPATH||")"
DESTFOLDER="$(echo "${DESTFOLDER%/*}")"
mkdir -p "$OUTPUTPATH/$DESTFOLDER"
7z x -p"$PASS" "$FILE" -o"$OUTPUTPATH/$DESTFOLDER" -y
[ $? -eq 0 ] && $OPT_DELAFTER && rm -v "$FILE"
done <<EOF
$(find "$INPUTPATH" -type f)
EOF