utaz finally work with many formats, with optimizations

This commit is contained in:
fatalerrors
2026-03-05 10:24:25 +01:00
parent ffee8c2e47
commit a068d57ba5
3 changed files with 97 additions and 27 deletions

View File

@@ -39,8 +39,47 @@
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
utaz() utaz()
{ {
_ununzip()
{
unzip -o "$1" -d "$2" >/dev/null 2>&1
}
_untar()
{
tar -xf "$1" -C "$2"
}
_ungzip()
{
tar -xzf "$1" -C "$2"
}
_unbzip2()
{
tar -xjf "$1" -C "$2"
}
_unxz()
{
tar -xJf "$1" -C "$2"
}
_unlzop()
{
lzop -d "$1" -o "$2/$(basename "${1%.*}")"
}
_unlzip()
{
if command -v plzip >/dev/null 2>&1; then
plzip -d -c "$1" > "$2/$(basename "${1%.*}")"
else
lzip -d -c "$1" > "$2/$(basename "${1%.*}")"
fi
}
for opt in $@; do for opt in $@; do
case $opt in case ${opt} in
"-h" | "--help") "-h" | "--help")
echo "utaz: uncompress all the given files and/or the ones found in the given" echo "utaz: uncompress all the given files and/or the ones found in the given"
echo " directories creating an host directory where needed." echo " directories creating an host directory where needed."
@@ -76,62 +115,92 @@ utaz()
*) *)
# The ${opt%/} writing is to remove trailing / if any # The ${opt%/} writing is to remove trailing / if any
local LIST="$LIST ${opt%/}" local LIST="${LIST} ${opt%/}"
;; ;;
esac esac
done done
[[ $createdir && $nodir ]] && \ [[ -n ${createdir} && -n ${nodir} ]] && \
disp E "The --create-dir and --no-dir options are mutually exclusive." disp E "The --create-dir and --no-dir options are mutually exclusive."
[[ ! $LIST ]] && local LIST="." [[ -z ${LIST} ]] && local LIST="."
for zitem in ${LIST}; do
for zitem in $LIST; do for f in "${zitem}"/*; do
shopt -s nullglob local dir="${f%.*}"
local zips=("$zitem"/*.zip) local extractor=""
shopt -u nullglob case "$f" in
[[ ${#zips[@]} -eq 0 ]] && \ *.zip)
disp W "$zitem contains no supported archive file, skipping." && \ extractor="_ununzip"
;;
*.tar.gz|*.tgz)
extractor="_ungzip"
;;
*.tar.bz2)
extractor="_unbzip2"
;;
*.tar.xz)
extractor="_unxz"
;;
*.tar.lz)
extractor="_unlzop"
;;
*.tar)
extractor="_untar"
;;
*)
disp I "File ${f} is not a supported archive, skipping."
continue continue
;; # Skip non-archive files
esac
for f in "$zitem"/*.zip; do # Verify binary existence
disp I "Processing archive $f... " local cmd=${extractor//_un/}
local dir=${f::-4} command -v ${cmd} >/dev/null 2>&1 || {
disp E "Binary ${cmd} necessary to extract ${f} is missing."
continue
}
mkdir -p "$dir" disp I "Processing archive ${f} with ${extractor}..."
mkdir -p "${dir}"
[[ $? -gt 0 ]] && [[ $? -gt 0 ]] &&
disp E "The filesystem can't create directories, exit!" && disp E "The filesystem can't create directories, exit!" &&
return 1 return 1
unzip -o "$f" -d "$dir" >/dev/null 2>&1 ${extractor} "${f}" "${dir}"
case $? in case $? in
0) 0)
[[ $willrm ]] && rm -f "$f" && disp I "File $zitem/$f deleted." [[ -n ${willrm} ]] &&
rm -f "${f}" && disp I "File ${zitem}/${f} deleted."
;; ;;
1) 1)
disp W "Compression program returned a warning: deletion canceled." disp W "Compression program returned a warning: deletion canceled."
;; ;;
*) *)
disp E "The zip file seems corrupted, failed." disp E "The zip file seems corrupted, failed."
rm -rf "$dir" >/dev/null 2>&1 rm -rf "${dir}" >/dev/null 2>&1
continue continue
;; ;;
esac esac
if [[ $createdir ]]; then if [[ -n ${createdir} ]]; then
disp I "Archive extracted successfully in subdirectory." disp I "Archive extracted successfully in subdirectory."
elif [[ $nodir ]]; then elif [[ -n ${nodir} ]]; then
mv "./$dir/"* ./ && rmdir "$dir" mv "./${dir}/"* ./ && rmdir "${dir}"
disp I "Archive extracted successfully, no subdirectory needed." disp I "Archive extracted successfully, no subdirectory needed."
else else
subdirs=$(find $dir -maxdepth 1 | wc -l) # Set nullglob to ensure the array is empty if no files match
if [[ $subdirs -eq 2 ]]; then shopt -s nullglob
mv ./$dir/* ./ && rmdir $dir local contents=( "${dir}"/* )
# Check if exactly one item exists and if that item is a directory
if [[ ${#contents[@]} -eq 1 ]] && [[ -d "${contents[0]}" ]]; then
# Single directory detected
mv "${contents[0]}"/* ./ && rmdir "${dir}"
disp I "Archive extracted successfully, no subdirectory needed." disp I "Archive extracted successfully, no subdirectory needed."
else else
disp I "Archive extracted successfully in subdirectory." disp I "Archive extracted successfully in subdirectory."
fi fi
shopt -u nullglob
fi fi
done done
done done

View File

@@ -336,6 +336,7 @@ file_stats()
} }
}' }'
} }
export -f file_stats
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------

View File

@@ -1,4 +1,4 @@
#!/bin/bash #!/usr/bin/env bash
# Begin profile # Begin profile
# ------------------------------------------------------------------------------ # ------------------------------------------------------------------------------
# Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org> # Copyright (c) 2013-2022 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>