fix(extract): extraction to directory for single-file .gz (#11852)

This commit is contained in:
Marc Cornellà 2023-08-23 13:25:33 +02:00 committed by GitHub
parent dfe2f04de7
commit 3f477e5da5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -64,7 +64,7 @@ EOF
(*.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" ;;
(*.gz) (( $+commands[pigz] )) && pigz -cdk "$full_path" > "${file:t:r}" || gunzip -ck "$full_path" > "${file:t:r}" ;;
(*.bz2) bunzip2 "$full_path" ;;
(*.xz) unxz "$full_path" ;;
(*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;;
@ -106,19 +106,19 @@ EOF
# - Y2: at most give 2 files
local -a content
content=("${extract_dir}"/*(DNY2))
if [[ ${#content} -eq 1 && -d "${content[1]}" ]]; then
# The extracted folder (${content[1]}) may have the same name as $extract_dir
if [[ ${#content} -eq 1 && -e "${content[1]}" ]]; then
# The extracted file/folder (${content[1]}) may have the same name as $extract_dir
# If so, we need to rename it to avoid conflicts in a 3-step process
#
# 1. Move and rename the extracted folder to a temporary random name
# 1. Move and rename the extracted file/folder to a temporary random name
# 2. Delete the empty folder
# 3. Rename the extracted folder to the original name
# 3. Rename the extracted file/folder to the original name
if [[ "${content[1]:t}" == "$extract_dir" ]]; then
# =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
local tmp_dir==(:); tmp_dir="${tmp_dir:t}"
command mv "${content[1]}" "$tmp_dir" \
local tmp_name==(:); tmp_name="${tmp_name:t}"
command mv "${content[1]}" "$tmp_name" \
&& command rmdir "$extract_dir" \
&& command mv "$tmp_dir" "$extract_dir"
&& command mv "$tmp_name" "$extract_dir"
# Otherwise, if the extracted folder name already exists in the current
# directory (because of a previous file / folder), keep the extract_dir
elif [[ ! -e "${content[1]:t}" ]]; then