extract.plugin.zsh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. alias x=extract
  2. extract() {
  3. setopt localoptions noautopushd
  4. if (( $# == 0 )); then
  5. cat >&2 <<'EOF'
  6. Usage: extract [-option] [file ...]
  7. Options:
  8. -r, --remove Remove archive after unpacking.
  9. EOF
  10. fi
  11. local remove_archive=1
  12. if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
  13. remove_archive=0
  14. shift
  15. fi
  16. local pwd="$PWD"
  17. while (( $# > 0 )); do
  18. if [[ ! -f "$1" ]]; then
  19. echo "extract: '$1' is not a valid file" >&2
  20. shift
  21. continue
  22. fi
  23. local success=0
  24. local file="$1" full_path="${1:A}"
  25. local extract_dir="${1:t:r}"
  26. # Remove the .tar extension if the file name is .tar.*
  27. if [[ $extract_dir =~ '\.tar$' ]]; then
  28. extract_dir="${extract_dir:r}"
  29. fi
  30. # If there's a file or directory with the same name as the archive
  31. # add a random string to the end of the extract directory
  32. if [[ -e "$extract_dir" ]]; then
  33. local rnd="${(L)"${$(( [##36]$RANDOM*$RANDOM ))}":1:5}"
  34. extract_dir="${extract_dir}-${rnd}"
  35. fi
  36. # Create an extraction directory based on the file name
  37. command mkdir -p "$extract_dir"
  38. builtin cd -q "$extract_dir"
  39. echo "extract: extracting to $extract_dir" >&2
  40. case "${file:l}" in
  41. (*.tar.gz|*.tgz)
  42. (( $+commands[pigz] )) && { tar -I pigz -xvf "$full_path" } || tar zxvf "$full_path" ;;
  43. (*.tar.bz2|*.tbz|*.tbz2)
  44. (( $+commands[pbzip2] )) && { tar -I pbzip2 -xvf "$full_path" } || tar xvjf "$full_path" ;;
  45. (*.tar.xz|*.txz)
  46. (( $+commands[pixz] )) && { tar -I pixz -xvf "$full_path" } || {
  47. tar --xz --help &> /dev/null \
  48. && tar --xz -xvf "$full_path" \
  49. || xzcat "$full_path" | tar xvf - } ;;
  50. (*.tar.zma|*.tlz)
  51. tar --lzma --help &> /dev/null \
  52. && tar --lzma -xvf "$full_path" \
  53. || lzcat "$full_path" | tar xvf - ;;
  54. (*.tar.zst|*.tzst)
  55. tar --zstd --help &> /dev/null \
  56. && tar --zstd -xvf "$full_path" \
  57. || zstdcat "$full_path" | tar xvf - ;;
  58. (*.tar) tar xvf "$full_path" ;;
  59. (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$full_path" ;;
  60. (*.tar.lz4) lz4 -c -d "$full_path" | tar xvf - ;;
  61. (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$full_path" ;;
  62. (*.gz) (( $+commands[pigz] )) && pigz -cdk "$full_path" > "${file:t:r}" || gunzip -ck "$full_path" > "${file:t:r}" ;;
  63. (*.bz2) (( $+commands[pbzip2] )) && pbzip2 -d "$full_path" || bunzip2 "$full_path" ;;
  64. (*.xz) unxz "$full_path" ;;
  65. (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;;
  66. (*.lz4) lz4 -d "$full_path" ;;
  67. (*.lzma) unlzma "$full_path" ;;
  68. (*.z) uncompress "$full_path" ;;
  69. (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$full_path" ;;
  70. (*.rar) unrar x -ad "$full_path" ;;
  71. (*.rpm)
  72. rpm2cpio "$full_path" | cpio --quiet -id ;;
  73. (*.7z) 7za x "$full_path" ;;
  74. (*.deb)
  75. command mkdir -p "control" "data"
  76. ar vx "$full_path" > /dev/null
  77. builtin cd -q control; extract ../control.tar.*
  78. builtin cd -q ../data; extract ../data.tar.*
  79. builtin cd -q ..; command rm *.tar.* debian-binary ;;
  80. (*.zst) unzstd --stdout "$full_path" > "${file:t:r}" ;;
  81. (*.cab|*.exe) cabextract "$full_path" ;;
  82. (*.cpio|*.obscpio) cpio -idmvF "$full_path" ;;
  83. (*.zpaq) zpaq x "$full_path" ;;
  84. (*.zlib) zlib-flate -uncompress < "$full_path" > "${file:r}" ;;
  85. (*)
  86. echo "extract: '$file' cannot be extracted" >&2
  87. success=1 ;;
  88. esac
  89. (( success = success > 0 ? success : $? ))
  90. (( success == 0 && remove_archive == 0 )) && command rm "$full_path"
  91. shift
  92. # Go back to original working directory
  93. builtin cd -q "$pwd"
  94. # If content of extract dir is a single directory, move its contents up
  95. # Glob flags:
  96. # - D: include files starting with .
  97. # - N: no error if directory is empty
  98. # - Y2: at most give 2 files
  99. local -a content
  100. content=("${extract_dir}"/*(DNY2))
  101. if [[ ${#content} -eq 1 && -e "${content[1]}" ]]; then
  102. # The extracted file/folder (${content[1]}) may have the same name as $extract_dir
  103. # If so, we need to rename it to avoid conflicts in a 3-step process
  104. #
  105. # 1. Move and rename the extracted file/folder to a temporary random name
  106. # 2. Delete the empty folder
  107. # 3. Rename the extracted file/folder to the original name
  108. if [[ "${content[1]:t}" == "$extract_dir" ]]; then
  109. # =(:) gives /tmp/zsh<random>, with :t it gives zsh<random>
  110. local tmp_name==(:); tmp_name="${tmp_name:t}"
  111. command mv "${content[1]}" "$tmp_name" \
  112. && command rmdir "$extract_dir" \
  113. && command mv "$tmp_name" "$extract_dir"
  114. # Otherwise, if the extracted folder name already exists in the current
  115. # directory (because of a previous file / folder), keep the extract_dir
  116. elif [[ ! -e "${content[1]:t}" ]]; then
  117. command mv "${content[1]}" . \
  118. && command rmdir "$extract_dir"
  119. fi
  120. elif [[ ${#content} -eq 0 ]]; then
  121. command rmdir "$extract_dir"
  122. fi
  123. done
  124. }