mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-18 10:22:00 +00:00
feat(extract): always extract files into its own folder (#11596)
This commit is contained in:
parent
f7d903f3a3
commit
75405b7b0a
1 changed files with 51 additions and 33 deletions
|
@ -29,61 +29,79 @@ EOF
|
||||||
local success=0
|
local success=0
|
||||||
local extract_dir="${1:t:r}"
|
local extract_dir="${1:t:r}"
|
||||||
local file="$1" full_path="${1:A}"
|
local file="$1" full_path="${1:A}"
|
||||||
|
|
||||||
|
# Create an extraction directory based on the file name
|
||||||
|
command mkdir -p "$extract_dir"
|
||||||
|
builtin cd -q "$extract_dir"
|
||||||
|
|
||||||
case "${file:l}" in
|
case "${file:l}" in
|
||||||
(*.tar.gz|*.tgz)
|
(*.tar.gz|*.tgz)
|
||||||
(( $+commands[pigz] )) && { tar -I pigz -xvf "$file" } || tar zxvf "$file" ;;
|
(( $+commands[pigz] )) && { tar -I pigz -xvf "$full_path" } || tar zxvf "$full_path" ;;
|
||||||
(*.tar.bz2|*.tbz|*.tbz2)
|
(*.tar.bz2|*.tbz|*.tbz2)
|
||||||
(( $+commands[pbzip2] )) && { tar -I pbzip2 -xvf "$file" } || tar xvjf "$file" ;;
|
(( $+commands[pbzip2] )) && { tar -I pbzip2 -xvf "$full_path" } || tar xvjf "$full_path" ;;
|
||||||
(*.tar.xz|*.txz)
|
(*.tar.xz|*.txz)
|
||||||
(( $+commands[pixz] )) && { tar -I pixz -xvf "$file" } || {
|
(( $+commands[pixz] )) && { tar -I pixz -xvf "$full_path" } || {
|
||||||
tar --xz --help &> /dev/null \
|
tar --xz --help &> /dev/null \
|
||||||
&& tar --xz -xvf "$file" \
|
&& tar --xz -xvf "$full_path" \
|
||||||
|| xzcat "$file" | tar xvf - } ;;
|
|| xzcat "$full_path" | tar xvf - } ;;
|
||||||
(*.tar.zma|*.tlz)
|
(*.tar.zma|*.tlz)
|
||||||
tar --lzma --help &> /dev/null \
|
tar --lzma --help &> /dev/null \
|
||||||
&& tar --lzma -xvf "$file" \
|
&& tar --lzma -xvf "$full_path" \
|
||||||
|| lzcat "$file" | tar xvf - ;;
|
|| lzcat "$full_path" | tar xvf - ;;
|
||||||
(*.tar.zst|*.tzst)
|
(*.tar.zst|*.tzst)
|
||||||
tar --zstd --help &> /dev/null \
|
tar --zstd --help &> /dev/null \
|
||||||
&& tar --zstd -xvf "$file" \
|
&& tar --zstd -xvf "$full_path" \
|
||||||
|| zstdcat "$file" | tar xvf - ;;
|
|| zstdcat "$full_path" | tar xvf - ;;
|
||||||
(*.tar) tar xvf "$file" ;;
|
(*.tar) tar xvf "$full_path" ;;
|
||||||
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
|
(*.tar.lz) (( $+commands[lzip] )) && tar xvf "$full_path" ;;
|
||||||
(*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
|
(*.tar.lz4) lz4 -c -d "$full_path" | tar xvf - ;;
|
||||||
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
|
(*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$full_path" ;;
|
||||||
(*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
|
(*.gz) (( $+commands[pigz] )) && pigz -dk "$full_path" || gunzip -k "$full_path" ;;
|
||||||
(*.bz2) bunzip2 "$file" ;;
|
(*.bz2) bunzip2 "$full_path" ;;
|
||||||
(*.xz) unxz "$file" ;;
|
(*.xz) unxz "$full_path" ;;
|
||||||
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
|
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;;
|
||||||
(*.lz4) lz4 -d "$file" ;;
|
(*.lz4) lz4 -d "$full_path" ;;
|
||||||
(*.lzma) unlzma "$file" ;;
|
(*.lzma) unlzma "$full_path" ;;
|
||||||
(*.z) uncompress "$file" ;;
|
(*.z) uncompress "$full_path" ;;
|
||||||
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
|
(*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$full_path" ;;
|
||||||
(*.rar) unrar x -ad "$file" ;;
|
(*.rar) unrar x -ad "$full_path" ;;
|
||||||
(*.rpm)
|
(*.rpm)
|
||||||
command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
|
rpm2cpio "$full_path" | cpio --quiet -id ;;
|
||||||
&& rpm2cpio "$full_path" | cpio --quiet -id ;;
|
(*.7z) 7za x "$full_path" ;;
|
||||||
(*.7z) 7za x "$file" ;;
|
|
||||||
(*.deb)
|
(*.deb)
|
||||||
command mkdir -p "$extract_dir/control" "$extract_dir/data"
|
command mkdir -p "control" "data"
|
||||||
builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
|
ar vx "$full_path" > /dev/null
|
||||||
builtin cd -q control; extract ../control.tar.*
|
builtin cd -q control; extract ../control.tar.*
|
||||||
builtin cd -q ../data; extract ../data.tar.*
|
builtin cd -q ../data; extract ../data.tar.*
|
||||||
builtin cd -q ..; command rm *.tar.* debian-binary ;;
|
builtin cd -q ..; command rm *.tar.* debian-binary ;;
|
||||||
(*.zst) unzstd "$file" ;;
|
(*.zst) unzstd "$full_path" ;;
|
||||||
(*.cab) cabextract -d "$extract_dir" "$file" ;;
|
(*.cab) cabextract "$full_path" ;;
|
||||||
(*.cpio|*.obscpio) cpio -idmvF "$file" ;;
|
(*.cpio|*.obscpio) cpio -idmvF "$full_path" ;;
|
||||||
(*.zpaq) zpaq x "$file" ;;
|
(*.zpaq) zpaq x "$full_path" ;;
|
||||||
(*)
|
(*)
|
||||||
echo "extract: '$file' 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 "$full_path"
|
(( success == 0 && remove_archive == 0 )) && command rm "$full_path"
|
||||||
shift
|
shift
|
||||||
|
|
||||||
# Go back to original working directory in case we ran cd previously
|
# Go back to original working directory
|
||||||
|
# and remove extraction directory if there was an error
|
||||||
builtin cd -q "$pwd"
|
builtin cd -q "$pwd"
|
||||||
|
(( 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
|
||||||
done
|
done
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue