Fix extr output location option for gz,xz,zstd,bz2,rar,uncompress. Rework logic to make it easier to add magic support later
This commit is contained in:
parent
e033e0bd99
commit
67d73149d3
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.swp
|
||||||
399
extr
399
extr
@ -13,30 +13,35 @@
|
|||||||
#You should have received a copy of the GNU General Public License
|
#You should have received a copy of the GNU General Public License
|
||||||
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
#along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
#Copyright 2019 rexy712
|
#Copyright 2019-2021 rexy712
|
||||||
|
|
||||||
function extr_usage() {
|
function extr_usage() {
|
||||||
if [ -n "$1" ];then
|
if [ -n "$1" ];then
|
||||||
echo "$1" >&2
|
echo "$1" >&2
|
||||||
fi
|
fi
|
||||||
echo "extr [OPTIONS...] [FILES...]"
|
echo "extr [OPTIONS...] [FILES...]"
|
||||||
echo ""
|
printf "\t%-20s%s\n" "-v, --verbose" "Verbose extraction"
|
||||||
printf "\t%-25s%s\n" "-v, --verbose" "Verbose extraction"
|
printf "\t%-20s%s\n" "-q, --quiet" "Suppress as much output as possible"
|
||||||
printf "\t%-25s%s\n" "-q, --quiet" "Suppress as much output as possible"
|
printf "\t%-20s%s\n" "-f, --force" "Overwrite existing output files"
|
||||||
printf "\t%-25s%s\n" "-f, --force" "Overwrite existing output files"
|
printf "\t%-20s%s\n" "-o, --output" "Output to given directory"
|
||||||
printf "\t%-25s%s\n" "-o, --output" "Output to given directory"
|
printf "\t%-20s%s\n" "-h, --help" "Output this help and exit"
|
||||||
printf "\t%-25s%s\n" "-h, --help" "Output this help and exit"
|
|
||||||
|
|
||||||
printf "\n%s Copyright (C) 2019 rexy712\n" "$0"
|
echo ""
|
||||||
printf "This program comes with ABSOLUTELY NO WARRANTY.\n"
|
echo "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>"
|
||||||
printf "This is free software, and you are welcome to redistribute it\n"
|
echo "This is free software: you are free to change and redistribute it."
|
||||||
printf "under certain conditions; see the GNU GPLv3 for details.\n"
|
echo "There is NO WARRANTY, to the extent permitted by law."
|
||||||
printf "A copy of the GPLv3 is available with the source in the file 'LICENSE'\n"
|
echo "Copyright (C) 2019-2021 rexy712"
|
||||||
}
|
}
|
||||||
function extr_unsupported_file(){
|
function extr_unsupported_file(){
|
||||||
echo "Unsupported format. Ignoring." >&2
|
echo "Unsupported format. Ignoring." >&2
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
function check_output_dir_valid(){
|
||||||
|
local dir="$1"
|
||||||
|
[ -d "${dir}" ] && return 0
|
||||||
|
[ -e "${dir}" ] && return 1
|
||||||
|
mkdir -p "${dir}" || return 1
|
||||||
|
}
|
||||||
function extr_handle_arguments(){
|
function extr_handle_arguments(){
|
||||||
local pass_through=
|
local pass_through=
|
||||||
local -n verb=$1
|
local -n verb=$1
|
||||||
@ -67,6 +72,10 @@ function extr_handle_arguments(){
|
|||||||
extr_usage "Missing argument to $1"
|
extr_usage "Missing argument to $1"
|
||||||
fi
|
fi
|
||||||
outl="$2"
|
outl="$2"
|
||||||
|
if ! check_output_dir_valid "$outl";then
|
||||||
|
should_q=1
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
shift
|
shift
|
||||||
;;
|
;;
|
||||||
-h|--help)
|
-h|--help)
|
||||||
@ -111,6 +120,206 @@ function fatal_error(){
|
|||||||
exit 1
|
exit 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function do_tar(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 1 ];then
|
||||||
|
#flags+=("--checkpoint")
|
||||||
|
:;
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("--verbose")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("--overwrite")
|
||||||
|
else
|
||||||
|
flags+=("--keep-old-files")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
flags+=("-C" "$outloc")
|
||||||
|
fi
|
||||||
|
tar "${flags[@]}" -xf "$file" "${passthrough_args[@]}"
|
||||||
|
}
|
||||||
|
function do_bz2(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
flags+=("--quiet")
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("--verbose")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("--force")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-4}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}"
|
||||||
|
else
|
||||||
|
bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_gz(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
flags+=("--quiet")
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("--verbose")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("--force")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-3}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}"
|
||||||
|
else
|
||||||
|
gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_zip(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" -lt "2" ];then
|
||||||
|
flags+=("-q")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("-o")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
flags+=("-d" "$outloc")
|
||||||
|
fi
|
||||||
|
unzip "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
}
|
||||||
|
function do_uncompress(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("-v")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("-f")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-2}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
uncompress "${flags[@]}" "${passthrough_args[@]}" "$file" -c > "${fileout}"
|
||||||
|
else
|
||||||
|
uncompress "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_7zip(){
|
||||||
|
local flags=()
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
flags+=("-o${outloc}")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("-y")
|
||||||
|
fi
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
7za x "${flags[@]}" "${passthrough_args[@]}" "$file" >/dev/null 2>&1
|
||||||
|
else
|
||||||
|
7za x "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_rar(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
flags+=("-inul")
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("-ierr")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("-o+")
|
||||||
|
else
|
||||||
|
flags+=("-o-")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-4}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
unrar "${flags[@]}" p "${passthrough_args[@]}" "$file" > "${fileout}"
|
||||||
|
else
|
||||||
|
unrar "${flags[@]}" x "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_zstd(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
flags+=("--quiet")
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("-v")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("-f")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-5}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}"
|
||||||
|
else
|
||||||
|
unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
function do_xz(){
|
||||||
|
local flags=()
|
||||||
|
if [ "$verbosity" == 0 ];then
|
||||||
|
flags+=("--quiet")
|
||||||
|
elif [ "$verbosity" == 2 ];then
|
||||||
|
flags+=("--verbose")
|
||||||
|
fi
|
||||||
|
if [ -n "$overwrite" ];then
|
||||||
|
flags+=("--force")
|
||||||
|
fi
|
||||||
|
if [ -n "$outloc" ];then
|
||||||
|
local fileout="${outloc}/$(basename "${file:0:-3}")"
|
||||||
|
if [ -e "${fileout}" ] && [ -z "$overwrite" ];then
|
||||||
|
echo "Could not decompress $file; output file exists" >&2
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
xz -dk "${flags[@]}" "${passthrough_args[@]}" "$file" --stdout > "${fileout}"
|
||||||
|
else
|
||||||
|
xz -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
function do_file_extension(){
|
||||||
|
local file="$1"
|
||||||
|
case "$file" in
|
||||||
|
*.tar.*|*.tbz2|*.tgz|*.txz|*.tar|*.tarZ|*.targz|*.tarxz|*.tzst)
|
||||||
|
do_tar;;
|
||||||
|
*.bz2)
|
||||||
|
do_bz2;;
|
||||||
|
*.gz)
|
||||||
|
do_gz;;
|
||||||
|
*.zip)
|
||||||
|
do_zip;;
|
||||||
|
*.Z)
|
||||||
|
do_uncompress;;
|
||||||
|
*.7z)
|
||||||
|
do_7zip;;
|
||||||
|
*.rar)
|
||||||
|
do_rar;;
|
||||||
|
*.zst)
|
||||||
|
do_zstd;;
|
||||||
|
*.xz)
|
||||||
|
do_xz;;
|
||||||
|
*)
|
||||||
|
extr_unsupported_file || return -1;;
|
||||||
|
esac
|
||||||
|
retval=$?
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
function _extr(){
|
function _extr(){
|
||||||
local verbosity=
|
local verbosity=
|
||||||
local overwrite=
|
local overwrite=
|
||||||
@ -128,169 +337,8 @@ function _extr(){
|
|||||||
fi
|
fi
|
||||||
for file in "${filenames[@]}";do
|
for file in "${filenames[@]}";do
|
||||||
if [ -f "$file" ];then
|
if [ -f "$file" ];then
|
||||||
case "$file" in
|
do_file_extension "$file"
|
||||||
*.tar.*|*.tbz2|*.tgz|*.txz|*.tar|*.tarZ|*.targz|*.tarxz|*.tzst)
|
[ $? -lt 0 ] && exit "$ret"
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 1 ];then
|
|
||||||
#flags+=("--checkpoint")
|
|
||||||
:;
|
|
||||||
elif [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("--verbose")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("--overwrite")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
flags+=("-C" "$outloc")
|
|
||||||
fi
|
|
||||||
tar "${flags[@]}" -xf "$file" "${passthrough_args[@]}"
|
|
||||||
retval=$?;;
|
|
||||||
*.bz2)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 0 ];then
|
|
||||||
flags+=("--quiet")
|
|
||||||
elif [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("--verbose")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("--force")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
if [ ! "${file:0:1}" == "/" ];then
|
|
||||||
file="$PWD/$file"
|
|
||||||
fi
|
|
||||||
push_dir "$outloc" || continue
|
|
||||||
bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
pop_dir || fatal_error
|
|
||||||
else
|
|
||||||
bunzip2 -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
*.gz)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 0 ];then
|
|
||||||
flags+=("--quiet")
|
|
||||||
elif [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("--verbose")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("--force")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
if [ ! "${file:0:1}" == "/" ];then
|
|
||||||
file="$PWD/$file"
|
|
||||||
fi
|
|
||||||
push_dir "$outloc" || continue
|
|
||||||
gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
popd || fatal_error
|
|
||||||
else
|
|
||||||
gunzip -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
*.zip)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" -lt "2" ];then
|
|
||||||
flags+=("-q")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("-o")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
flags+=("-d" "$outloc")
|
|
||||||
fi
|
|
||||||
unzip "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?;;
|
|
||||||
*.Z)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("-v")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("-f")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
if [ ! "${file:0:1}" == "/" ];then
|
|
||||||
file="$PWD/$file"
|
|
||||||
fi
|
|
||||||
push_dir "$outloc" || continue
|
|
||||||
uncompress "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
pop_dir || fatal_error
|
|
||||||
else
|
|
||||||
uncompress "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
*.7z)
|
|
||||||
local flags=()
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
flags+=("-o${outloc}")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("-y")
|
|
||||||
fi
|
|
||||||
if [ "$verbosity" == 0 ];then
|
|
||||||
7za x "${flags[@]}" "${passthrough_args[@]}" "$file" >/dev/null 2>&1
|
|
||||||
else
|
|
||||||
7za x "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
fi
|
|
||||||
retval=$?
|
|
||||||
;;
|
|
||||||
*.rar)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 0 ];then
|
|
||||||
flags+=("-inul")
|
|
||||||
elif [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("-ierr")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("-o+")
|
|
||||||
else
|
|
||||||
flags+=("-o-")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
if [ ! "${file:0:1}" == "/" ];then
|
|
||||||
file="$PWD/$file"
|
|
||||||
fi
|
|
||||||
push_dir "$outloc" || continue
|
|
||||||
unrar "${flags[@]}" x "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
pop_dir || fatal_error
|
|
||||||
else
|
|
||||||
unrar "${flags[@]}" x "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
*.zst)
|
|
||||||
local flags=()
|
|
||||||
if [ "$verbosity" == 0 ];then
|
|
||||||
flags+=("--quiet")
|
|
||||||
elif [ "$verbosity" == 2 ];then
|
|
||||||
flags+=("-v")
|
|
||||||
fi
|
|
||||||
if [ -n "$overwrite" ];then
|
|
||||||
flags+=("-f")
|
|
||||||
fi
|
|
||||||
if [ -n "$outloc" ];then
|
|
||||||
if [ ! "${file:0:1}" == "/" ];then
|
|
||||||
file="$PWD/$file"
|
|
||||||
fi
|
|
||||||
push_dir "$outloc" || continue
|
|
||||||
unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
pop_dir || fatal_error
|
|
||||||
else
|
|
||||||
unzstd -dk "${flags[@]}" "${passthrough_args[@]}" "$file"
|
|
||||||
retval=$?
|
|
||||||
fi
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
_extr_unsupported_file || exit "$?";;
|
|
||||||
esac
|
|
||||||
elif [[ -d "$file" ]];then
|
elif [[ -d "$file" ]];then
|
||||||
echo "Cannot extract a directory: '$file'" >&2
|
echo "Cannot extract a directory: '$file'" >&2
|
||||||
retval=2
|
retval=2
|
||||||
@ -298,7 +346,6 @@ function _extr(){
|
|||||||
echo "No such file or directory: '$file'" >&2
|
echo "No such file or directory: '$file'" >&2
|
||||||
retval=3
|
retval=3
|
||||||
fi
|
fi
|
||||||
shift
|
|
||||||
done
|
done
|
||||||
return $retval
|
return $retval
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user