extractor/extract

47 lines
1.2 KiB
Text
Raw Permalink Normal View History

2024-06-20 11:39:15 +02:00
#!/bin/bash
set -euo pipefail
IFS=$'\n\t'
usage() {
echo "Usage: $0 <archive-file>"
echo "Extract supported archive formats: .tar.gz, .zip, .rar, .7z, etc."
}
check_command() {
local cmd="$1"
if ! command -v "$cmd" &>/dev/null; then
echo "Error: required command '$cmd' not found."
exit 3
fi
}
if [[ $# -eq 0 || "$1" == "--help" ]]; then
usage
exit 0
fi
file="$1"
if [[ ! -f "$file" ]]; then
echo "Error: '$file' is not a valid file."
exit 2
fi
case "$file" in
*.tar.bz2) check_command tar; tar xjf "$file" ;;
*.tar.gz) check_command tar; tar xzf "$file" ;;
*.bz2) check_command bunzip2; bunzip2 "$file" ;;
*.rar) check_command unrar; unrar x "$file" ;;
*.gz) check_command gunzip; gunzip "$file" ;;
*.tar) check_command tar; tar xf "$file" ;;
*.tbz2) check_command tar; tar xjf "$file" ;;
*.tgz) check_command tar; tar xzf "$file" ;;
*.zip) check_command unzip; unzip "$file" ;;
*.Z) check_command uncompress; uncompress "$file" ;;
*.7z) check_command 7z; 7z x "$file" ;;
*)
echo "Error: '$file' has an unsupported file extension."
exit 1
;;
esac