Skip to content

Instantly share code, notes, and snippets.

@syuu1228
Forked from ferdnyc/rar2zip.sh
Last active August 12, 2020 08:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save syuu1228/06c9fce304d2667b17a4174792987856 to your computer and use it in GitHub Desktop.
Save syuu1228/06c9fce304d2667b17a4174792987856 to your computer and use it in GitHub Desktop.
Conversion from rar to zip (ported to macos)
#!/bin/bash
#
# rar2zip conversion script
# Based on: https://shkspr.mobi/blog/2016/12/converting-rar-to-zip-in-linux/
#
# Usage: rar2zip.sh file [file ...]
echo "Converting RARs to ZIPs"
# Use /tmp for temporary files.
WORKDIR="/tmp/"
for INFILE in "$@"; do
# Absolute path to old file
OLDFILE=`realpath "${INFILE}"`
# Get the file name without the extension
BASENAME=`basename "${OLDFILE%.*}"`
# Path for the file. The ".zip" file will be written there.
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=`gmktemp -p "$WORKDIR" -d`
# Create a temporary folder for unRARed files
echo "Extracting $OLDFILE"
unrar x "$OLDFILE" "${TEMPDIR}/"
# Zip the files with maximum compression
7z a -tzip -mx=9 "$NEWNAME" "${TEMPDIR}/*"
# Alternative. MUCH SLOWER, but better compression
# 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" *
# Preserve file modification time
touch -r "$OLDFILE" "$NEWNAME"
# Delete the temporary directory
rm -r "$TEMPDIR"
# OPTIONAL. Safe-remove the old file
# gio trash "$OLDFILE"
else
echo "${NEWNAME}: File exists!"
fi
done
echo "Conversion Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment