add plugin 'universalarchive()' to conveniently compress files

This commit is contained in:
Enno Nagel 2018-05-21 12:27:55 -03:00
commit 1c389ea4e5
3 changed files with 115 additions and 0 deletions

49
plugins/ua/README.md Normal file
View file

@ -0,0 +1,49 @@
# universalarchive plugin
Lets you compress files by a catchy command `ua <format> <files>` into various compression formats;
namely, `<format>` could be `zip`, `xz`, ..
For example, the command
```sh
ua xz *.html
```
will compress all `html` files in directory `folder` into `folder.xz`.
In more detail:
This plug-in defines a function called `universalarchive` that compresses the files you pass it; it supports a wide variety of compression format.
This way you don't have to know what specific command compresses a file, you
just do `universalarchive <archive type> <filename>` and the function takes
care of the rest.
To use it, add `universalarchive` to the plugins array in your zshrc file:
```zsh
plugins=(... universalarchive)
```
## Supported file extensions
| Extension | Description |
|:------------------|:-------------------------------------|
| `7z` | 7zip file |
| `Z` | Z archive (LZW) |
| `bz2` | Bzip2 file |
| `gz` | Gzip file |
| `lzma` | LZMA archive |
| `lzo` | LZO archive |
| `rar` | WinRAR archive |
| `tar` | Tarball |
| `tar.bz2` | Tarball with bzip2 compression |
| `tar.gz` | Tarball with gzip compression |
| `tar.xz` | Tarball with lzma2 compression |
| `tar.zma` | Tarball with lzma compression |
| `tbz` | Tarball with bzip compression |
| `tbz2` | Tarball with bzip2 compression |
| `tgz` | Tarball with gzip compression |
| `tlz` | Tarball with lzma compression |
| `txz` | Tarball with lzma2 compression |
| `xz` | LZMA2 archive |
| `zip` | Zip archive |
See [list of archive formats](https://en.wikipedia.org/wiki/List_of_archive_formats) for more information regarding archive formats.

View file

@ -0,0 +1,7 @@
#compdef universalarchive
#autoload
_arguments \
'(-r --remove)'{-r,--remove}'[Remove archive.]' \
"*::archive file:_files -g '(#i)*.(7z|Z|bz2|gz|lzma|rar|tar|tar.bz2|tar.gz|tar.xz|tar.zma|tbz|tbz2|tgz|tlz|txz|xz|zip)(-.)'" \
&& return 0

View file

@ -0,0 +1,59 @@
alias ua='universalarchive'
alias ax='universalarchive tar.xz '
# exclusively POSIX shell syntax is used
universalarchive() { # compress a file or folder
local file
local parent
local name
local type="$1"
local input="$2"
if [ -f "$input" ]; then
if [ "$#" -eq 2 ]; then
name="${input%.[^.]*}"
else
file="$(realpath "$input")"
parent="${file%/*}"
name="${parent##*/}"
fi
elif [ -d "$input" ]; then
name="${input%/*}"
else
echo "universalarchive(): archive a file or directory."
echo "Usage: ua <archive type> <files>"
echo "Error: Please give a valid file or directory name!"
fi
if [ -f "${name}.${type}" ]; then
name=$(mktemp --dry-run "./${name}_XXX.${type}")
fi
case "$type" in
tar) shift; tar cvf "${name}.tar" "${@%%}" ;;
tbz|tar.bz) shift; tar cvjf "${name}.tar.bz2" "${@%}" ;;
tbz2|tar.bz2) shift; tar cvjf "${name}.tar.bz2" "${@%%}" ;;
txz|tar.xz) shift; env XZ_OPT=-T0 tar cvJf "${name}.tar.xz" "${@%%}" ;;
tgz|tar.gz) shift; tar cvzf "${name}.tar.gz" "${@%%}" ;;
tZ|tar.Z) shift; tar cvZf "${name}.tar.Z" "${@%%}" ;;
gz|gzip) shift; gzip -vcf "${@%%}" > "${name}.gz" ;;
bz|bzip) shift; bzip2 -vcf "${@%%}" > "${name}.bz" ;;
bz2|bzip2) shift; bzip2 -vcf "${@%%}" > "${name}.bz2" ;;
Z|compress) shift; compress -vcf "${@%%}" > "${name}.Z" ;;
zip) shift; zip -rull "${name}.zip" "${@%%}" ;;
7z|7zip) shift; 7z u "${name}.7z" "${@%%}" ;;
rar) shift; rar a "${name}.rar" "${@%%}" ;;
lzo) shift; lzop -vc "${@%%}" > "${name}.lzo" ;;
xz) shift; xz -vc -T0 "${@%%}" > "${name}.xz" ;;
lzma) shift; lzma -vc "${@%%}" > "${name}.lzma" ;;
*) echo "universalarchive(): archive a file or directory."
echo "Usage: ua <archive type> <files>"
echo "Example: ua tbz2 PKGBUILD"
echo "Please specify archive type and files."
echo "Valid archive types are:"
echo "tar, tbz2, txz, tgz, tZ, gz, bz2, Z,"
echo "zip, 7z, rar, lzo, xz, and lzma." ;;
esac
}