1
0
Fork 0
mirror of synced 2024-11-27 01:35:35 -05:00
zimfw/modules/archive/functions/unarchive
Eric Nielsen 1c23ea1604 [archive] Support archive of multiple files
not just one directory.

Also use shorter versions of the `tar` parameters, since we were using a
mixture of the short and long ones among `archive` and `unarchive`.

About not using `$` inside `(( ))`, this is what the the section
ARITHMETIC EVALUATION in zshmisc(1) says:

> Named parameters and subscripted arrays can be referenced by name
> within an arithmetic expression without using the parameter expansion
> syntax.

And according to http://www.bash2zsh.com/zsh_refcard/refcard.pdf:

> `var` (does not require `$` in front unless some substitution e.g.
> `${#var}` is needed, `$` is error if `var` is to be modified)

Closes #308
2018-11-14 20:32:37 -05:00

39 lines
1.3 KiB
Text

#
# Unarchives files
#
if (( # != 1 )); then
print "usage: ${0} [archive.ext]" >&2
return 1
fi
if [[ ! -s ${1} ]]; then
print "archive \"${1}\" was not found or has size 0." >&2
return 1
fi
# strip the path, just in case one is provided for some reason
local archive_name="${1:t}"
# using unpigz/pbunzip2 provides little to decompression time; the benefit is mainly in compression time.
# setting it as an alias in the init.zsh file should be sufficient here.
case "${archive_name}" in
(*.tar.gz|*.tgz) tar -xvzf "${archive_name}" ;;
(*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -xvjf "${archive_name}" ;;
(*.tar.xz|*.txz) tar -J --help &>/dev/null && tar -xvJf "${archive_name}" \
|| xzcat "${archive_name}" | tar xvf - ;;
(*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && tar --lzma -xvf "${archive_name}" \
|| lzcat "${archive_name}" | tar xvf - ;;
(*.tar) tar xvf "${archive_name}" ;;
(*.gz) gunzip "${archive_name}" ;;
(*.bz|*.bz2) bunzip2 "${archive_name}" ;;
(*.xz) unxz "${archive_name}" ;;
(*.lzma) unlzma "${archive_name}" ;;
(*.Z) uncompress "${archive_name}" ;;
(*.zip) unzip "${archive_name}";;
(*.rar) (( $+{commands[unrar]} )) && unrar x -ad "${archive_name}" \
|| rar x -ad "${archive_name}" ;;
(*.7z|*.001) 7za x "${archive_name}" ;;
(*) print "${0}: unknown archive type: ${archive_name}" ;;
esac