1
0
Fork 0
mirror of synced 2024-12-27 08:23:21 -05:00
zimfw/modules/archive/functions/archive
Eric Nielsen 2e3ba7996b
[archive] Allow archives in any directory
Current code will create an archive in the current directory with:

    % archive ../test.tar.gz test.*

or will complain that the following archive doesn't exist in the current
directory (given it actually exists in the parent one):

    % unarchive ../test.tar.gz

Fix that and allow archives in any directory.

Other changes:

* Use `<required_param>` instead of `[required_param]` in the usage text
* Don't explictly check if archive exists in `unarchive`, but let the
  respective tool fail with its own message

Closes #312
2018-11-24 22:45:08 -05:00

34 lines
1.5 KiB
Bash

# vim:et sts=2 sw=2 ft=zsh
#
# Creates archive files
#
if (( # < 2 )); then
print "usage: ${0} <archive_name.ext> <file>..." >&2
return 1
fi
# we are quitting (above) if there are less than 2 vars,
# so we don't need any argc check here.
local archive_name="${1}"
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 -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" ;;
(*.lzma) print "${0}: .lzma is only useful for single files, and does not capture permissions. Use .tar.lzma" ;;
(*) print "${0}: unknown archive type: ${archive_name}" ;;
esac