2016-09-29 04:19:47 -07:00
|
|
|
alias x=extract
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2016-09-29 04:19:47 -07:00
|
|
|
extract() {
|
2021-08-14 13:03:41 -07:00
|
|
|
setopt localoptions noautopushd
|
2021-08-14 13:05:48 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
if (( $# == 0 )); then
|
|
|
|
cat >&2 <<'EOF'
|
|
|
|
Usage: extract [-option] [file ...]
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
Options:
|
|
|
|
-r, --remove Remove archive after unpacking.
|
|
|
|
EOF
|
|
|
|
fi
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
local remove_archive=1
|
|
|
|
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
|
|
|
|
remove_archive=0
|
|
|
|
shift
|
|
|
|
fi
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
local pwd="$PWD"
|
|
|
|
while (( $# > 0 )); do
|
|
|
|
if [[ ! -f "$1" ]]; then
|
|
|
|
echo "extract: '$1' is not a valid file" >&2
|
|
|
|
shift
|
|
|
|
continue
|
|
|
|
fi
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
local success=0
|
|
|
|
local extract_dir="${1:t:r}"
|
|
|
|
local file="$1" full_path="${1:A}"
|
2023-04-02 05:28:01 -07:00
|
|
|
|
|
|
|
# Create an extraction directory based on the file name
|
|
|
|
command mkdir -p "$extract_dir"
|
|
|
|
builtin cd -q "$extract_dir"
|
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
case "${file:l}" in
|
2023-01-16 05:19:01 -07:00
|
|
|
(*.tar.gz|*.tgz)
|
2023-04-02 05:28:01 -07:00
|
|
|
(( $+commands[pigz] )) && { tar -I pigz -xvf "$full_path" } || tar zxvf "$full_path" ;;
|
2023-01-16 05:19:01 -07:00
|
|
|
(*.tar.bz2|*.tbz|*.tbz2)
|
2023-04-02 05:28:01 -07:00
|
|
|
(( $+commands[pbzip2] )) && { tar -I pbzip2 -xvf "$full_path" } || tar xvjf "$full_path" ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*.tar.xz|*.txz)
|
2023-04-02 05:28:01 -07:00
|
|
|
(( $+commands[pixz] )) && { tar -I pixz -xvf "$full_path" } || {
|
2021-08-14 13:03:41 -07:00
|
|
|
tar --xz --help &> /dev/null \
|
2023-04-02 05:28:01 -07:00
|
|
|
&& tar --xz -xvf "$full_path" \
|
|
|
|
|| xzcat "$full_path" | tar xvf - } ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*.tar.zma|*.tlz)
|
|
|
|
tar --lzma --help &> /dev/null \
|
2023-04-02 05:28:01 -07:00
|
|
|
&& tar --lzma -xvf "$full_path" \
|
|
|
|
|| lzcat "$full_path" | tar xvf - ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*.tar.zst|*.tzst)
|
|
|
|
tar --zstd --help &> /dev/null \
|
2023-04-02 05:28:01 -07:00
|
|
|
&& tar --zstd -xvf "$full_path" \
|
|
|
|
|| zstdcat "$full_path" | tar xvf - ;;
|
|
|
|
(*.tar) tar xvf "$full_path" ;;
|
|
|
|
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$full_path" ;;
|
|
|
|
(*.tar.lz4) lz4 -c -d "$full_path" | tar xvf - ;;
|
|
|
|
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$full_path" ;;
|
|
|
|
(*.gz) (( $+commands[pigz] )) && pigz -dk "$full_path" || gunzip -k "$full_path" ;;
|
|
|
|
(*.bz2) bunzip2 "$full_path" ;;
|
|
|
|
(*.xz) unxz "$full_path" ;;
|
|
|
|
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;;
|
|
|
|
(*.lz4) lz4 -d "$full_path" ;;
|
|
|
|
(*.lzma) unlzma "$full_path" ;;
|
|
|
|
(*.z) uncompress "$full_path" ;;
|
|
|
|
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$full_path" ;;
|
|
|
|
(*.rar) unrar x -ad "$full_path" ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*.rpm)
|
2023-04-02 05:28:01 -07:00
|
|
|
rpm2cpio "$full_path" | cpio --quiet -id ;;
|
|
|
|
(*.7z) 7za x "$full_path" ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*.deb)
|
2023-04-02 05:28:01 -07:00
|
|
|
command mkdir -p "control" "data"
|
|
|
|
ar vx "$full_path" > /dev/null
|
2021-08-14 13:03:41 -07:00
|
|
|
builtin cd -q control; extract ../control.tar.*
|
|
|
|
builtin cd -q ../data; extract ../data.tar.*
|
|
|
|
builtin cd -q ..; command rm *.tar.* debian-binary ;;
|
2023-04-02 05:28:01 -07:00
|
|
|
(*.zst) unzstd "$full_path" ;;
|
|
|
|
(*.cab) cabextract "$full_path" ;;
|
|
|
|
(*.cpio|*.obscpio) cpio -idmvF "$full_path" ;;
|
|
|
|
(*.zpaq) zpaq x "$full_path" ;;
|
2021-08-14 13:03:41 -07:00
|
|
|
(*)
|
|
|
|
echo "extract: '$file' cannot be extracted" >&2
|
|
|
|
success=1 ;;
|
|
|
|
esac
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2021-08-14 13:03:41 -07:00
|
|
|
(( success = success > 0 ? success : $? ))
|
2023-04-02 05:28:01 -07:00
|
|
|
(( success == 0 && remove_archive == 0 )) && command rm "$full_path"
|
2021-08-14 13:03:41 -07:00
|
|
|
shift
|
2011-02-03 20:46:52 -07:00
|
|
|
|
2023-04-02 05:28:01 -07:00
|
|
|
# Go back to original working directory
|
|
|
|
# and remove extraction directory if there was an error
|
2021-08-14 13:03:41 -07:00
|
|
|
builtin cd -q "$pwd"
|
2023-04-02 05:28:01 -07:00
|
|
|
(( success > 0 )) && command rm -r "$extract_dir"
|
|
|
|
|
|
|
|
# If content of extract dir is a single directory, move its contents up
|
|
|
|
# Glob flags:
|
|
|
|
# - D: include files starting with .
|
|
|
|
# - N: no error if directory is empty
|
|
|
|
# - Y2: at most give 2 files
|
|
|
|
local -a content
|
|
|
|
content=("${extract_dir}"/*(DNY2))
|
|
|
|
if [[ ${#content} -eq 1 && -d "${content[1]}" ]]; then
|
|
|
|
command mv -f "${content[1]}" .
|
|
|
|
command rmdir "$extract_dir"
|
|
|
|
fi
|
2021-08-14 13:03:41 -07:00
|
|
|
done
|
2011-02-03 20:46:52 -07:00
|
|
|
}
|