extract.plugin.zsh 4.7 KB

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