1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-14 08:19:03 -08:00
rc/sh/lsarchive

69 lines
1.9 KiB
Text
Raw Normal View History

#!/usr/bin/env zsh
# compat: -ash -bash -dash +zsh
2013-06-28 05:22:14 -07:00
lsarchive() { ### @- list the contents of an archive file in one of many formats.
### borrowed from [prezto.](https://github.com/sorin-ionescu/prezto)
#
# Lists the contents of archives.
#
# Authors:
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
2019-05-27 11:19:12 -07:00
local verbose=
2013-06-28 05:22:14 -07:00
if (( $# == 0 )); then
cat >&2 <<EOF
2013-06-28 05:22:14 -07:00
usage: $0 [-option] [file ...]
options:
-v, --verbose verbose archive listing
Report bugs to <sorin.ionescu@gmail.com>.
EOF
return 1
fi
if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then
verbose=0
shift
fi
while (( $# > 0 )); do
if [[ ! -s "$1" ]]; then
print "$0: file not valid: $1" >&2
shift
continue
fi
case "$1:l" in
(*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
(*.tar.xz|*.txz) tar --xz --help &> /dev/null \
&& tar --xz -t${verbose:+v}f "$1" \
|| xzcat "$1" | tar t${verbose:+v}f - ;;
(*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
&& tar --lzma -t${verbose:+v}f "$1" \
|| lzcat "$1" | tar x${verbose:+v}f - ;;
(*.tar.zst|*.tzst) tar -I zstd -t${verbose:+v}f "$1" ;;
(*.tar) tar t${verbose:+v}f "$1" ;;
(*.zip|*.jar) unzip -l${verbose:+v} "$1" ;;
(*.rar) ( (( $+commands[unrar] )) \
&& unrar ${${verbose:+v}:-l} "$1" ) \
|| ( (( $+commands[rar] )) \
&& rar ${${verbose:+v}:-l} "$1" ) \
|| lsar ${verbose:+-l} "$1" ;;
(*.7z) 7za l "$1" ;;
(*)
print "$0: cannot list: $1" >&2
success=1
;;
esac
shift
done
}
[ -n "${preload+-}" ] || lsarchive "$@"