Refactor
This commit is contained in:
+31
-8
@@ -10,30 +10,52 @@ echo "Converting RARs to ZIPs"
|
||||
# Use RAM disk for temporary files.
|
||||
WORKDIR="/dev/shm/"
|
||||
|
||||
# Cleanup function
|
||||
cleanup_temp() {
|
||||
if [ -n "$TEMPDIR" ] && [ -d "$TEMPDIR" ]; then
|
||||
rm -r "$TEMPDIR"
|
||||
fi
|
||||
}
|
||||
|
||||
for INFILE in "$@"; do
|
||||
# Absolute path to old file
|
||||
OLDFILE=`realpath "${INFILE}"`
|
||||
OLDFILE=$(realpath "${INFILE}")
|
||||
|
||||
# Get the file name without the extension
|
||||
BASENAME=`basename "${OLDFILE%.*}"`
|
||||
BASENAME=$(basename "${OLDFILE%.*}")
|
||||
|
||||
# Path for the file. The ".zip" file will be written there.
|
||||
DIRNAME=`dirname "$OLDFILE"`
|
||||
DIRNAME=$(dirname "$OLDFILE")
|
||||
|
||||
# Name of the .zip file
|
||||
NEWNAME="${DIRNAME}/$BASENAME.zip"
|
||||
|
||||
if [ ! -e "${NEWNAME}" ]; then
|
||||
# Set name for the temp dir. This directory will be created under WORKDIR
|
||||
TEMPDIR=`mktemp -p "$WORKDIR" -d`
|
||||
TEMPDIR=$(mktemp -p "$WORKDIR" -d)
|
||||
|
||||
# Set up cleanup trap
|
||||
trap cleanup_temp EXIT INT TERM
|
||||
|
||||
# Create a temporary folder for unRARed files
|
||||
echo "Extracting $OLDFILE"
|
||||
|
||||
rar x "$OLDFILE" "${TEMPDIR}/"
|
||||
|
||||
if ! rar x "$OLDFILE" "${TEMPDIR}/"; then
|
||||
echo "Error: Failed to extract $OLDFILE"
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
|
||||
# Zip the files with maximum compression
|
||||
7z a -tzip -mx=9 "$NEWNAME" "${TEMPDIR}/*"
|
||||
pushd "$TEMPDIR" > /dev/null
|
||||
if ! 7z a -tzip -mx=9 "$NEWNAME" ./*; then
|
||||
echo "Error: Failed to create ZIP for $BASENAME"
|
||||
popd > /dev/null
|
||||
cleanup_temp
|
||||
continue
|
||||
fi
|
||||
popd > /dev/null
|
||||
|
||||
# Alternative. MUCH SLOWER, but better compression
|
||||
# 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" *
|
||||
|
||||
@@ -41,7 +63,8 @@ for INFILE in "$@"; do
|
||||
touch -r "$OLDFILE" "$NEWNAME"
|
||||
|
||||
# Delete the temporary directory
|
||||
rm -r "$TEMPDIR"
|
||||
cleanup_temp
|
||||
trap - EXIT INT TERM
|
||||
|
||||
# OPTIONAL. Safe-remove the old file
|
||||
#gio trash "$OLDFILE"
|
||||
|
||||
Reference in New Issue
Block a user