Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/sh
- # make-7z v1.16 - frontend for 7-Zip with a nice progress bar. Usage: make-7z.sh [path to source file or dir] [path to target file]
- # Source path must lead to a file or directory. If no target is given the source path and name is used for the archive.
- # The 7z suffix will be added automatically if not present in the target file name. The following variables can be used to override
- # the default settings: $PS for compression preset (0-9; default: 9), $DS for dictionary size (up to 3840; determined automatically
- # by default), $FB for fast bytes aka word size (up to 273; default: 273), $MT number of CPU threads (default: 3).
- ERROR(){ # Print error message & exit with return code 1.
- printf "\033[0;31mError - $1\033[0;32m"
- exit 1
- }
- # Check dependencies: progress.sh, 7z (progress.sh is no longer mandatory & ignored if not present)
- touch ${LOG=/media/ramdisk/log_$(date +%y%m%d%H%M%S%3N)}
- PROGRESS=$(type progress.sh) && PROGRESS="/"${PROGRESS##* /}
- [ -f "$PROGRESS" ] || PROGRESS=$LOG
- SZIP=$(type 7z) && SZIP="/"${SZIP##* /}
- [ -x "$SZIP" ] || ERROR "7z is either missing or not executable\n"
- . "$PROGRESS"
- PROGRESS_R1=" "
- PROGRESS_FT=T
- PROGRESS_MSG=" Compressed file size compared to original: "
- # Check source & target path & file name
- [ -z "$1" ] || [ ! -e "$1" ] && ERROR "Wrong input path or file name or none at all.\n"
- if [ -z "$2" ]; then
- OUT=$(printf "$1" | sed 's:/*$::').7z # remove all trailing slashes & add suffix
- else
- FILENAME=${2%*.7z}
- [ -z ${2#$FILENAME*} ] && OUT="$2".7z || OUT="$2"
- fi
- if [ -e "$OUT" ]; then
- read -r -p "Output file already exists. Overwrite (y/N)? " OPT
- case $OPT in
- y|Y)
- rm -f "$OUT";;
- *)
- ERROR "Unable to write output file.\n";;
- esac
- fi
- # Calculate optimal dictionary size according to available RAM & the amount of data to compress
- DICT=64
- DSTEP=$(($DICT/2))
- [ $((PROGRESS_MAX2=$(du -s "$1" | cut -f1))) -lt 3932160 ] && DMAX=$((($PROGRESS_MAX2+511)/1024)) || DMAX=3840
- while [ $(((($DICT+$DSTEP)*1088+56)/100)) -le $(($(awk '/lable:/ {printf $2}' /proc/meminfo)/1024)) ] && [ $DICT -lt $DMAX ]; do
- [ $((DICT=$DICT+$DSTEP)) -gt 3840 ] && DICT=3840
- [ $(($DSTEP*4)) -eq $DICT ] && DSTEP=$(($DSTEP*2))
- done
- # Check cpression settings (can be set manually by exporting the corresponding variables)
- [ -z $PS ] || [ -z "${PS##*[!0-9]*}" ] || [ $PS -gt 9 ] && PS=9
- [ -z $DS ] || [ -z "${DS##*[!0-9]*}" ] || [ $DS -gt $DICT ] && DS=$DICT
- [ -z $FB ] || [ -z "${FB##*[!0-9]*}" ] || [ $FB -gt 273 ] && FB=273
- CCORES=$(grep -m1 "siblings" /proc/cpuinfo | cut -d" " -f2)
- if [ -n "$MT" ] && [ -n "${MT##*[!0-9]*}" ] && [ $MT -gt 0 ]; then
- [ $MT -gt $CCORES ] && MT=$CCORES
- else
- [ $CCORES -gt 3 ] && MT=3 || MT=$CCORES
- fi
- COMPRESS="-mx=$PS -md="$DS"m -mfb=$FB -mmt=$MT"
- # Start compression
- if [ -z "$(command -V PROGRESS_BAR | grep function)" ]; then
- $SZIP a "$OUT" "$1" $COMPRESS -snl
- else
- exec $SZIP a "$OUT" "$1" $COMPRESS -snl -bso1 -bsp1 >$LOG &
- while [ -z "$(grep 7-Zip $LOG)" ]; do # wait for 7-Zip to open its log
- sleep 0.2s
- done
- tail -n +2 $LOG
- printf "\033[\rAPreset: $PS, dictionary size: "$DS"mb, word size: $FB, number of CPU threads: $MT\n"
- while [ -n "$(ps -e | grep ' 7z')" ]; do
- sleep 0.1s
- PROG=$(tr -d '\b\n' <$LOG | awk -F'% \\+ |% [0-9]+ \\+ ' '{print $(NF-1)}' | awk -F" " '{printf $NF}')
- if [ -n "${PROG##*[!0-9]*}" ]; then
- [ $PROG -eq 0 ] && DIV=1 || DIV=$PROG
- RATIO=$((($(du -s "$OUT" | cut -f1)*100+$DIV*10/18)/$DIV))
- if [ $((COUNT=$COUNT+1)) -le 5 ]; then
- RATIO_AVG=$(($RATIO_AVG+$RATIO))
- PROGRESS_BAR $PROG $(($RATIO_RES+0))
- else
- RATIO_RES=$((($RATIO_AVG*10+28)/50))
- [ $RATIO_RES -gt $PROGRESS_MAX2 ] && RATIO_AVG=$PROGRESS_MAX
- COUNT=1
- RATIO_AVG=$RATIO_RES
- PROGRESS_BAR $PROG $RATIO_RES
- fi
- fi
- done
- PROGRESS_BAR 100 $(du -s "$OUT" | cut -f1)
- printf "\033[4B\n"
- fi
- rm -f $LOG
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement