Extract Compressed Archive
#!/bin/bash
#
# This script extracts archives by file-extension
# to a directory specified in gdialog and
# redirects stdout to a log file.
#
# Distributed under the terms of GNU GPL version 2 or later
#
# By Thor himself
#
# Install in your ~/Nautilus/scripts directory.
# Tested with Nautilus 3.x
# Debug
#set -x
# /usr/bin/unrar
[ -n "`which unrar`" ] && UNRAR="`which unrar` x -kb" || UNRAR=""
[ -n "`which tar`" ] && TAR="`which tar`" || TAR=""
[ -n "`which gunzip`" ] && GUNZIP="`which gunzip`" || GUNZIP=""
[ -n "`which bunzip2`" ] && BUNZIP2="`which bunzip2`" || BUNZIP2=""
[ -n "`which unzip`" ] && UNZIP="`which unzip`" || UNZIP=""
[ -n "`which 7z`" ] && SEVENzip="`which 7z` x" || SEVENzip=""
CURRENT_DIR=$(dirname "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS");
FILE_NAME=$(basename "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS");
FILE_EXT=$(echo "$FILE_NAME" | awk -F. '{s=NF-1; print $NF}');
FILE_EXT2=$(echo "$FILE_NAME" | awk -F. '{s=NF-1; print $s}');
case $FILE_EXT in
rar|RAR)
if [ -z "$UNRAR" ]; then zenity --info --text="UNRAR not found. Cannot continue."; exit 1; fi
BINARY=$UNRAR;
break;
;;
gz|GZ)
if [ "$FILE_EXT2" == "tar" -o "$FILE_EXT2" == "TAR" ]; then
if [ -z "$TAR" ]; then zenity --info --text="TAR not found. Cannot continue."; exit 1; fi
BINARY="$TAR -xvzf"
else
if [ -z "$GUNZIP" ]; then zenity --info --text="GUNZIP not found. Cannot continue."; exit 1; fi
BINARY=$GUNZIP;
fi
break;
;;
bz2|BZ2)
if [ "$FILE_EXT2" == "tar" -o "$FILE_EXT2" == "TAR" ]; then
if [ -z "$TAR" ]; then zenity --info --text="TAR not found. Cannot continue."; exit 1; fi
BINARY="$TAR -xvjf"
else
if [ -z "$BUNZIP2" ]; then zenity --info --text="BUNZIP2 not found. Cannot continue."; exit 1; fi
BINARY=$BUNZIP2;
fi
break;
;;
tar|TAR)
if [ -z "$TAR" ]; then zenity --info --text="TAR not found. Cannot continue."; exit 1; fi
BINARY="$TAR -xvf"
break;
;;
zip|ZIP)
if [ -z "$UNZIP" ]; then zenity --info --text="UNZIP not found. Cannot continue."; exit 1; fi
BINARY=$UNZIP
break;
;;
7z|7Z)
if [ -z "$SEVENzip" ]; then zenity --info --text="7zip not found. Cannot continue."; exit 1; fi
BINARY=$SEVENzip
;;
*)
echo "Unknown file-extension $FILE_EXT";
zenity --info --text="Unknown file-extension $FILE_EXT"
exit 1;
;;
esac
DSTDIR=$(zenity --entry --text="extract file to..." --entry-text="$CURRENT_DIR/$FILE_NAME" 2>&1);
if [ $? -ne 0 ]; then
zenity --info --text="Action abort by User."
exit 2
fi
[ -d "$DSTDIR" ] || mkdir -p "$DSTDIR"
cd "$DSTDIR";
$BINARY "$1" 2>&1 | /usr/bin/tee "$DSTDIR/$FILE_NAME".log | zenity --progress --text="extracting..." --auto-kill --pulsate --auto-close
# --pulsate
if [ -f "$DSTDIR/$FILE_NAME.log" ]; then
zenity --text-info --filename="$DSTDIR/$FILE_NAME".log 2>&1
rm "$DSTDIR/$FILE_NAME".log
fi
exit 0;