Unverified Commit 0b809c8d authored by Marc Cornellà's avatar Marc Cornellà
Browse files

Merge branch 'refactor/extract-plugin'

Closes #7948
Closes #9959
parents af6c7f3d 0b32e4b2
...@@ -21,6 +21,8 @@ plugins=(... extract) ...@@ -21,6 +21,8 @@ plugins=(... extract)
| `apk` | Android app file | | `apk` | Android app file |
| `aar` | Android library file | | `aar` | Android library file |
| `bz2` | Bzip2 file | | `bz2` | Bzip2 file |
| `cab` | Microsoft cabinet archive |
| `cpio` | Cpio archive |
| `deb` | Debian package | | `deb` | Debian package |
| `ear` | Enterprise Application aRchive | | `ear` | Enterprise Application aRchive |
| `gz` | Gzip file | | `gz` | Gzip file |
......
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
_arguments \ _arguments \
'(-r --remove)'{-r,--remove}'[Remove archive.]' \ '(-r --remove)'{-r,--remove}'[Remove archive.]' \
"*::archive file:_files -g '(#i)*.(7z|Z|apk|aar|bz2|deb|ear|gz|ipa|ipsw|jar|lrz|lz4|lzma|rar|rpm|sublime-package|tar|tar.bz2|tar.gz|tar.lrz|tar.lz|tar.lz4|tar.xz|tar.zma|tar.zst|tbz|tbz2|tgz|tlz|txz|tzst|war|whl|xpi|xz|zip|zst)(-.)'" \ "*::archive file:_files -g '(#i)*.(7z|Z|apk|aar|bz2|cab|cpio|deb|ear|gz|ipa|ipsw|jar|lrz|lz4|lzma|rar|rpm|sublime-package|tar|tar.bz2|tar.gz|tar.lrz|tar.lz|tar.lz4|tar.xz|tar.zma|tar.zst|tbz|tbz2|tgz|tlz|txz|tzst|war|whl|xpi|xz|zip|zst)(-.)'" \
&& return 0 && return 0
alias x=extract alias x=extract
extract() { extract() {
local remove_archive setopt localoptions noautopushd
local success
local extract_dir
if (( $# == 0 )); then if (( $# == 0 )); then
cat <<-'EOF' >&2 cat >&2 <<'EOF'
Usage: extract [-option] [file ...] Usage: extract [-option] [file ...]
Options: Options:
-r, --remove Remove archive after unpacking. -r, --remove Remove archive after unpacking.
EOF EOF
fi fi
remove_archive=1 local remove_archive=1
if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
remove_archive=0 remove_archive=0
shift shift
fi fi
local pwd="$PWD"
while (( $# > 0 )); do while (( $# > 0 )); do
if [[ ! -f "$1" ]]; then if [[ ! -f "$1" ]]; then
echo "extract: '$1' is not a valid file" >&2 echo "extract: '$1' is not a valid file" >&2
...@@ -27,56 +26,60 @@ extract() { ...@@ -27,56 +26,60 @@ extract() {
continue continue
fi fi
success=0 local success=0
extract_dir="${1:t:r}" local extract_dir="${1:t:r}"
case "${1:l}" in local file="$1" full_path="${1:A}"
(*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$1" | tar xv } || tar zxvf "$1" ;; case "${file:l}" in
(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;; (*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$file" | tar xv } || tar zxvf "$file" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
(*.tar.xz|*.txz) (*.tar.xz|*.txz)
tar --xz --help &> /dev/null \ tar --xz --help &> /dev/null \
&& tar --xz -xvf "$1" \ && tar --xz -xvf "$file" \
|| xzcat "$1" | tar xvf - ;; || xzcat "$file" | tar xvf - ;;
(*.tar.zma|*.tlz) (*.tar.zma|*.tlz)
tar --lzma --help &> /dev/null \ tar --lzma --help &> /dev/null \
&& tar --lzma -xvf "$1" \ && tar --lzma -xvf "$file" \
|| lzcat "$1" | tar xvf - ;; || lzcat "$file" | tar xvf - ;;
(*.tar.zst|*.tzst) (*.tar.zst|*.tzst)
tar --zstd --help &> /dev/null \ tar --zstd --help &> /dev/null \
&& tar --zstd -xvf "$1" \ && tar --zstd -xvf "$file" \
|| zstdcat "$1" | tar xvf - ;; || zstdcat "$file" | tar xvf - ;;
(*.tar) tar xvf "$1" ;; (*.tar) tar xvf "$file" ;;
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$1" ;; (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
(*.tar.lz4) lz4 -c -d "$1" | tar xvf - ;; (*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$1" ;; (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
(*.gz) (( $+commands[pigz] )) && pigz -dk "$1" || gunzip -k "$1" ;; (*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
(*.bz2) bunzip2 "$1" ;; (*.bz2) bunzip2 "$file" ;;
(*.xz) unxz "$1" ;; (*.xz) unxz "$file" ;;
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$1" ;; (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
(*.lz4) lz4 -d "$1" ;; (*.lz4) lz4 -d "$file" ;;
(*.lzma) unlzma "$1" ;; (*.lzma) unlzma "$file" ;;
(*.z) uncompress "$1" ;; (*.z) uncompress "$file" ;;
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d $extract_dir ;; (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
(*.rar) unrar x -ad "$1" ;; (*.rar) unrar x -ad "$file" ;;
(*.rpm) mkdir "$extract_dir" && cd "$extract_dir" && rpm2cpio "../$1" | cpio --quiet -id && cd .. ;; (*.rpm)
(*.7z) 7za x "$1" ;; command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
&& rpm2cpio "$full_path" | cpio --quiet -id ;;
(*.7z) 7za x "$file" ;;
(*.deb) (*.deb)
mkdir -p "$extract_dir/control" command mkdir -p "$extract_dir/control" "$extract_dir/data"
mkdir -p "$extract_dir/data" builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
cd "$extract_dir"; ar vx "../${1}" > /dev/null builtin cd -q control; extract ../control.tar.*
cd control; tar xzvf ../control.tar.gz builtin cd -q ../data; extract ../data.tar.*
cd ../data; extract ../data.tar.* builtin cd -q ..; command rm *.tar.* debian-binary ;;
cd ..; rm *.tar.* debian-binary (*.zst) unzstd "$file" ;;
cd .. (*.cab) cabextract -d "$extract_dir" "$file" ;;
;; (*.cpio) cpio -idmvF "$file" ;;
(*.zst) unzstd "$1" ;;
(*) (*)
echo "extract: '$1' cannot be extracted" >&2 echo "extract: '$file' cannot be extracted" >&2
success=1 success=1 ;;
;;
esac esac
(( success = $success > 0 ? $success : $? )) (( success = success > 0 ? success : $? ))
(( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1" (( success == 0 && remove_archive == 0 )) && rm "$full_path"
shift shift
# Go back to original working directory in case we ran cd previously
builtin cd -q "$pwd"
done done
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment