extract.plugin.zsh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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.
  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" 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) tar xvf "$1" ;;
  38. (*.gz) (( $+commands[pigz] )) && pigz -d "$1" || gunzip "$1" ;;
  39. (*.bz2) bunzip2 "$1" ;;
  40. (*.xz) unxz "$1" ;;
  41. (*.lzma) unlzma "$1" ;;
  42. (*.Z) uncompress "$1" ;;
  43. (*.zip|*.war|*.jar|*.sublime-package|*.ipsw|*.xpi|*.apk) unzip "$1" -d $extract_dir ;;
  44. (*.rar) unrar x -ad "$1" ;;
  45. (*.7z) 7za x "$1" ;;
  46. (*.deb)
  47. mkdir -p "$extract_dir/control"
  48. mkdir -p "$extract_dir/data"
  49. cd "$extract_dir"; ar vx "../${1}" > /dev/null
  50. cd control; tar xzvf ../control.tar.gz
  51. cd ../data; extract ../data.tar.*
  52. cd ..; rm *.tar.* debian-binary
  53. cd ..
  54. ;;
  55. (*)
  56. echo "extract: '$1' cannot be extracted" >&2
  57. success=1
  58. ;;
  59. esac
  60. (( success = $success > 0 ? $success : $? ))
  61. (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
  62. shift
  63. done
  64. }