rar2zip.sh

Publicado por Jarno Trulli 10/04/2008

[ Hits: 6.515 ]

Download rar2zip.sh




Este é um script (ainda em construção!) que eu uso para transformar meus arquivos RAR para arquivos ZIP.

Eu o uso por que nem sempre eu tenho como abrir um arquivo RAR. Ele é bastante simples, meio monolítico, mas utilizável. Ainda precisa de umas melhorias, mas é bastante útil.

Usage: rar2zip.sh infile.rar

Dependências: unrar, zip, bash.

  



Esconder código-fonte

#!/bin/sh

#
# rar2zip.sh
# Transforms a single RAR file,
# passed under cmdline,
# to an equivalent ZIP file,
# storing it in the current directory
#

# Typical usage: rar2zip.sh infile.rar
#
#set -ex

function usage
{
    cat <<EOF
Usage: rar2zip.sh infile.rar [-f] [-o outfile.zip]

Modifies a RAR file to a ZIP file

Options:
-f            force continuation (e.g. if RAR file is damaged)
-o outfile    Destination file (defaults to same filename as application, with .zip extension)
--            Break cmdline parsing (useful if the filename starts with -)

EOF
}

function cleanup
{
    echo "Cleaning $1.."
    rm -rfv $1
}

trap 'cleanup $TMPDIR' 2 15

CWD=$(pwd)
TMPDIR=$(mktemp -d -p /tmp/ rar2zip.XXXXXX)
if [ "$TMPDIR" = "" ]
    then
    echo "Error creating temporary dir!"
    exit 1
fi

# Parse cmdline args

while :
do
  case $1 in
      -o)
          OUTFILE=$2
          shift 2
          ;;
      -f)
          FORCE=1
          shift 1
          ;;
      --)
          shift 1
          break
          ;;
      *)
          echo "A fix to it in the future!"
          break
          ;;
  esac
done

if [ "$1" = "" ]
    then
    usage
    cleanup $TMPDIR
    exit 1
fi

DEFAULT_ZIP_OPT="-v -9 -b /tmp/"
ZIP_OPT=${ZIP_OPT:-$DEFAULT_ZIP_OPT}

FILE_NAME=$1
BASE_NAME=$(basename $1)
DIR_NAME=$(dirname $1)

# If the target name doesn't start with /,
# it is NOT the 'absolute' pathname --
# It is needed (or something equivalent)

if [ "$(echo $DIR_NAME|grep -c -e'^/' )" = "0"  ]
    then
    DIR_NAME=${CWD}/${DIR_NAME}
fi

ZIP_NAME=$(echo $BASE_NAME|sed -e's/\.[a-zA-Z0-9]\{1,\}$/\.zip/g')


if [ "$(file $DIR_NAME/$BASE_NAME|grep -c -e'RAR archive')" != "1" ]
    then
    echo "The file $DIR_NAME/$BASE_NAME doesn't appear to be a RAR archive!"
    cleanup $TMPDIR
    exit 1
fi

cd $TMPDIR
rm -fr ${BASE_NAME}-dir
mkdir -p ${BASE_NAME}-dir
cd ${BASE_NAME}-dir

unrar x $DIR_NAME/$BASE_NAME ||
{
    if [ "FORCE" != "yes" ]
        then
        echo "The file $TARGET_NAME/$BASE_NAME appears to be damaged. I will stop now"
        exit 1
    else
        echo "The file $TARGET_NAME/$BASE_NAME appears to be damaged. I will  continue anyway..."
    fi ;
 }

#Zip the directory contents
find . -type f| sort | zip -@ $ZIP_OPT $ZIP_NAME
mv -- $ZIP_NAME $CWD

rm -fr $TMPDIR

Scripts recomendados

sources do ubuntu

Adiciona imagens ao email

Menu no terminal usando Shell Script

Script para atualização de hora

Monitoramento por e-mail e celular


  

Comentários
[1] Comentário enviado por maurixnovatrento em 28/05/2020 - 21:05h


Eu estava precisando.

___________________________________
Conhecimento não se Leva para o Túmulo.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts