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
while (( $# > 0 )); do local pwd="$PWD"
if [[ ! -f "$1" ]]; then while (( $# > 0 )); do
echo "extract: '$1' is not a valid file" >&2 if [[ ! -f "$1" ]]; then
shift echo "extract: '$1' is not a valid file" >&2
continue shift
fi continue
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.xz|*.txz) (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
tar --xz --help &> /dev/null \ (*.tar.xz|*.txz)
&& tar --xz -xvf "$1" \ tar --xz --help &> /dev/null \
|| xzcat "$1" | tar xvf - ;; && tar --xz -xvf "$file" \
(*.tar.zma|*.tlz) || xzcat "$file" | tar xvf - ;;
tar --lzma --help &> /dev/null \ (*.tar.zma|*.tlz)
&& tar --lzma -xvf "$1" \ tar --lzma --help &> /dev/null \
|| lzcat "$1" | tar xvf - ;; && tar --lzma -xvf "$file" \
(*.tar.zst|*.tzst) || lzcat "$file" | tar xvf - ;;
tar --zstd --help &> /dev/null \ (*.tar.zst|*.tzst)
&& tar --zstd -xvf "$1" \ tar --zstd --help &> /dev/null \
|| zstdcat "$1" | tar xvf - ;; && tar --zstd -xvf "$file" \
(*.tar) tar xvf "$1" ;; || zstdcat "$file" | tar xvf - ;;
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$1" ;; (*.tar) tar xvf "$file" ;;
(*.tar.lz4) lz4 -c -d "$1" | tar xvf - ;; (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$1" ;; (*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
(*.gz) (( $+commands[pigz] )) && pigz -dk "$1" || gunzip -k "$1" ;; (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
(*.bz2) bunzip2 "$1" ;; (*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
(*.xz) unxz "$1" ;; (*.bz2) bunzip2 "$file" ;;
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$1" ;; (*.xz) unxz "$file" ;;
(*.lz4) lz4 -d "$1" ;; (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
(*.lzma) unlzma "$1" ;; (*.lz4) lz4 -d "$file" ;;
(*.z) uncompress "$1" ;; (*.lzma) unlzma "$file" ;;
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d $extract_dir ;; (*.z) uncompress "$file" ;;
(*.rar) unrar x -ad "$1" ;; (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
(*.rpm) mkdir "$extract_dir" && cd "$extract_dir" && rpm2cpio "../$1" | cpio --quiet -id && cd .. ;; (*.rar) unrar x -ad "$file" ;;
(*.7z) 7za x "$1" ;; (*.rpm)
(*.deb) command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
mkdir -p "$extract_dir/control" && rpm2cpio "$full_path" | cpio --quiet -id ;;
mkdir -p "$extract_dir/data" (*.7z) 7za x "$file" ;;
cd "$extract_dir"; ar vx "../${1}" > /dev/null (*.deb)
cd control; tar xzvf ../control.tar.gz command mkdir -p "$extract_dir/control" "$extract_dir/data"
cd ../data; extract ../data.tar.* builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
cd ..; rm *.tar.* debian-binary builtin cd -q control; extract ../control.tar.*
cd .. builtin cd -q ../data; extract ../data.tar.*
;; builtin cd -q ..; command rm *.tar.* debian-binary ;;
(*.zst) unzstd "$1" ;; (*.zst) unzstd "$file" ;;
(*) (*.cab) cabextract -d "$extract_dir" "$file" ;;
echo "extract: '$1' cannot be extracted" >&2 (*.cpio) cpio -idmvF "$file" ;;
success=1 (*)
;; echo "extract: '$file' cannot be extracted" >&2
esac success=1 ;;
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
done
# Go back to original working directory in case we ran cd previously
builtin cd -q "$pwd"
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