Converting RAR to ZIP in Linux

As ever, mostly notes to myself.

RAR is a silly and proprietary format. I prefer free software and I find that ZIP files are smaller and decompress faster. Not everyone agrees, and that's fine.
Assuming you've downloaded a RAR file and want to convert it to ZIP, what's the easiest way?

Install P7Zip

sudo apt-get install p7zip-full p7zip-rar


This basic bash script will

  1. Extract a RAR example.rar file to a temporary directory on a RAM disk.
  2. Recompress to
  3. Delete the temporary directory.


It is based on this conversion from CBR to CBZ.

echo "Converting RARs to ZIPs"

# Separate files using ␜

# Use RAM disk for temporary files.

# Set name for the temp dir. This directory will be created under WORKDIR

# Run using "./ /full/path/to/files/" 
# If no directory is specified, then use the current working directory ("."). 

if test -z $1; then

echo "Using $SOURCEDIR"

# Create an temporary directory to work in.
mkdir $TEMPDIR

# Find all the .rar files in the specified directory.
# Using -iname means it will find .rar .RAR .RaR etc.
# "-printf "%p␜" will cause the file names to be separated by the ␜ symbol, 
# rather than the default newline.

for OLDFILE in `find $SOURCEDIR -iname "*.rar" -printf "%p␜"`; do

   # Get the file name without the extension
   BASENAME=`basename "${OLDFILE%.*}"`

   # Path for the file. The ".zip" file will be moved there.
   DIRNAME=`dirname $OLDFILE`

   # Name of the .zip file

   # Create a temporary folder for unRARed files
   echo "Extracting $OLDFILE"
   mkdir "$BASENAME"
   7z x "$OLDFILE" -O"$BASENAME"
   cd "$BASENAME"

   # Zip the files with maximum compression
   7z a -tzip -mx=9 "$NEWNAME" *
   # Alternative. MUCH SLOWER, but better compression
   # 7z a -mm=Deflate -mfb=258 -mpass=15 -r "$NEWNAME" *

   # Move the new .zip to the directory containing the original ".rar" file

   # Delete the temporary directory
   cd $WORKDIR
   rm -r "$BASENAME"

   # OPTIONAL. Delete the RAR file
   # cd $DIRNAME
   # rm "$OLDFILE"


# Delete the temporary directory
rm -r $TEMPDIR

echo "Conversion Done"

That's it! I suppose it might be nice if there were a simpler way to do it - but this is fairly quick and error-proof.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: