Skip to content

Instantly share code, notes, and snippets.

@roboyoshi
Created March 23, 2019 21:14
Show Gist options
  • Save roboyoshi/590a25f0771c9ba847a50a1cc2f5fcb4 to your computer and use it in GitHub Desktop.
Save roboyoshi/590a25f0771c9ba847a50a1cc2f5fcb4 to your computer and use it in GitHub Desktop.
#!/bin/bash
# --------------------------------------------------------------- #
# Make sure that all files in the archive are extracted correctly
# by comparing the filesize inside the archive with the extracted
# file in the directory.
# --------------------------------------------------------------- #
# TODO: Test with nested files
# --------------------------------------------------------------- #
function unrar_compare_extracted_fsize(){
VERBOSE=0
# Define RarFile
rarfile="${1}"
# Extract all Filenames from rarfile
files_in_rar=$(unrar l "${rarfile}" | grep '\.\..*' | awk '{ print $5 }')
# Extract Size+Filenames from rarfile
files_in_rar_with_size=$(unrar l "${rarfile}" | grep '\.\..*' | awk '{ print $2, $5 }')
# Define Variable for Success and set it to true by default
# If any file is not found or the sizes do not match,
# it should be extracted again.
ALL_OK=TRUE
# Iterate over filenames
while read -r fname; do
# check if a file exists
if [ -f "${fname}" ]; then
# check if filesize matches with the one in the rarfile
fsize=$(stat --printf="%s" ${fname})
rsize=$(echo "${files_in_rar_with_size}" | grep "${fname}" | awk '{ print $1 }')
if [ ${fsize} -eq ${rsize} ]; then
[ VERBOSE ] && echo "Extracted-File-Size of ${fname} is equal to Rared-File-Size [ ${fsize} / ${rsize} ]"
else
ALL_OK=FALSE
fi
else
ALL_OK=FALSE
fi
done <<< "${files_in_rar}"
# Output result
if [ ALL_OK ]; then
[ VERBOSE ] && echo "All extracted files appear to be ok."
return 0
else
[ VERBOSE ] && echo "Some Files are not Extracted or do not match the size."
return 1
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment