Fish script to convert RAR to ZIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/fish | |
# rar2zip.fish | |
function usage | |
printf " | |
Usage: rar2zip.fish [Only one RAR file] | |
Transforms a single RAR file, passed under cmdline, to an equivalent ZIP file, | |
storing it in the current directory" | |
end | |
# TODO: test for only one directory | |
function cleanup | |
echo "Cleaning $argv.." | |
rm -rfv $argv | |
end | |
trap 'cleanup $TMPDIR' 2 15 | |
if math (count $argv) != 1 > /dev/null | |
usage | |
exit 1 | |
end | |
set CWD (pwd) | |
set TMPDIR (mktemp -d -p /tmp/ rar2zip.XXXXXX) | |
if test "$TMPDIR" = "" | |
echo "Error creating temporary dir!" | |
exit 1 | |
end | |
set rarfile (realpath $argv) | |
set BASENAME (basename $rarfile) | |
set DIRNAME (dirname $rarfile) | |
set FILENAME "$DIRNAME/$BASENAME" | |
set ZIPNAME ""(echo $BASENAME|sed -e's/\.[a-zA-Z0-9]\{1,\}$/\.zip/g')"" | |
if [ (file $FILENAME | grep -c -e'RAR') != 1 ] | |
echo "$FILENAME doesn't appear to be a RAR archive!" | |
cleanup $TMPDIR | |
exit 1 | |
end | |
cd $TMPDIR | |
rm -fr "$BASENAME"-dir | |
mkdir -p "$BASENAME"-dir | |
cd "$BASENAME"-dir | |
unar "$FILENAME" | |
zip -0 -r ../"$ZIPNAME" . | |
cd .. | |
mv $ZIPNAME $CWD | |
cleanup $TMPDIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment