Skip to content

Instantly share code, notes, and snippets.

@AndersonTorres
Last active July 10, 2020 17:27
Show Gist options
  • Save AndersonTorres/99974eb745a25be5f23ceb54f1d74963 to your computer and use it in GitHub Desktop.
Save AndersonTorres/99974eb745a25be5f23ceb54f1d74963 to your computer and use it in GitHub Desktop.
Fish script to convert RAR to ZIP
#!/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