extract.plugin.zsh 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. case "${file:l}" in
  27. (*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$file" | tar xv } || tar zxvf "$file" ;;
  28. (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$file" ;;
  29. (*.tar.xz|*.txz)
  30. tar --xz --help &> /dev/null \
  31. && tar --xz -xvf "$file" \
  32. || xzcat "$file" | tar xvf - ;;
  33. (*.tar.zma|*.tlz)
  34. tar --lzma --help &> /dev/null \
  35. && tar --lzma -xvf "$file" \
  36. || lzcat "$file" | tar xvf - ;;
  37. (*.tar.zst|*.tzst)
  38. tar --zstd --help &> /dev/null \
  39. && tar --zstd -xvf "$file" \
  40. || zstdcat "$file" | tar xvf - ;;
  41. (*.tar) tar xvf "$file" ;;
  42. (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$file" ;;
  43. (*.tar.lz4) lz4 -c -d "$file" | tar xvf - ;;
  44. (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$file" ;;
  45. (*.gz) (( $+commands[pigz] )) && pigz -dk "$file" || gunzip -k "$file" ;;
  46. (*.bz2) bunzip2 "$file" ;;
  47. (*.xz) unxz "$file" ;;
  48. (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$file" ;;
  49. (*.lz4) lz4 -d "$file" ;;
  50. (*.lzma) unlzma "$file" ;;
  51. (*.z) uncompress "$file" ;;
  52. (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$file" -d "$extract_dir" ;;
  53. (*.rar) unrar x -ad "$file" ;;
  54. (*.rpm)
  55. command mkdir -p "$extract_dir" && builtin cd -q "$extract_dir" \
  56. && rpm2cpio "$full_path" | cpio --quiet -id ;;
  57. (*.7z) 7za x "$file" ;;
  58. (*.deb)
  59. command mkdir -p "$extract_dir/control" "$extract_dir/data"
  60. builtin cd -q "$extract_dir"; ar vx "$full_path" > /dev/null
  61. builtin cd -q control; extract ../control.tar.*
  62. builtin cd -q ../data; extract ../data.tar.*
  63. builtin cd -q ..; command rm *.tar.* debian-binary ;;
  64. (*.zst) unzstd "$file" ;;
  65. (*.cab) cabextract -d "$extract_dir" "$file" ;;
  66. (*.cpio) cpio -idmvF "$file" ;;
  67. (*)
  68. echo "extract: '$file' cannot be extracted" >&2
  69. success=1 ;;
  70. esac
  71. (( success = success > 0 ? success : $? ))
  72. (( success == 0 && remove_archive == 0 )) && rm "$full_path"
  73. shift
  74. # Go back to original working directory in case we ran cd previously
  75. builtin cd -q "$pwd"
  76. done
  77. }