[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
This commit is contained in:
Eric Nielsen 2018-11-14 20:32:37 -05:00
parent 2c5e7c02ea
commit 1c23ea1604
2 changed files with 14 additions and 21 deletions

View File

@ -2,37 +2,30 @@
# Creates archive files
#
if (( ${#} != 2 )); then
print "usage: ${0} [archive_name.ext] [/path/to/include/in/archive]" >&2
if (( # < 2 )); then
print "usage: ${0} [archive_name.ext] [file]..." >&2
return 1
fi
# we are quitting (above) if there are not exactly 2 vars,
# we are quitting (above) if there are less than 2 vars,
# so we don't need any argc check here.
# strip the path, just in case one is provided for some reason
local archive_name="${1:t}"
# use absolute paths, and follow symlinks
local dir_to_archive="${2}"
# if the directory doesn't exist, quit. Nothing to archive
if [[ ! -e "${dir_to_archive}" ]]; then
print "${0}: file or directory not valid: ${dir_to_archive}" >&2
return 1
fi
shift
# pigz and pbzip2 are aliased in the init.zsh file. This provides a significant speedup, resulting in a
# near-liner decrease in compression time based on on the number of available cores.
case "${archive_name}" in
(*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program=gzip "${dir_to_archive}" ;;
(*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program=bzip2 "${dir_to_archive}" ;;
(*.tar.xz|*.txz) tar --xz --help &>/dev/null && tar -cvJf "${archive_name}" "${dir_to_archive}" ;;
(*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;;
(*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;;
(*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;;
(*.rar) rar a "${archive_name}" "${dir_to_archive}" ;;
(*.7z) 7za a "${archive_name}" "${dir_to_archive}" ;;
(*.tar.gz|*.tgz) tar -cvzf "${archive_name}" "${@}" ;;
(*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -cvjf "${archive_name}" "${@}" ;;
(*.tar.xz|*.txz) tar -J --help &>/dev/null && tar -cvJf "${archive_name}" "${@}" ;;
(*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && tar --lzma -cvf "${archive_name}" "${@}" ;;
(*.tar) tar -cvf "${archive_name}" "${@}" ;;
(*.zip) zip -r "${archive_name}" "${@}" ;;
(*.rar) rar a "${archive_name}" "${@}" ;;
(*.7z) 7za a "${archive_name}" "${@}" ;;
(*.gz) print "${0}: .gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;;
(*.bz|*.bz2) print "${0}: .bzip2 is only useful for single files, and does not capture permissions. Use .tar.bz2" ;;
(*.xz) print "${0}: .xz is only useful for single files, and does not capture permissions. Use .tar.xz" ;;

View File

@ -2,7 +2,7 @@
# Unarchives files
#
if (( ${#} != 1 )); then
if (( # != 1 )); then
print "usage: ${0} [archive.ext]" >&2
return 1
fi
@ -21,7 +21,7 @@ local archive_name="${1:t}"
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 --xz --help &>/dev/null && tar --xz -xvf "${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 - ;;