lsarchive 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #
  2. # Lists the contents of archives.
  3. #
  4. # Authors:
  5. # Sorin Ionescu <sorin.ionescu@gmail.com>
  6. #
  7. # function lsarchive {
  8. local verbose
  9. if (( $# == 0 )); then
  10. cat >&2 <<EOF
  11. usage: $0 [-option] [file ...]
  12. options:
  13. -v, --verbose verbose archive listing
  14. Report bugs to <sorin.ionescu@gmail.com>.
  15. EOF
  16. fi
  17. if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then
  18. verbose=0
  19. shift
  20. fi
  21. while (( $# > 0 )); do
  22. if [[ ! -s "$1" ]]; then
  23. print "$0: file not valid: $1" >&2
  24. shift
  25. continue
  26. fi
  27. case "$1:l" in
  28. (*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
  29. (*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
  30. (*.tar.xz|*.txz) tar --xz --help &> /dev/null \
  31. && tar --xz -t${verbose:+v}f "$1" \
  32. || xzcat "$1" | tar t${verbose:+v}f - ;;
  33. (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
  34. && tar --lzma -t${verbose:+v}f "$1" \
  35. || lzcat "$1" | tar x${verbose:+v}f - ;;
  36. (*.tar.zst|*.tzst) tar -I zstd -t${verbose:+v}f "$1" ;;
  37. (*.tar) tar t${verbose:+v}f "$1" ;;
  38. (*.zip|*.jar) unzip -l${verbose:+v} "$1" ;;
  39. (*.rar) ( (( $+commands[unrar] )) \
  40. && unrar ${${verbose:+v}:-l} "$1" ) \
  41. || ( (( $+commands[rar] )) \
  42. && rar ${${verbose:+v}:-l} "$1" ) \
  43. || lsar ${verbose:+-l} "$1" ;;
  44. (*.7z) 7za l "$1" ;;
  45. (*)
  46. print "$0: cannot list: $1" >&2
  47. success=1
  48. ;;
  49. esac
  50. shift
  51. done
  52. # }