summaryrefslogtreecommitdiff
path: root/plugins/universalarchive/universalarchive.plugin.zsh
diff options
context:
space:
mode:
authorTuowen Zhao <ztuowen@gmail.com>2021-01-18 13:16:40 -0700
committerTuowen Zhao <ztuowen@gmail.com>2021-01-18 13:16:40 -0700
commitc6e754a3119b7273a57f0cfed38e85303662d26b (patch)
treec50ae3c6b0271ec88e964d3a57199ea37e63bf0b /plugins/universalarchive/universalarchive.plugin.zsh
parentfb45741fc1dbd40dd2be72bc35a28c6ee8f3f7a5 (diff)
parentefcbd9f3480a28ec69c607c46adcbfd8d230ac9f (diff)
downloadzsh-c6e754a3119b7273a57f0cfed38e85303662d26b.tar.gz
zsh-c6e754a3119b7273a57f0cfed38e85303662d26b.tar.bz2
zsh-c6e754a3119b7273a57f0cfed38e85303662d26b.zip
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'plugins/universalarchive/universalarchive.plugin.zsh')
-rw-r--r--plugins/universalarchive/universalarchive.plugin.zsh70
1 files changed, 70 insertions, 0 deletions
diff --git a/plugins/universalarchive/universalarchive.plugin.zsh b/plugins/universalarchive/universalarchive.plugin.zsh
new file mode 100644
index 000000000..b287c22b2
--- /dev/null
+++ b/plugins/universalarchive/universalarchive.plugin.zsh
@@ -0,0 +1,70 @@
+function ua() {
+ local usage=\
+"Archive files and directories using a given compression algorithm.
+
+Usage: $0 <format> <files>
+Example: $0 tbz PKGBUILD
+
+Supported archive formats are:
+7z, bz2, gz, lzma, lzo, rar, tar, tbz (tar.bz2), tgz (tar.gz),
+tlz (tar.lzma), txz (tar.xz), tZ (tar.Z), xz, Z, zip, and zst."
+
+ if [[ $# -lt 2 ]]; then
+ print -u2 -- "$usage"
+ return 1
+ fi
+
+ local ext="$1"
+ local input="${2:a}"
+
+ shift
+
+ if [[ ! -e "$input" ]]; then
+ print -u2 -- "$input not found"
+ return 1
+ fi
+
+ # generate output file name
+ local output
+ if [[ $# -gt 1 ]]; then
+ output="${input:h:t}"
+ elif [[ -f "$input" ]]; then
+ output="${input:r:t}"
+ elif [[ -d "$input" ]]; then
+ output="${input:t}"
+ fi
+
+ # if output file exists, generate a random name
+ if [[ -f "${output}.${ext}" ]]; then
+ output=$(mktemp "${output}_XXX") && rm "$output" || return 1
+ fi
+
+ # add extension
+ output="${output}.${ext}"
+
+ # safety check
+ if [[ -f "$output" ]]; then
+ print -u2 -- "output file '$output' already exists. Aborting"
+ return 1
+ fi
+
+ case "$ext" in
+ 7z) 7z u "${output}" "${@}" ;;
+ bz2) bzip2 -vcf "${@}" > "${output}" ;;
+ gz) gzip -vcf "${@}" > "${output}" ;;
+ lzma) lzma -vc -T0 "${@}" > "${output}" ;;
+ lzo) lzop -vc "${@}" > "${output}" ;;
+ rar) rar a "${output}" "${@}" ;;
+ tar) tar -cvf "${output}" "${@}" ;;
+ tbz|tar.bz2) tar -cvjf "${output}" "${@}" ;;
+ tgz|tar.gz) tar -cvzf "${output}" "${@}" ;;
+ tlz|tar.lzma) XZ_OPT=-T0 tar --lzma -cvf "${output}" "${@}" ;;
+ txz|tar.xz) XZ_OPT=-T0 tar -cvJf "${output}" "${@}" ;;
+ tZ|tar.Z) tar -cvZf "${output}" "${@}" ;;
+ xz) xz -vc -T0 "${@}" > "${output}" ;;
+ Z) compress -vcf "${@}" > "${output}" ;;
+ zip) zip -rull "${output}" "${@}" ;;
+ zst) zstd -c -T0 "${@}" > "${output}" ;;
+ *) print -u2 -- "$usage"; return 1 ;;
+ esac
+}