#!/bin/bash
# This is a script to quickly create encrypted .7z archives
# using the same preset + password every time
#
# Default multi-file handling is files -> single archive
# Use opt '-s' when running to switch to separate mode (one file per archive)
#
# I have no need for multi-part archives so there is NO support
# Do not split archives, make smaller ones that are self contained
# (easier to reup+checksum spoof)
password="Your Archive Password"
#*************************#
# Format Vars
b="$(tput bold)"
r="$(tput sgr0)"
gr="${b}$(tput setaf 121)"
or="${b}$(tput setaf 214)"
yl="${b}$(tput setaf 184)"
cl=$(tput clear; printf '%s\n' "$(tput rev)$(tput setaf 182)${b}make_7zenc.sh v1.0${r}\n\n")
#*************************#
while getopts ":lsp" opt; do
case $opt in
l) lst=1 ;; # list contents existing archive
s) sep=1 ;; # separate archives each file
p) ovr=1 ;; # password override (temporary)
*) echo "error [2]; invalid option"; exit 2 ;;
esac; done; shift "$((OPTIND-1))"
#*************************#
encrypt_sep () {
for file in "${arrFiles[@]}"; do
{
input="$(basename "${file%.*}")"
output="${input//[^[:alnum:]]}"
echo -e "$cl"
echo -e "${yl}${file##*/}${r} ${gr}-->${r} ${or}${output}.7z${r}" >> outputs7zenc_temp.txt
echo -e "${yl}${file##*/}${r} ${gr}-->${r} ${or}${output}.7z${r}"
7z a -t7z -m0=LZMA2 -mmt=on -mx5 -md=32m -mfb=32 -ms=4g \
-mqs=on -sccUTF-8 -p"$password" -mhe=on -bb0 -bse0 -bsp2 \
-w"${PWD}" -snl -mtc=on -mta=on "${PWD}/${output}.7z" "$file" &>/dev/null
}
done
echo -e "$cl" && echo -e "${gr}all files processed${r}\n"
echo -e "${yl}$(cat outputs7zenc_temp.txt)${r}" && sleep 5;
rm -- *_temp.txt && exit 0
}
encrypt_aio () {
read -rp "$(echo -e "${yl}Enter archive name${r}")"$'\n' arcname
[[ $arcname =~ .7z ]] && arcname="${arcname%.*}"
output="${arcname//[^[:alnum:]][[:space:]]}"
7z a -t7z -m0=LZMA2 -mmt=on -mx5 -md=32m -mfb=32 -ms=4g \
-mqs=on -sccUTF-8 -p"$password" -mhe=on -bb0 -bse0 -bsp2 \
-w"${PWD}" -snl -mtc=on -mta=on "${PWD}/${output}.7z" "$@" &>/dev/null
}
list () {
if 7z l -ba -p"$password" "$@" &>/dev/null; then
true
elif 7z l -ba "$@" &>/dev/null; then
password=""
else
read -rp '$\n'"$(echo -e "${yl}Enter archive password${r}\"\n")"$'\n' password
fi
7z l -ba -p"$password" "$@" && exit 0
}
#*************************#
mapfile -t arrFiles < <(printf '%s\n' "$@")
[[ "$ovr" ]] && read -rp '$\n'"$(echo -e "${yl}Enter archive password${r}\"\n")"$'\n' password
[[ "$lst" ]] && list "$@"
[[ "$sep" ]] && encrypt_sep "$@"
encrypt_aio "$@"