]> src.twobees.de Git - dotfiles.git/blob - stow/oh-my-zsh/.oh-my-zsh/plugins/universalarchive/universalarchive.plugin.zsh
...
[dotfiles.git] / stow / oh-my-zsh / .oh-my-zsh / plugins / universalarchive / universalarchive.plugin.zsh
1 function ua() {
2   local usage=\
3 "Archive files and directories using a given compression algorithm.
4
5 Usage:   $0 <format> <files>
6 Example: $0 tbz PKGBUILD
7
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."
11
12   if [[ $# -lt 2 ]]; then
13     print -u2 -- "$usage"
14     return 1
15   fi
16
17   local ext="$1"
18   local input="${2:a}"
19
20   shift
21
22   if [[ ! -e "$input" ]]; then
23     print -u2 -- "$input not found"
24     return 1
25   fi
26
27   # generate output file name
28   local output
29   if [[ $# -gt 1 ]]; then
30     output="${input:h:t}"
31   elif [[ -f "$input" ]]; then
32     output="${input:r:t}"
33   elif [[ -d "$input" ]]; then
34     output="${input:t}"
35   fi
36
37   # if output file exists, generate a random name
38   if [[ -f "${output}.${ext}" ]]; then
39     output=$(mktemp "${output}_XXX") && rm "$output" || return 1
40   fi
41
42   # add extension
43   output="${output}.${ext}"
44
45   # safety check
46   if [[ -f "$output" ]]; then
47     print -u2 -- "output file '$output' already exists. Aborting"
48     return 1
49   fi
50
51   case "$ext" in
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 ;;
69   esac
70 }