I had trouble finding a built-in solution that requires no additional downloads for an tar extraction progress bar. Since I am using RHEL7 with KDE I found that kdialog has a progress bar.
After enough research I managed to create a script that will automatically extract a tar file (can be inputted as argument or a file selection will appear).
NOTE: This script assumes that the contents of the tar file has a root folder and uses that to clean up files if removed. This can definitely use work, but I have all the main logic for the actual progress bar.
Simply, it checks the tar contents with tar tf TAR_FILE
and counts the number of files. Using that count, it places the output of tar xvf TAR_FILE
onto a file and compares the lines to the first to keep track of progress.
#! /bin/bash
if [ $# -gt 1 ]; then exit 1; fi
if [ $# -eq 1 ]; then
tarfile=$(readlink -f $1)
else
tarfile=$(kdialog --getopenfilename "." )
fi
if [ ! -f "$tarfile" ]; then exit 1; fi
tar_dir=$(dirname $tarfile)
echo "Please Wait. Checking tar file size..."
kdialog --msgbox "Please Wait. Checking Tar File..." &
msgboxpid=$!
echo "MSGBOX PID: $msgboxpid"
project_name=$(tar tf "$tarfile" | awk 'NR==1 {print; exit}')
if test -z $project_name; then
kdialog --error "Invalid Tar File"
exit 1
fi
project_dir="${tar_dir}/${project_name}"
project_dir="${project_dir%/}"
echo "Project Dir: $project_dir"
tar tf $tarfile > expected
msgstatus=$(pid $msgboxpid --no-headers)
if ps -p $msgboxpid >/dev/null; then
kill $msgboxpid
fi
size=$(wc -l < expected)
rm expected
echo "Extracting File. Please wait..."
tar xvf $tarfile > out &
tar_pid=$!
echo "Tar PID: $tar_pid"
progressbar=$(kdialog --progressbar "Untarring.\nYou may Cancel at any time" 100)
qdbus $progressbar showCancelButton true >/dev/null
progress=0
until [ $progress -eq 100 ] ; do
# Check if user closed
if ! qdbus $progressbar Get "" "value" >/dev/null 2>&1
then
echo "ProgressBar Manual Exit"
kill $tar_pid
wait
break
fi
# Check Cancelled
if test "true" = "$(qdbus $progressbar wasCancelled 2>&1)";then
echo "ProgressBar Cancelled"
kill $tar_pid
wait
qdbus $progressbar close >/dev/null
break
fi
current_size=$(wc -l < out)
progress=$(((current_size * 100) / size ))
qdbus $progressbar Set "" "value" $progress >/dev/null 2>&1
done
rm out
if test $progress -ne 100; then
kdialog --msgbox "Extraction Aborted. Cleaning up files..." &
msgpid=$!
echo "Removing Files."
rm -rf $project_dir
echo "Done."
kill $msgpid
exit 1
fi
qdbus $progressbar close >/dev/null