extract.plugin.zsh 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. alias x=extract
  2. extract() {
  3. local remove_archive
  4. local success
  5. local extract_dir
  6. if (( $# == 0 )); then
  7. cat <<-'EOF' >&2
  8. Usage: extract [-option] [file ...]
  9. Options:
  10. -r, --remove Remove archive after unpacking.
  11. EOF
  12. fi
  13. remove_archive=1
  14. if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
  15. remove_archive=0
  16. shift
  17. fi
  18. while (( $# > 0 )); do
  19. if [[ ! -f "$1" ]]; then
  20. echo "extract: '$1' is not a valid file" >&2
  21. shift
  22. continue
  23. fi
  24. success=0
  25. extract_dir="${1:t:r}"
  26. case "${1:l}" in
  27. (*.tar.gz|*.tgz) (( $+commands[pigz] )) && { pigz -dc "$1" | tar xv } || tar zxvf "$1" ;;
  28. (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
  29. (*.tar.xz|*.txz)
  30. tar --xz --help &> /dev/null \
  31. && tar --xz -xvf "$1" \
  32. || xzcat "$1" | tar xvf - ;;
  33. (*.tar.zma|*.tlz)
  34. tar --lzma --help &> /dev/null \
  35. && tar --lzma -xvf "$1" \
  36. || lzcat "$1" | tar xvf - ;;
  37. (*.tar.zst|*.tzst)
  38. tar --zstd --help &> /dev/null \
  39. && tar --zstd -xvf "$1" \
  40. || zstdcat "$1" | tar xvf - ;;
  41. (*.tar) tar xvf "$1" ;;
  42. (*.tar.lz) (( $+commands[lzip] )) && tar xvf "$1" ;;
  43. (*.tar.lz4) lz4 -c -d "$1" | tar xvf - ;;
  44. (*.tar.lrz) (( $+commands[lrzuntar] )) && lrzuntar "$1" ;;
  45. (*.gz) (( $+commands[pigz] )) && pigz -dk "$1" || gunzip -k "$1" ;;
  46. (*.bz2) bunzip2 "$1" ;;
  47. (*.xz) unxz "$1" ;;
  48. (*.lrz) (( $+commands[lrunzip] )) && lrunzip "$1" ;;
  49. (*.lz4) lz4 -d "$1" ;;
  50. (*.lzma) unlzma "$1" ;;
  51. (*.z) uncompress "$1" ;;
  52. (*.zip|*.war|*.jar|*.ear|*.sublime-package|*.ipa|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d $extract_dir ;;
  53. (*.rar) unrar x -ad "$1" ;;
  54. (*.rpm) mkdir "$extract_dir" && cd "$extract_dir" && rpm2cpio "../$1" | cpio --quiet -id && cd .. ;;
  55. (*.7z) 7za x "$1" ;;
  56. (*.deb)
  57. mkdir -p "$extract_dir/control"
  58. mkdir -p "$extract_dir/data"
  59. cd "$extract_dir"; ar vx "../${1}" > /dev/null
  60. cd control; tar xzvf ../control.tar.gz
  61. cd ../data; extract ../data.tar.*
  62. cd ..; rm *.tar.* debian-binary
  63. cd ..
  64. ;;
  65. (*.zst) unzstd "$1" ;;
  66. (*)
  67. echo "extract: '$1' cannot be extracted" >&2
  68. success=1
  69. ;;
  70. esac
  71. (( success = $success > 0 ? $success : $? ))
  72. (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
  73. shift
  74. done
  75. }