ambevar-dotfiles/.scripts/pp7z-extract

80 lines
1.7 KiB
Plaintext
Raw Normal View History

2013-03-12 16:41:47 +01:00
#!/usr/bin/env zsh
2013-07-06 20:51:19 +02:00
_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.
2013-03-12 16:41:47 +01:00
2013-07-06 20:51:19 +02:00
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.
2013-03-12 16:41:47 +01:00
2013-07-06 20:51:19 +02:00
Synopsis.
2013-03-12 16:41:47 +01:00
2013-07-06 20:51:19 +02:00
-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.
2013-03-12 16:41:47 +01:00
EOF
2013-07-06 20:51:19 +02:00
}
OPT_DELAFTER=false
PASS=''
INPUTPATH="$PWD"
OUTPUTPATH="$PWD"
while getopts ":hi:o:fp" opt; do
2013-07-06 20:51:19 +02:00
case $opt in
h) _printhelp "$0"
return 1 ;;
i) INPUTPATH="$OPTARG" ;;
o) OUTPUTPATH="$OPTARG" ;;
f) OPT_DELAFTER=true ;;
2013-07-06 20:51:19 +02:00
p)
echo -n "Password: "
read -s PASS ;;
2013-07-06 20:51:19 +02:00
?)
_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."
2013-03-12 16:41:47 +01:00
exit
fi
2013-07-06 20:51:19 +02:00
while read -r FILE; do
[ -z "$(file "$FILE" | grep '7-zip')" ] && continue
2013-03-12 16:41:47 +01:00
2013-07-06 20:51:19 +02:00
echo "================================================================================"
echo "Decompressing $FILE"
## We remove SOURCEFOLDER from the FILE path.
DESTFOLDER="$(echo "$FILE" | sed "s|$INPUTPATH||")"
2013-07-06 20:51:19 +02:00
DESTFOLDER="$(echo "${DESTFOLDER%/*}")"
mkdir -p "$OUTPUTPATH/$DESTFOLDER"
7z x -p"$PASS" "$FILE" -o"$OUTPUTPATH/$DESTFOLDER" -y
2013-07-06 20:51:19 +02:00
[ $? -eq 0 ] && $OPT_DELAFTER && rm -v "$FILE"
done <<EOF
$(find "$INPUTPATH" -type f)
2013-07-06 20:51:19 +02:00
EOF