archive: add support for archiving multiple files
This commit is contained in:
parent
60b0910f6d
commit
40f1701530
|
@ -2,40 +2,47 @@
|
||||||
# Creates archive files
|
# Creates archive files
|
||||||
#
|
#
|
||||||
|
|
||||||
if (( ${#} != 2 )); then
|
archive() { # compress a file or folder
|
||||||
print "usage: ${0} [archive_name.ext] [/path/to/include/in/archive]" >&2
|
local name="${1%.[^.]*}"
|
||||||
return 1
|
local type="${1##*.}"
|
||||||
fi
|
local input="$2"
|
||||||
|
|
||||||
# we are quitting (above) if there are not exactly 2 vars,
|
if [ "$#" -eq 1 ]; then
|
||||||
# so we don't need any argc check here.
|
echo "archive(): archive a file or directory."
|
||||||
|
echo "Usage: archive <archive> <files>"
|
||||||
|
echo "Error: Please give a valid file or directory name!"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
# strip the path, just in case one is provided for some reason
|
if [ -f "${name}.${type}" ]; then
|
||||||
local archive_name="${1:t}"
|
echo "archive(): file ${name}.${type} already exists."
|
||||||
# use absolute paths, and follow symlinks
|
name=$(mktemp --dry-run "./${name}_XXX.${type}")
|
||||||
local dir_to_archive="${2}"
|
echo "archive(): using ${name}.${type} instead."
|
||||||
|
fi
|
||||||
|
|
||||||
# if the directory doesn't exist, quit. Nothing to archive
|
case "$type" in
|
||||||
if [[ ! -e "${dir_to_archive}" ]]; then
|
tar) shift; tar cvf "${name}.tar" "${@%%}" ;;
|
||||||
print "${0}: file or directory not valid: ${dir_to_archive}" >&2
|
tbz|tar.bz) shift; tar cvjf "${name}.tar.bz2" "${@%}" ;;
|
||||||
return 1
|
tbz2|tar.bz2) shift; tar cvjf "${name}.tar.bz2" "${@%%}" ;;
|
||||||
fi
|
txz|tar.xz) shift; env XZ_OPT=-T0 tar cvJf "${name}.tar.xz" "${@%%}" ;;
|
||||||
|
tgz|tar.gz) shift; tar cvzf "${name}.tar.gz" "${@%%}" ;;
|
||||||
# pigz and pbzip2 are aliased in the init.zsh file. This provides a significant speedup, resulting in a
|
tZ|tar.Z) shift; tar cvZf "${name}.tar.Z" "${@%%}" ;;
|
||||||
# near-liner decrease in compression time based on on the number of available cores.
|
gz|gzip) shift; gzip -vcf "${@%%}" > "${name}.gz" ;;
|
||||||
|
bz|bzip) shift; bzip2 -vcf "${@%%}" > "${name}.bz" ;;
|
||||||
case "${archive_name}" in
|
bz2|bzip2) shift; bzip2 -vcf "${@%%}" > "${name}.bz2" ;;
|
||||||
(*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program="gzip" "${dir_to_archive}" ;;
|
Z|compress) shift; compress -vcf "${@%%}" > "${name}.Z" ;;
|
||||||
(*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program="bzip2" "${dir_to_archive}" ;;
|
zip) shift; zip -rull "${name}.zip" "${@%%}" ;;
|
||||||
(*.tar.xz|*.txz) tar --xz --help &>/dev/null && tar -cvJf "${archive_name}" "${dir_to_archive}" ;;
|
7z|7zip) shift; 7z u "${name}.7z" "${@%%}" ;;
|
||||||
(*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;;
|
rar) shift; rar a "${name}.rar" "${@%%}" ;;
|
||||||
(*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;;
|
lzo) shift; lzop -vc "${@%%}" > "${name}.lzo" ;;
|
||||||
(*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;;
|
xz) shift; xz -vc -T0 "${@%%}" > "${name}.xz" ;;
|
||||||
(*.rar) rar a "${archive_name}" "${dir_to_archive}" ;;
|
lzma) shift; lzma -vc "${@%%}" > "${name}.lzma" ;;
|
||||||
(*.7z) 7za a "${archive_name}" "${dir_to_archive}" ;;
|
*) echo "archive(): archive a file or directory."
|
||||||
(*.gz) print "${0}: .gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;;
|
echo "Usage: archive <archive> <files>"
|
||||||
(*.bz2) print "${0}: .bzip2 is only useful for single files, and does not capture permissions. Use .tar.bz2" ;;
|
echo "Example: archive file.tbz2 file.txt file.png"
|
||||||
(*.xz) print "${0}: .xz is only useful for single files, and does not capture permissions. Use .tar.xz" ;;
|
echo "Please specify archive and files."
|
||||||
(*.lzma) print "${0}: .lzma is only useful for single files, and does not capture permissions. Use .tar.lzma" ;;
|
echo "Valid archive file extensions are:"
|
||||||
(*) print "${0}: unknown archive type: ${archive_name}" ;;
|
echo "tar, tbz2, txz, tgz, tZ, gz, bz2, Z,"
|
||||||
esac
|
echo "zip, 7z, rar, lzo, xz, and lzma." ;;
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue