Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Fish script to convert RAR to ZIP
#!/bin/fish
# rar2zip.fish
# Transforms a single RAR file,
# passed under cmdline,
# to an equivalent ZIP file,
# storing it in the current directory
#
# Typical usage: rar2zip.fish infile.rar
#
function usage
printf "Usage: rar2zip.fish [Only one RAR file]
Modifies RAR files to ZIP
"
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 $argv
set BASENAME (basename $rarfile)
set DIRNAME (dirname $rarfile)
if [ (echo $DIRNAME| grep -c -e'^/' ) = 0 ]
set DIRNAME "$CWD/$DIRNAME"
end
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
7z x "$FILENAME"
7z a ../"$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
You can’t perform that action at this time.