zimfw/modules/archive/functions/archive

59 lines
2.2 KiB
Plaintext

#
# Creates archive files
#
local archive_name dir_to_archive _gzip_bin _bzip2_bin
if (( $# != 2 )); then
cat >&2 <<EOF
usage: $0 [archive_name.zip] [/path/to/include/into/archive]
Where 'archive.zip' uses any of the following extensions:
.tar.gz, .tar.bz2, .tar.xz, .tar.lzma, .tar, .zip, .rar, .7z
There is no '-v' switch; all operations are verbose.
EOF
return 1
fi
# we are quitting (above) if there are not exactly 2 vars,
# so we don't need any argc check here.
# strip the path, just in case one is provided for some reason
archive_name="${1:t}"
# use absolute paths, and follow symlinks
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
# here, we check for dropin/multi-threaded replacements
if (( $+commands[pigz] )); then
_gzip_bin='pigz'
else
_gzip_bin='gzip'
fi
if (( $+commands[pbzip2] )); then
_bzip2_bin='pbzip2'
else
_bzip2_bin='bzip2'
fi
case "${archive_name}" in
(*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program="${_gzip_bin}" "${dir_to_archive}" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program="${_bzip2_bin}" "${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}" ;;
(*.gz) print "${0}: .gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;;
(*.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