extract.plugin.zsh 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # ------------------------------------------------------------------------------
  2. # FILE: extract.plugin.zsh
  3. # DESCRIPTION: oh-my-zsh plugin file.
  4. # AUTHOR: Sorin Ionescu (sorin.ionescu@gmail.com)
  5. # VERSION: 1.0.1
  6. # ------------------------------------------------------------------------------
  7. function extract() {
  8. local remove_archive
  9. local success
  10. local file_name
  11. local extract_dir
  12. if (( $# == 0 )); then
  13. echo "Usage: extract [-option] [file ...]"
  14. echo
  15. echo Options:
  16. echo " -r, --remove Remove archive."
  17. echo
  18. echo "Report bugs to <sorin.ionescu@gmail.com>."
  19. fi
  20. remove_archive=1
  21. if [[ "$1" == "-r" ]] || [[ "$1" == "--remove" ]]; then
  22. remove_archive=0
  23. shift
  24. fi
  25. while (( $# > 0 )); do
  26. if [[ ! -f "$1" ]]; then
  27. echo "extract: '$1' is not a valid file" 1>&2
  28. shift
  29. continue
  30. fi
  31. success=0
  32. file_name="$( basename "$1" )"
  33. extract_dir="$( echo "$file_name" | sed "s/\.${1##*.}//g" )"
  34. case "$1" in
  35. (*.tar.gz|*.tgz) [ -z $commands[pigz] ] && tar zxvf "$1" || pigz -dc "$1" | tar xv ;;
  36. (*.tar.bz2|*.tbz|*.tbz2) tar xvjf "$1" ;;
  37. (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
  38. && tar --xz -xvf "$1" \
  39. || xzcat "$1" | tar xvf - ;;
  40. (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
  41. && tar --lzma -xvf "$1" \
  42. || lzcat "$1" | tar xvf - ;;
  43. (*.tar) tar xvf "$1" ;;
  44. (*.gz) [ -z $commands[pigz] ] && gunzip "$1" || pigz -d "$1" ;;
  45. (*.bz2) bunzip2 "$1" ;;
  46. (*.xz) unxz "$1" ;;
  47. (*.lzma) unlzma "$1" ;;
  48. (*.Z) uncompress "$1" ;;
  49. (*.zip|*.war|*.jar|*.sublime-package|*.ipsw|*.xpi|*.apk) unzip "$1" -d $extract_dir ;;
  50. (*.rar) unrar x -ad "$1" ;;
  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; tar xzvf ../data.tar.gz
  58. cd ..; rm *.tar.gz debian-binary
  59. cd ..
  60. ;;
  61. (*)
  62. echo "extract: '$1' cannot be extracted" 1>&2
  63. success=1
  64. ;;
  65. esac
  66. (( success = $success > 0 ? $success : $? ))
  67. (( $success == 0 )) && (( $remove_archive == 0 )) && rm "$1"
  68. shift
  69. done
  70. }
  71. alias x=extract