623 lines
20 KiB
Bash
623 lines
20 KiB
Bash
#!/usr/bin/env bash
|
|
# ------------------------------------------------------------------------------
|
|
# Copyright (c) 2013-2026 Geoffray Levasseur <fatalerrors@geoffray-levasseur.org>
|
|
# Protected by the BSD3 license. Please read bellow for details.
|
|
#
|
|
# * Redistribution and use in source and binary forms,
|
|
# * with or without modification, are permitted provided
|
|
# * that the following conditions are met:
|
|
# *
|
|
# * Redistributions of source code must retain the above
|
|
# * copyright notice, this list of conditions and the
|
|
# * following disclaimer.
|
|
# *
|
|
# * Redistributions in binary form must reproduce the above
|
|
# * copyright notice, this list of conditions and the following
|
|
# * disclaimer in the documentation and/or other materials
|
|
# * provided with the distribution.
|
|
# *
|
|
# * Neither the name of the copyright holder nor the names
|
|
# * of any other contributors may be used to endorse or
|
|
# * promote products derived from this software without
|
|
# * specific prior written permission.
|
|
# *
|
|
# * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
|
|
# * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
|
# * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
# * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
# * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
# * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
# * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
# * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
# * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
# * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
|
|
# * OF SUCH DAMAGE.
|
|
# ------------------------------------------------------------------------------
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Smartly uncompress archives
|
|
# Usage: utaz [option] [directorie(s)|file(s)]
|
|
# Options:
|
|
# -h, --help Display that help screen
|
|
# -d, --delete If decompression succeeded, delete the source file
|
|
# -c, --create-dir Always create a host directory
|
|
# -n, --no-dir Never create a host directory
|
|
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
|
|
}
|
|
|
|
_ununrar()
|
|
{
|
|
unrar x -o+ "$1" "$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_ununarj()
|
|
{
|
|
unarj e "$1" "$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_unlza()
|
|
{
|
|
# lha typically extracts into the current directory
|
|
# We ensure it hits the target directory
|
|
(cd "$2" && lha -x "../$1") >/dev/null 2>&1
|
|
}
|
|
|
|
_ununace()
|
|
{
|
|
unace x "$1" "$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_un7z()
|
|
{
|
|
7z x "$1" -o"$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_unzstd()
|
|
{
|
|
# Zstd decompresses files directly, often requiring tar for archives
|
|
tar --zstd -xf "$1" -C "$2"
|
|
}
|
|
|
|
_uncpio()
|
|
{
|
|
# CPIO requires careful directory handling
|
|
(cd "$2" && cpio -id < "../$1") >/dev/null 2>&1
|
|
}
|
|
|
|
_uncabextract()
|
|
{
|
|
# Requires 'cabextract' package
|
|
cabextract "$1" -d "$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_undeb()
|
|
{
|
|
# Extracts data content from a Debian package
|
|
dpkg-deb -x "$1" "$2/" >/dev/null 2>&1
|
|
}
|
|
|
|
_unrpm()
|
|
{
|
|
# Extracts CPIO-based payload from an RPM package
|
|
# Needs rpm2cpio and cpio
|
|
rpm2cpio "$1" | (cd "$2/" && cpio -idmv) >/dev/null 2>&1
|
|
}
|
|
|
|
local PARSED=$(getopt -o hdcn --long help,delete,create-dir,no-dir -n 'utaz' -- "$@")
|
|
|
|
if [ $? -ne 0 ]; then
|
|
disp E "Invalid options, use \"utaz --help\" to display usage."
|
|
return 1
|
|
fi
|
|
eval set -- "$PARSED"
|
|
while true; do
|
|
case "$1" in
|
|
-h|--help)
|
|
printf "utaz: uncompress all the given files and/or the ones found in the given\n"
|
|
printf " directories creating an host directory where needed.\n\n"
|
|
printf "Usage: utaz [option] [directorie(s)|file(s)]\n\n"
|
|
printf "Options:\n"
|
|
printf "\t-h, --help\t\tDisplay that help screen\n"
|
|
printf "\t-d, --delete\t\tIf decompression succeeded, delete the source file\n"
|
|
printf "\t-c, --create-dir\tAlways create a host directory\n"
|
|
printf "\t-n, --no-dir\t\tNever create a host directory\n\n"
|
|
printf "Supported archive format:\n"
|
|
printf "\t- zip\n"
|
|
printf "\t- tar.gz, .tgz\n"
|
|
printf "\t- tar.bz2, .tbz2\n"
|
|
printf "\t- tar.xz, .txz\n"
|
|
printf "\t- tar.lz, .tlz\n"
|
|
printf "\t- rar\n"
|
|
printf "\t- arj\n"
|
|
printf "\t- lha, lzh\n"
|
|
printf "\t- ace\n"
|
|
printf "\t- 7z, p7z\n"
|
|
printf "\t- zst\n"
|
|
printf "\t- cpio\n"
|
|
printf "\t- cab\n"
|
|
printf "\t- deb\n"
|
|
printf "\t- rpm\n"
|
|
return 0
|
|
;;
|
|
-d|--delete)
|
|
local willrm=1
|
|
shift
|
|
;;
|
|
-c|--create-dir)
|
|
local createdir=1
|
|
shift
|
|
;;
|
|
-n|--no-dir)
|
|
local nodir=1
|
|
shift
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
*)
|
|
disp E "Invalid option, use \"utaz --help\" to display options list"
|
|
return 1
|
|
;;
|
|
esac
|
|
done
|
|
# The remaining arguments after -- are the files/directories to process,
|
|
# "." is used if none is given
|
|
local FILES=("$@")
|
|
[[ ${#FILES[@]} -eq 0 ]] && FILES=(".")
|
|
|
|
[[ -n ${createdir} && -n ${nodir} ]] && \
|
|
disp E "The --create-dir and --no-dir options are mutually exclusive."
|
|
|
|
for zitem in "${FILES[@]}"; do
|
|
# Build list of input files to process, with whitespace-safe handling.
|
|
local targets=()
|
|
if [[ -f "$zitem" ]]; then
|
|
targets=("$zitem")
|
|
elif [[ -d "$zitem" ]]; then
|
|
mapfile -d '' -t targets < <(find "$zitem" -mindepth 1 -maxdepth 1 -print0 2>/dev/null)
|
|
if [[ ${#targets[@]} -eq 0 ]]; then
|
|
disp I "Directory ${zitem} is empty, skipping."
|
|
continue
|
|
fi
|
|
else
|
|
disp W "Path ${zitem} is not a file or directory, skipping."
|
|
continue
|
|
fi
|
|
|
|
for f in "${targets[@]}"; do
|
|
local dir="${f%.*}"
|
|
local extractor=""
|
|
case "$f" in
|
|
*.zip)
|
|
extractor="_ununzip"
|
|
;;
|
|
*.tar.gz|*.tgz)
|
|
extractor="_ungzip"
|
|
;;
|
|
*.tar.bz2|*.tbz2)
|
|
extractor="_unbzip2"
|
|
;;
|
|
*.tar.xz|*.txz)
|
|
extractor="_unxz"
|
|
;;
|
|
*.tar.lz|*.tlz)
|
|
extractor="_unlzop"
|
|
;;
|
|
*.tar)
|
|
extractor="_untar"
|
|
;;
|
|
*.rar)
|
|
extractor="_ununrar"
|
|
;;
|
|
*.arj)
|
|
extractor="_ununarj"
|
|
;;
|
|
*.lzh|*.lha)
|
|
extractor="_unlha"
|
|
;;
|
|
*.ace)
|
|
extractor="_ununace"
|
|
;;
|
|
*.7z|*.p7z)
|
|
extractor="_un7z"
|
|
;;
|
|
*.zst)
|
|
extractor="_unzstd"
|
|
;;
|
|
*.cpio)
|
|
extractor="_uncpio"
|
|
;;
|
|
*.cab)
|
|
extractor="_uncabextract"
|
|
;;
|
|
*.deb)
|
|
extractor="_undeb"
|
|
;;
|
|
*.rpm)
|
|
extractor="_unrpm"
|
|
;;
|
|
*)
|
|
disp I "File ${f} is not a supported archive, skipping."
|
|
continue
|
|
;; # Skip non-archive files
|
|
esac
|
|
|
|
# Verify binary existence
|
|
local cmd=${extractor//_un/}
|
|
if [[ $cmd == "deb" ]]; then
|
|
command -v -- dpkg-deb >/dev/null 2>&1 || {
|
|
disp E "The program 'dpkg-deb' is not installed, aborting."
|
|
continue
|
|
}
|
|
elif [[ $cmd == "rpm" ]]; then
|
|
command -v -- rpm2cpio >/dev/null 2>&1 || {
|
|
disp E "The program 'rpm2cpio' is not installed, aborting."
|
|
continue
|
|
}
|
|
command -v -- cpio >/dev/null 2>&1 || {
|
|
disp E "The program 'cpio' is not installed, aborting."
|
|
continue
|
|
}
|
|
else
|
|
command -v -- "${cmd}" >/dev/null 2>&1 || {
|
|
disp E "Binary ${cmd} necessary to extract ${f} is missing."
|
|
continue
|
|
}
|
|
fi
|
|
|
|
disp I "Processing archive ${f} with ${extractor}..."
|
|
mkdir -p "${dir}"
|
|
[[ $? -gt 0 ]] &&
|
|
disp E "The filesystem can't create directories, exit!" &&
|
|
return 1
|
|
|
|
${extractor} "${f}" "${dir}"
|
|
case $? in
|
|
0)
|
|
[[ -n ${willrm} ]] &&
|
|
rm -f "${f}" && disp I "File ${zitem}/${f} deleted."
|
|
;;
|
|
1)
|
|
if [[ -n ${willrm} ]]; then
|
|
disp W "Compression program returned a warning: deletion canceled."
|
|
else
|
|
disp W "Compression program returned a warning."
|
|
fi
|
|
;;
|
|
*)
|
|
disp E "The compressed file ${f} seems corrupted, failed."
|
|
rm -rf "${dir}" >/dev/null 2>&1
|
|
continue
|
|
;;
|
|
esac
|
|
|
|
if [[ -n ${createdir} ]]; then
|
|
disp I "Archive extracted successfully in subdirectory."
|
|
elif [[ -n ${nodir} ]]; then
|
|
shopt -s nullglob
|
|
for child in "${dir}"/*; do
|
|
mv -- "$child" .
|
|
done
|
|
shopt -u nullglob
|
|
rmdir -- "${dir}"
|
|
disp I "Archive extracted successfully, no subdirectory needed."
|
|
else
|
|
# Set nullglob to ensure the array is empty if no files match
|
|
shopt -s nullglob
|
|
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
|
|
shopt -s nullglob
|
|
for child in "${contents[0]}"/*; do
|
|
mv -- "$child" .
|
|
done
|
|
shopt -u nullglob
|
|
rmdir -- "${dir}"
|
|
disp I "Archive extracted successfully, no subdirectory needed."
|
|
else
|
|
disp I "Archive extracted successfully in subdirectory."
|
|
fi
|
|
shopt -u nullglob
|
|
fi
|
|
done
|
|
done
|
|
}
|
|
export -f utaz
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# ------------------------------------------------------------------------------
|
|
# Compress directories or files into one or more archive
|
|
# Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]
|
|
# Options:
|
|
# -h, --help Display that help screen
|
|
# -d, --delete Delete source file or directory after success
|
|
# -f, --format Chose archive format in the given list. If several format are
|
|
# given, the smalest is kept
|
|
# -p, --parallel Number of threads to use (if allowed by underlying utility)
|
|
# -v, --verbose Display progress where possible
|
|
# -q, --quiet Display less messages (only errors and warnings)
|
|
# -1, .., -9 Compression level to use [1=fast/biggest, 9=slow/smallest]
|
|
taz()
|
|
{
|
|
_doxz()
|
|
{
|
|
command -v xz >/dev/null 2>&1 || {
|
|
disp E "The program 'xz' is not installed, aborting."
|
|
return 127
|
|
}
|
|
|
|
[[ $4 ]] && local verb='-v'
|
|
|
|
# Display a warning for this format
|
|
disp W "xz format is not suited for long term archiving."
|
|
disp I "See https://www.nongnu.org/lzip/xz_inadequate.html for details."
|
|
|
|
# Compresse to xz (lzma2) - Deprecated
|
|
xz $verb --compress --keep -$3 -T $2 $1
|
|
return $?
|
|
}
|
|
|
|
_dolz()
|
|
{
|
|
local procopt="--threads $2"
|
|
local command=plzip
|
|
|
|
command -v plzip >/dev/null 2>&1 || {
|
|
command -v lzip >/dev/null 2>&1 || {
|
|
disp E "Program 'plzip' or 'lzip' are not installed, aborting."
|
|
return 127
|
|
}
|
|
command=lzip
|
|
local procopt=""
|
|
[[ $2 -gt 1 ]] &&
|
|
disp W "lzip doesn't support multithreading, falling back to 1 thread." &&
|
|
disp W "Consider installing plzip to obtain multithreading abilities."
|
|
}
|
|
|
|
[[ $4 ]] && local verb="-vv"
|
|
|
|
# Compresse au format lzip (lzma)
|
|
$command $verb $procopt --keep -$3 $1
|
|
return $?
|
|
}
|
|
|
|
_dogz()
|
|
{
|
|
local procopt="--processes $2"
|
|
local command=pigz
|
|
|
|
command -v pigz >/dev/null 2>&1 || {
|
|
command -v gzip >/dev/null 2>&1 || {
|
|
disp E "Programs 'pigz' or 'gzip' are not installed, aborting."
|
|
return 127
|
|
}
|
|
local command="gzip --compress"
|
|
local procopt=""
|
|
[[ $2 -gt 1 ]] &&
|
|
disp W "gzip doesn't support multithreading, falling back to 1 thread." &&
|
|
disp W "Consider installing pigz to obtain multithreading abilities."
|
|
}
|
|
|
|
[[ $4 ]] && local verb="--verbose"
|
|
|
|
# Compresse au format bz2
|
|
$command $verb $procopt --keep -$3 $1
|
|
return $?
|
|
}
|
|
|
|
_dobz2()
|
|
{
|
|
local procopt="-p$2"
|
|
local command=pbzip2
|
|
|
|
command -v pbzip2 >/dev/null 2>&1 || {
|
|
command -v bzip2 >/dev/null 2>&1 || {
|
|
disp E "The program 'pbzip2' or 'bzip2' are not installed, aborting."
|
|
return 127
|
|
}
|
|
local command=bzip2
|
|
local procopt=""
|
|
[[ $2 -gt 1 ]] &&
|
|
disp W "bzip2 doesn't support multithreading, falling back to 1 thread." &&
|
|
disp W "Consider installing pbzip2 to obtain multithreading abilities."
|
|
}
|
|
|
|
[[ $4 ]] && local verb="-v"
|
|
|
|
# Compresse au format bz2
|
|
$command $verb --compress $procopt --keep -$3 $1
|
|
return $?
|
|
}
|
|
|
|
_dolzo()
|
|
{
|
|
command -v lzop >/dev/null 2>&1 || {
|
|
disp E "The program 'lzop' is not installed, aborting."
|
|
return 127
|
|
}
|
|
|
|
[[ $4 ]] && local verb='-v'
|
|
[[ $2 -gt 1 ]] && disp W "lzop doesn't support multithreading, falling back to 1 thread."
|
|
|
|
# Compresse au format lzo
|
|
lzop --keep -$3 $1
|
|
return $?
|
|
}
|
|
|
|
local PARSED
|
|
PARSED=$(getopt -o hdf:p:vq123456789 --long help,delete,format:,parallel:,verbose,quiet --name "taz" -- "$@")
|
|
if [ $? -ne 0 ]; then
|
|
disp E "Invalid options, use \"taz --help\" to display usage."
|
|
return 1
|
|
fi
|
|
eval set -- "$PARSED"
|
|
while true; do
|
|
case "$1" in
|
|
-h|--help)
|
|
printf "taz: archive all files of a directory.\n\n"
|
|
printf "Usage: taz [option] [--parallel=<n>] [--format=<format>] [directory1 ... directoryN]\n\n"
|
|
printf "Options:\n"
|
|
printf "\t-h, --help\tDisplay that help screen\n"
|
|
printf "\t-d, --delete\tDelete source file or directory after success\n"
|
|
printf "\t-f, --format\tChose archive format in the given list. If several format are"
|
|
printf "\t\t\tgiven, the smalest is kept\n"
|
|
printf "\t-p, --parallel\tNumber of threads to use (if allowed by underlying utility)\n"
|
|
printf "\t-v, --verbose\tDisplay progress where possible\n"
|
|
printf "\t-q, --quiet\tDisplay less messages (only errors and warnings)\n"
|
|
printf "\t-1, .., -9\tCompression level to use [1=fast/biggest, 9=slow/smallest]\n\n"
|
|
printf "Supported archive format:\n"
|
|
printf "\tParam.| programs | Algo. | Description\n"
|
|
printf "\t------+---------------+-------+----------------------------------------\n"
|
|
printf "\t lz | plzip, lzip | lzma | Safe efficient default format\n"
|
|
printf "\t xz | xz | lzma2 | Unsafe, not for long term\n"
|
|
printf "\t bz2 | pbzip2, bzip2 | bzip2 | Historical but less efficient than lz\n"
|
|
printf "\t gz | pigz, gzip | lz77 | Historical, safe, fast\n"
|
|
printf "\t lzo | lzop | lzo | Very fast but no multithread\n"
|
|
printf "\t tar | tar | tar | No compression\n"
|
|
printf "\n"
|
|
return 0
|
|
;;
|
|
|
|
-d|--delete)
|
|
local willrm=1
|
|
shift
|
|
;;
|
|
|
|
-f|--format)
|
|
local compform=$2
|
|
shift 2
|
|
;;
|
|
|
|
-p|--parallel)
|
|
local nproc=$2
|
|
shift 2
|
|
;;
|
|
|
|
-v|--verbose)
|
|
local verbose=1
|
|
shift
|
|
;;
|
|
|
|
-q|--quiet)
|
|
local quiet=1
|
|
shift
|
|
;;
|
|
|
|
-[1-9])
|
|
compression="${1#-}"
|
|
shift
|
|
;;
|
|
--)
|
|
shift
|
|
break
|
|
;;
|
|
*)
|
|
disp E "Invalid option, use \"taz --help\" to display options list"
|
|
return 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
# The remaining arguments after -- are the files/directories to process,
|
|
# "." is used if none is given
|
|
local FILES=("$@")
|
|
[[ ${#FILES[@]} -eq 0 ]] && FILES=(".")
|
|
|
|
[[ ! $compform ]] && compform=lz # safe and efficient (unless data are already compressed)
|
|
[[ ! $nproc ]] && nproc=1
|
|
[[ ! $complevel ]] && complevel=6
|
|
[[ $verbose -gt 1 && $quiet -gt 1 ]] &&
|
|
disp E "The --verbose and --quiet options can't be used together."
|
|
|
|
for item in "${FILES[@]}"; do
|
|
local donetar=0
|
|
disp I "Processing $item..."
|
|
|
|
if [[ -d "$item" ]]; then
|
|
disp I "\t Creating $item.tar... "
|
|
|
|
tar -cf "$item.tar" "$item"
|
|
if [[ ! $? -eq 0 ]]; then
|
|
disp E "tar file creation failed, skipping to next item."
|
|
continue
|
|
fi
|
|
|
|
local donetar=1
|
|
fi
|
|
|
|
local fname=$item
|
|
[[ $donetar -gt 0 ]] && fname=$item.tar
|
|
|
|
# Skip compression part if tar is asked
|
|
if [[ $compform != "tar" ]]; then
|
|
disp I "\t Compressing archive..."
|
|
_do$compform "$fname" "$nproc" "$complevel" "$verbose"
|
|
[[ ! $? -eq 0 ]] && case $? in
|
|
127)
|
|
disp E "Compression program unavailable, aborting."
|
|
return 127
|
|
;;
|
|
*)
|
|
disp E "Compression program returned an error, not deleting anything if asked, skipping to next item."
|
|
continue
|
|
;;
|
|
esac
|
|
|
|
[[ $donetar -gt 0 ]] && rm "$fname"
|
|
fi
|
|
|
|
if [[ $willrm ]]; then
|
|
disp I "\t Deleting original source as asked... "
|
|
rm -r "$item"
|
|
fi
|
|
done
|
|
unset quiet
|
|
}
|
|
export -f taz
|
|
# ------------------------------------------------------------------------------
|
|
|
|
|
|
# EOF
|