Advertisement
pintcat

make-xz - simple frontend for tar & xz

Mar 31st, 2024 (edited)
650
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.99 KB | Software | 0 0
  1. #!/bin/sh
  2. # make-xz v1.11 - simple frontend for tar & xz with a nice progress bar. Usage: make-xz.sh [path to source file or dir] [path to target file]
  3. # Source path must lead to a file or directory. It then decides whether to use tar in addition or not. If no target is given the source path is
  4. # used for the archive. The xz or tar.xz suffixes will be added automatically if not present in the target file name. Use the $COMPRESS variable
  5. # to set compression method and strength. You may use presets from 0 to 9 and add "e" for a more extreme compression or use custom options.
  6.  
  7. . progress.sh
  8.  
  9. ERROR(){
  10.         printf "\033[0;31mError - $1\033[0;32m\n"
  11.         exit 1
  12. }
  13.  
  14. [ -z "$1" ] || [ ! -e "$1" ] && ERROR "Wrong input path or file name or none at all."
  15.  
  16. LOG=/media/ramdisk/log_$(date +%y%m%d%H%M%S%3N)
  17. PROGRESS_MAX1=$(($(du -s -- "$1" | cut -f1)/102))
  18. PROGRESS_MAX2=1000
  19. PROGRESS_R1=" "
  20. PROGRESS_FT=T
  21. PROGRESS_MSG=" Compressed file size compared to original: "
  22. #COMPRESS=" --lzma2=dict=256MiB,lc=0,lp=0,pb=2,mode=normal,nice=273,mf=bt4,depth=512"
  23. COMPRESS=9e
  24. export XZ_DEFAULTS="-T0 -vvcz$COMPRESS"
  25.  
  26. [ -d "$1" ] && SUFFIX=.tar.xz || SUFFIX=.xz
  27. if [ -z "$2" ]; then
  28.     OUT=$(printf "$1" | sed 's:/*$::')$SUFFIX    # remove all trailing slashes & add suffix
  29. else
  30.     FILENAME=${2%*$SUFFIX}
  31.     [ -z ${2#$FILENAME*} ] && OUT="$2"$SUFFIX || OUT="$2"
  32. fi
  33. if [ -e "$OUT" ]; then
  34.     read -r -p "Output file already exists. Overwrite (y/N)? " OPT
  35.     case $OPT in
  36.         y|Y)
  37.             rm -f "$OUT"
  38.             ;;
  39.         *)
  40.             ERROR "Error: Unable to write output file."
  41.             ;;
  42.     esac
  43. fi
  44. printf "Compressing file"
  45. # XZ triggers a SIGALRM every second as an indicator to print its progress output to stdout.
  46. # However, by redirecting XZ's progress output the SIGALRM is no longer sent preventing XZ from emitting any progress info.
  47. # With the following code the SIGALRM is sent "manually" making XZ print its progress again and wherever we want.
  48. {
  49.     if [ $SUFFIX = .xz ]; then
  50.         echo " with XZ..."
  51.         xz "$1" > "$OUT" & XZ_PID=$!
  52.     else
  53.         echo "s with TAR-XZ..."
  54.         tar -JC "$1/.." "$(basename "$1")" -cf "$OUT" &
  55.         while [ -z $XZ_PID ]; do    # wait for XZ's PID to appear in the process list
  56.             sleep 0.2s
  57.             XZ_PID=$(ps -e | grep ' xz' | awk -F' ' '{$1=$1;print $1}')
  58.         done
  59.     fi
  60.     while sleep 0.2s; do
  61.         kill -ALRM "$XZ_PID" || break
  62.     done
  63.     wait "$XZ_PID"
  64. } 2>$LOG &
  65. while ! grep -qs "MiB = " $LOG; do sleep 0.2s; done    # wait for XZ to stream it's output
  66. while [ -n "$(ps -e | grep ' xz')" ]; do
  67.     # The long SED filter chain at the end strips the output down to a clean number by removing all leading zeros
  68.     # and any other unwanted junk while preserving the value if it's just zero.
  69.     PROG=$(tail -n1 $LOG | cut -d"/" -f2 | awk -F 'MiB = ' '{print $1}' | sed 's/[^0-9]*//g;s/^0*\([1-9]\)/\1/;s/^0*$/0/')
  70.     RATIO=$(tail -n1 $LOG | cut -d"=" -f2 | awk -F ' ' '{print $1}' | sed 's/[^0-9]*//g;s/^0*\([1-9]\)/\1/;s/^0*$/0/')
  71.     PROGRESS_BAR $PROG $RATIO
  72.     sleep 0.1s
  73. done
  74. PROGRESS_BAR $PROGRESS_MAX1 $RATIO
  75. rm -f $LOG
  76. printf "\033[4B\n"
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement