Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Any suggestion of how to convert tens of zip files to rar format? App or command line tool are welcome.

share|improve this question
    
Why do you want to do that? –  lhf Aug 15 '12 at 12:07
    
There is a comic viewer which works well with .rar files but not .zip files. –  ohho Aug 16 '12 at 4:48

2 Answers 2

up vote 3 down vote accepted

What you could do is making a little batch file that would use RAR, a shareware command line utility for mac (I did not find any free rar command line utility, but RAR is available as a trial).

Installing rar command

To install RAR into your terminal, simply copy rar and unrar into your bin folder.

To get access to the bin directory, open Terminal.app and type

open ­/bin

The Windows version of RAR allows to "convert" zip archive into rar archive in tools, but the mac version doesn't seem to have this feature. The solution would be to unzip each of the files into separate folders and then to RAR content of those folders right away.

The Solution

I'm not very familiar (yet) with batch files and terminal but here is my attempt:

#!/bin/bash
# batch file that will convert zip files into rar files
# Require RAR for Mac os x to be placed in bin folder

# Working directory, use ~ for home folder shortcut :)
WorkingDirectory=~/test

# Temp directory that will be used for zip files manipulation
# Will prevent loop from raring other folders ;)
TempDirectory="${WorkingDirectory}"/zipToRarTemp

# Target Directory is where you want the rar files to go after the process
TargetDirectory="${WorkingDirectory}"

# Let's create the directories
mkdir "${TempDirectory}"
mkdir "${TargetDirectory}"

# Will loop into WorkingDirectory and unzip each .zip files
for file in "${WorkingDirectory}"/*.zip
do
    # Get file name
    # See http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash
    # 1st answer
    filename=$(basename "$file")
    extension="${filename##*.}"
    filename="${filename%.*}"

    # Temp folder in the loop
    tempFolderToRar="${TempDirectory}"/"${filename}"

    # Create folders to rar later
    mkdir "${tempFolderToRar}"

    # unzip -d folder/extract/to fileToExtract.zip
    unzip -d "${TempDirectory}"/"${filename}" "${file}"

    # rar all the files in tempFolderToRar into the target
    rar a "${TargetDirectory}"/"${filename}".rar "${tempFolderToRar}" 
done

# Optionnaly, delete temp directory if different from target
if [ "${TempDirectory}" != "${TargetDirectory}" ]
    then
    rm -r "${TempDirectory}"
fi

Save this to a file with no extension, be sure to set the good paths in the first variables and it should work fine running it in terminal.app

Conclusion

Well, It worked for me.

Note: there are a lot of " and it's to make it work with files named with spaces. Maybe there's a better way, but it works ;) Oh and sorry if code quality is bad, I just learned how to make batch files.

Hope it helps.

share|improve this answer
    
If every novice bash programmer would write code of this quality, the world of scripts would be a better place! Well done. –  patrix Aug 15 '12 at 18:50
    
Thanks, that is my first bash script ever ^^. I Wrote alot of comments for learning purpose –  GabLeRoux Aug 15 '12 at 18:53
1  
rm -r ${TempDirectory}, mkdir ${TempDirectory}, and mkdir ${TargetDirectory} should also be double-quoted. You should probably use something like mktemp -td or mkdir /tmp/$(uuidgen) for creating the temporary directory. The files in the rar archives are extracted to $PWD/$TempDirectory. –  ؘؘؘؘ Aug 15 '12 at 19:15

Here's a simpler script like the one posted by GabLeRoux. rar can be downloaded from from http://www.rarlab.com/download.htm.

#!/bin/bash

for f in ~/Desktop/*.zip; do
    d=/tmp/$(uuidgen)
    unzip "$f" -d $d
    cd "$d"
    rm -rf __MACOSX
    ~/bin/rar a "${f%zip}rar" *
    rm -r "$d"
done
share|improve this answer
    
Err, there is a cd .. missing somewhere :-) –  patrix Aug 15 '12 at 18:51
    
What do you mean? The glob expression at the start is ~/Desktop/*.zip just for testing. –  ؘؘؘؘ Aug 15 '12 at 18:58
    
You cd into $d but where do you get out of the directory again? –  patrix Aug 15 '12 at 19:25
1  
The PWD at the end wouldn't matter unless the script does something after converting the archives. It could be kept by running the script in a subshell with ( ) or with something like oldpdw="$PWD" ... cd "$oldpwd". –  ؘؘؘؘ Aug 15 '12 at 19:42
    
Join me in chat.stackexchange.com/rooms/4519/room-for-patrix-and-lri to discuss further –  patrix Aug 15 '12 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.