#!/bin/zsh -f # Copyright 2010 - 2011 by Christian J. Robinson # Distributable under the terms of the GNU GPL, version 3 or later. # # TODO: # - Make the script more generic to convert from and to arbitrary # compression formats # - Check for decompression and recompression errors # # $Id: rar2zip,v 1.2 2011/02/14 02:20:25 infynity Exp $ if [[ $# -le 0 ]] then echo "Usage: ${0##*/} file1.rar [file2.rar ... fileN.rar]" >&2 exit 1 fi whence unrar > /dev/null 2>&1 && rarcmd=unrar whence rar > /dev/null 2>&1 && rarcmd=rar if [[ -z "$rarcmd" ]] then echo "No rar/unrar command found, bye." >&2 exit 1 fi : ${TMPDIR:=/usr/tmp/$LOGNAME} if [[ ! -e $TMPDIR ]] then mkdir $TMPDIR || exit 1 fi temp=$(mktemp -d $TMPDIR/rar2zip$$-XXXXXX) || exit 1 cur=$(pwd) || exit 1 cd $temp || exit 1 if [[ ! -z "$(echo *)" ]] 2>/dev/null then echo "Somehow temporary directory \"$temp\" is not empty! Aborting." >&2 exit 1 fi for i in $* do if [[ ! -r "$cur/$i" ]] then echo "File not found or not readable: $i" >&2 continue fi if [[ "$i" != *.rar && "$i" != *.RAR ]] then echo "Not a rar file: $i" >&2 continue fi newfile=${i%.rar}.zip if [[ -e "$cur/$newfile" ]] then echo "Skipping: $i ($newfile already exists)" >&2 continue fi cp $cur/$i $temp/ # $temp is not provided in this command-line due to oddities with Cygwin + # "native" Windows applications and path delimiters: $rarcmd x $i rm -f $temp/$i zip -r $temp/$newfile * mv $temp/$newfile $cur/ # This is done because this directory is reused for multiple args: rm -rf $temp/* done cd $cur || exit 1 rmdir $temp