1
0
mirror of https://gitlab.com/JKANetwork/jka-toolkit.git synced 2026-02-15 17:41:31 +01:00
Files
jka-toolkit/jka-toolkit/jkazip
2016-09-16 11:28:11 +02:00

216 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
#JKA - ZIP
#A script for de/compress files and folders in many formats
#Author: JKA Network - contacto@jkanetwork.com
# gettext initialization
export TEXTDOMAIN='jkazip'
export TEXTDOMAINDIR='/usr/share/locale'
#function sacaFormato, get the file extension to zip or unzip
function sacaFormato() {
local var=$1
cant=$(echo $var | grep -o "\." | wc -l)
if [ $cant -eq 0 ];then
echo "$(gettext "File extension haven't been specified")" > /dev/stderr
echo > /dev/stderr
exit
fi
fieldPost=$(expr $cant + 1)
postform=$(echo $var | cut -f$fieldPost -d".")
preform=$(echo $var | cut -f$cant -d".")
if [[ $preform = "tar" ]];then
formato=$(echo ${preform}.${postform})
else
formato=$postform
fi
}
#Function msg, print in stdout a final message
function msg() {
local var="$1"
local var2="$2"
printf -- "$(gettext "File/s %s has/have been compressed in %s")" "$var" "$var2"
echo
}
#Function msg2, print in stdout a final message
function msg2() {
local var="$1"
printf -- "$(gettext "File %s has been decompressed")" "$var"
echo
}
version="2.3.1"
if [ -z "$1" ];then
echo "$(gettext "No option specified, use jkazip -h")" > /dev/stderr
elif [ "$1" = "-h" ];then
echo "$(gettext " synopsis: jkazip <options> <file or directory>")"
echo ""
echo "$(gettext " -d, decompress file")"
echo "$(gettext " -c <output file>, compress file")"
echo "$(gettext " -l, list all files")"
echo "$(gettext " -h, show help box and exit")"
echo "$(gettext " -v, show version and exit")"
echo ""
echo "$(gettext " The order must be \"jkazip -d <file>\" or \"jkazip -c <output> <files or directories>\"")"
echo ""
echo "$(gettext " Examples")"
echo "$(gettext " jkazip -d file.7z")"
echo "$(gettext " jkazip -c ouputfile.tar.gz directory")"
echo ""
echo "$(gettext " Supported formats: tar, gzip, bzip2, xzip, lzip, 7z, rar (only unzip) and zip")"
echo ""
elif [ "$1" = "-v" ];then
printf -- "$(gettext "jkazip by JKA Network; version %s")" "$version"
echo
elif [ "$1" = "-d" ];then
if [ -z "$2" ];then
echo "$(gettext "No file or directory specified")" > /dev/stderr
exit
elif [ -f "$2" ];then
printf -- "$(gettext "File %s will be decompressed")" "$2"
echo
else
echo "$(gettext "%s no such file or directory")" "$2" > /dev/stderr
echo
exit
fi
entrada="$2"
sacaFormato "$entrada"
case $formato in
"tar" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "tgz" | "tbz2" | "txz" | "tlz")
tar xvf "$entrada"
msg2 "$entrada"
;;
"7z")
7z x "$entrada"
msg2 "$entrada"
;;
"zip")
unzip "$entrada"
msg2 "$entrada"
;;
"gz" | "lz")
formato="${formato}ip"
$formato -dk "$entrada"
msg2 "$entrada"
;;
"xz")
$formato -dk "$entrada"
msg2 "$entrada"
;;
"bz2")
bzip2 -dk "$entrada"
msg2 "$entrada"
;;
"rar")
unrar x "$entrada"
msg2 "$entrada"
;;
*)
printf -- "$(gettext "File extension %s are not supported")" $formato > /dev/stderr
echo > /dev/stderr
;;
esac
elif [ "$1" = "-c" ];then
salida="$2"
if [ -z "$3" ];then
echo "$(gettext "No file or directory specified")"
exit
fi
# TODO: Hacer que esto funcione
### Funcion nueva
for compfile in "$@"; do
if [[ "$compfile" != "$1" && "$compfile" != "$2" ]];then
if [[ ! -d "$compfile" && ! -f "$compfile" ]];then
printf -- "$(gettext "%s no such file or directory")" "$compfile"
echo
#exit
fi
fi
done
### Fin funcion nueva del tod
#Funcion files nueva
for compfile in "$@"; do
if [[ "$compfile" != "$1" && "$compfile" != "$2" ]];then
files="$files $compfile"
fi
done
#Fin funcion files nueva
## files anterior
# files="$3"
# for (( i=4; i<=$#; i++))
# do
# files="$files $(echo $* | cut -f$i -d" ")"
# done
sacaFormato "$salida"
case $formato in
"tar" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "tgz" | "tbz2" | "txz" | "tlz")
tar cvfa "$salida" $files
msg "$files" "$salida"
;;
"7z")
7z a "$salida" $files
msg "$files" "$salida"
;;
"zip")
zip -r "$salida" $files
msg "$files" "$salida"
;;
"gz" | "lz")
formato="${formato}ip"
"$formato" -9kc "$3" > "$salida"
msg "$3" "$salida"
;;
"xz")
"$formato" -9kc "$3" > "$salida"
msg "$3" "$salida"
;;
"bz2")
"$formato" -9kc "$3" > "$salida"
msg "$3" "$salida"
;;
"rar")
echo "$(gettext "rar compression are not supported")" > /dev/stderr
;;
*)
printf -- "$(gettext "File extension %s are not supported")" $formato > /dev/stderr
echo > /dev/stderr
;;
esac
elif [ "$1" = "-l" ];then
if [ -z "$2" ];then
echo "$(gettext "No file or directory specified")" > /dev/stderr
exit
fi
sacaFormato $2
case $formato in
"tar" | "tar.xz" | "tar.lz" | "tar.gz" | "tar.bz2" | "tgz" | "tbz2" | "txz" | "tlz")
tar taf "$2"
;;
"7z")
7z l "$2"
;;
"zip")
uzip -v "$2"
"gz" )
gzip -l "$2"
;;
"xz")
xz -l "$2"
;;
"bz2" | "lz" )
echo "Not supported for listing" > /dev/stderr
;;
"rar")
unrar v "$2"
;;
*)
printf -- "$(gettext "File extension %s are not supported")" $formato > /dev/stderr
echo > /dev/stderr
;;
esac
else
echo "$(gettext "Unknown option")" > /dev/stderr
fi