3 "Archive files and directories using a given compression algorithm.
5 Usage: $0 <format> <files>
6 Example: $0 tbz PKGBUILD
8 Supported archive formats are:
9 7z, bz2, gz, lzma, lzo, rar, tar, tbz (tar.bz2), tgz (tar.gz),
10 tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst."
12 if [[ $# -lt 2 ]]; then
22 if [[ ! -e "$input" ]]; then
23 print -u2 -- "$input not found"
27 # generate output file name
29 if [[ $# -gt 1 ]]; then
31 elif [[ -f "$input" ]]; then
33 elif [[ -d "$input" ]]; then
37 # if output file exists, generate a random name
38 if [[ -f "${output}.${ext}" ]]; then
39 output=$(mktemp "${output}_XXX") && rm "$output" || return 1
43 output="${output}.${ext}"
46 if [[ -f "$output" ]]; then
47 print -u2 -- "output file '$output' already exists. Aborting"
52 7z) 7z u "${output}" "${@}" ;;
53 bz2) bzip2 -vcf "${@}" > "${output}" ;;
54 gz) gzip -vcf "${@}" > "${output}" ;;
55 lzma) lzma -vc -T0 "${@}" > "${output}" ;;
56 lzo) lzop -vc "${@}" > "${output}" ;;
57 rar) rar a "${output}" "${@}" ;;
58 tar) tar -cvf "${output}" "${@}" ;;
59 tbz|tar.bz2) tar -cvjf "${output}" "${@}" ;;
60 tgz|tar.gz) tar -cvzf "${output}" "${@}" ;;
61 tlz|tar.lzma) XZ_OPT=-T0 tar --lzma -cvf "${output}" "${@}" ;;
62 txz|tar.xz) XZ_OPT=-T0 tar -cvJf "${output}" "${@}" ;;
63 tZ|tar.Z) tar -cvZf "${output}" "${@}" ;;
64 xz) xz -vc -T0 "${@}" > "${output}" ;;
65 Z) compress -vcf "${@}" > "${output}" ;;
66 zip) zip -rull "${output}" "${@}" ;;
67 zst) zstd -c -T0 "${@}" > "${output}" ;;
68 *) print -u2 -- "$usage"; return 1 ;;