extract.plugin.zsh 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. (*.gz) (( $+commands[pigz] )) && pigz -dk "$1" || gunzip -k "$1" ;;
  44. (*.bz2) bunzip2 "$1" ;;
  45. (*.xz) unxz "$1" ;;
  46. (*.lzma) unlzma "$1" ;;
  47. (*.z) uncompress "$1" ;;
  48. (*.zip|*.war|*.jar|*.sublime-package|*.ipsw|*.xpi|*.apk|*.aar|*.whl) unzip "$1" -d $extract_dir ;;
  49. (*.rar) unrar x -ad "$1" ;;
  50. (*.rpm) mkdir "$extract_dir" && cd "$extract_dir" && rpm2cpio "../$1" | cpio --quiet -id && cd .. ;;
  51. (*.7z) 7za x "$1" ;;
  52. (*.deb)
  53. mkdir -p "$extract_dir/control"
  54. mkdir -p "$extract_dir/data"
  55. cd "$extract_dir"; ar vx "../${1}" > /dev/null
  56. cd control; tar xzvf ../control.tar.gz
  57. cd ../data; extract ../data.tar.*
  58. cd ..; rm *.tar.* debian-binary
  59. cd ..
  60. ;;
  61. (*.zst) unzstd "$1" ;;
  62. (*)
  63. echo "extract: '$1' cannot be extracted" >&2
  64. success=1
  65. ;;
  66. esac
  67. (( success = $success > 0 ? $success : $? ))
  68. (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
  69. shift
  70. done
  71. }