extract.plugin.zsh 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 extract_dir="${1:t:r}"
  25. local file="$1" full_path="${1:A}"
  26. # Create an extraction directory based on the file name
  27. command mkdir -p "$extract_dir"
  28. builtin cd -q "$extract_dir"
  29. case "${file:l}" in
  30. (*.tar.gz|*.tgz)
  31. (( $+commands[pigz] )) && { tar -I pigz -xvf "$full_path" } || tar zxvf "$full_path" ;;
  32. (*.tar.bz2|*.tbz|*.tbz2)
  33. (( $+commands[pbzip2] )) && { tar -I pbzip2 -xvf "$full_path" } || tar xvjf "$full_path" ;;
  34. (*.tar.xz|*.txz)
  35. (( $+commands[pixz] )) && { tar -I pixz -xvf "$full_path" } || {
  36. tar --xz --help &> /dev/null \
  37. && tar --xz -xvf "$full_path" \
  38. || xzcat "$full_path" | tar xvf - } ;;
  39. (*.tar.zma|*.tlz)
  40. tar --lzma --help &> /dev/null \
  41. && tar --lzma -xvf "$full_path" \
  42. || lzcat "$full_path" | tar xvf - ;;
  43. (*.tar.zst|*.tzst)
  44. tar --zstd --help &> /dev/null \
  45. && tar --zstd -xvf "$full_path" \
  46. || zstdcat "$full_path" | tar xvf - ;;
  47. (*.tar) tar xvf "$full_path" ;;
  48. (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$full_path" ;;
  49. (*.tar.lz4) lz4 -c -d "$full_path" | tar xvf - ;;
  50. (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$full_path" ;;
  51. (*.gz) (( $+commands[pigz] )) && pigz -dk "$full_path" || gunzip -k "$full_path" ;;
  52. (*.bz2) bunzip2 "$full_path" ;;
  53. (*.xz) unxz "$full_path" ;;
  54. (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$full_path" ;;
  55. (*.lz4) lz4 -d "$full_path" ;;
  56. (*.lzma) unlzma "$full_path" ;;
  57. (*.z) uncompress "$full_path" ;;
  58. (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$full_path" ;;
  59. (*.rar) unrar x -ad "$full_path" ;;
  60. (*.rpm)
  61. rpm2cpio "$full_path" | cpio --quiet -id ;;
  62. (*.7z) 7za x "$full_path" ;;
  63. (*.deb)
  64. command mkdir -p "control" "data"
  65. ar vx "$full_path" > /dev/null
  66. builtin cd -q control; extract ../control.tar.*
  67. builtin cd -q ../data; extract ../data.tar.*
  68. builtin cd -q ..; command rm *.tar.* debian-binary ;;
  69. (*.zst) unzstd "$full_path" ;;
  70. (*.cab) cabextract "$full_path" ;;
  71. (*.cpio|*.obscpio) cpio -idmvF "$full_path" ;;
  72. (*.zpaq) zpaq x "$full_path" ;;
  73. (*)
  74. echo "extract: '$file' cannot be extracted" >&2
  75. success=1 ;;
  76. esac
  77. (( success = success > 0 ? success : $? ))
  78. (( success == 0 && remove_archive == 0 )) && command rm "$full_path"
  79. shift
  80. # Go back to original working directory
  81. builtin cd -q "$pwd"
  82. # If content of extract dir is a single directory, move its contents up
  83. # Glob flags:
  84. # - D: include files starting with .
  85. # - N: no error if directory is empty
  86. # - Y2: at most give 2 files
  87. local -a content
  88. content=("${extract_dir}"/*(DNY2))
  89. if [[ ${#content} -eq 1 && -d "${content[1]}" ]]; then
  90. command mv -f "${content[1]}" .
  91. command rmdir "$extract_dir"
  92. elif [[ ${#content} -eq 0 ]]; then
  93. command rmdir "$extract_dir"
  94. fi
  95. done
  96. }