From ceb0f3ad2a7dcd0db02f44a235d5ad62e9b199e5 Mon Sep 17 00:00:00 2001 From: Matt Hamilton Date: Wed, 16 Dec 2015 17:00:25 -0500 Subject: [PATCH] add archive module --- modules/archive/README.md | 31 +++++++++++++++ modules/archive/functions/archive | 58 +++++++++++++++++++++++++++++ modules/archive/functions/unarchive | 44 ++++++++++++++++++++++ modules/archive/init.zsh | 29 +++++++++++++++ 4 files changed, 162 insertions(+) create mode 100644 modules/archive/README.md create mode 100644 modules/archive/functions/archive create mode 100644 modules/archive/functions/unarchive create mode 100644 modules/archive/init.zsh diff --git a/modules/archive/README.md b/modules/archive/README.md new file mode 100644 index 0000000..87e8943 --- /dev/null +++ b/modules/archive/README.md @@ -0,0 +1,31 @@ +Archive +======= + +Provides `archive` and `unarchive` functions for easy archive manipulation. + +This module will make use of `pigz` and `pbzip2` if available to make use of all available CPU cores. + +Functions +--------- + + * `archive` generates an archive based on file extension. Syntax is `archive myarchive.tar.gz /path/to/archive` + * `unarchive` unarchives a file based on the extension. Syntax is `unarchive myarchive.7z` + +Archive Formats +--------------- + +| Format | Requirements | +| ------ | ------------ | +| .tar | `tar` | +| .tar.gz, .tgz | `tar` or `pigz` | +| .tar.bz2, .tbz | `tar` or `pbzip2` | +| .tar.xz, .txz | `tar` with xz support | +| .tar.zma, .tlz | `tar` with lzma support | +| .gz | `gunzip` or `pigz` | +| .bz2 | `bunzip2` or `pbzip2` | +| .xz | `unxz` | +| .lzma | `unzlma` | +| .Z | `uncompress` | +| .zip | `unzip` | +| .rar | `unrar` or `rar` | +| .7z | `7za` | diff --git a/modules/archive/functions/archive b/modules/archive/functions/archive new file mode 100644 index 0000000..1b8a781 --- /dev/null +++ b/modules/archive/functions/archive @@ -0,0 +1,58 @@ +# +# Creates archive files +# + +local archive_name dir_to_archive _gzip_bin _bzip2_bin + +if (( $# != 2 )); then + cat >&2 <&2 + return 1 +fi + +# here, we check for dropin/multi-threaded replacements +if (( $+commands[pigz] )); then + _gzip_bin='pigz' +else + _gzip_bin='gzip' +fi + +if (( $+commands[pbzip2] )); then + _bzip2_bin='pbzip2' +else + _bzip2_bin='bzip2' +fi + +case "${archive_name}" in + (*.tar.gz|*.tgz) tar -cvf "${archive_name}" --use-compress-program="${_gzip_bin}" "${dir_to_archive}" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar -cvf "${archive_name}" --use-compress-program="${_bzip2_bin}" "${dir_to_archive}" ;; + (*.tar.xz|*.txz) tar --xz --help &> /dev/null && tar -cvJf "${archive_name}" "${dir_to_archive}" ;; + (*.tar.lzma|*.tlz) tar --lzma --help &> /dev/null && tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;; + (*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;; + (*.zip) zip -r "${archive_name}" "${dir_to_archive}" ;; + (*.rar) rar a "${archive_name}" "${dir_to_archive}" ;; + (*.7z) 7za a "${archive_name}" "${dir_to_archive}" ;; + (*.gz) print "${0}: .gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;; + (*.bz2) print "${0}: .bzip2 is only useful for single files, and does not capture permissions. Use .tar.bz2" ;; + (*.xz) print "${0}: .xz is only useful for single files, and does not capture permissions. Use .tar.xz" ;; + (*.lzma) print "${0}: .lzma is only useful for single files, and does not capture permissions. Use .tar.lzma" ;; + (*) print "${0}: unknown archive type: ${archive_name}" ;; +esac diff --git a/modules/archive/functions/unarchive b/modules/archive/functions/unarchive new file mode 100644 index 0000000..2009950 --- /dev/null +++ b/modules/archive/functions/unarchive @@ -0,0 +1,44 @@ +# +# Unarchives files +# + +local archive_name _gunzip_bin _bunzip2_bin + +if (( $# != 1 )); then + cat >&2 <&2 + return 1 +fi + +# strip the path, just in case one is provided for some reason +archive_name="${1:t}" + +# using unpigz/pbunzip2 provides little to decompression time; the benefit is mainly in compression time. +# may add it later. + +case "${archive_name}" in + (*.tar.gz|*.tgz) tar -xvzf "${archive_name}" ;; + (*.tar.bz2|*.tbz|*.tbz2) tar -xvjf "${archive_name}" ;; + (*.tar.xz|*.txz) tar --xz --help &> /dev/null && tar --xz -xvf "${archive_name}" \ + || xzcat "${archive_name}" | tar xvf - ;; + (*.tar.zma|*.tlz) tar --lzma --help &> /dev/null && tar --lzma -xvf "${archive_name}" \ + || lzcat "${archive_name}" | tar xvf - ;; + (*.tar) tar xvf "${archive_name}" ;; + (*.gz) gunzip "${archive_name}" ;; + (*.bz2) bunzip2 "${archive_name}" ;; + (*.xz) unxz "${archive_name}" ;; + (*.lzma) unlzma "${archive_name}" ;; + (*.Z) uncompress "${archive_name}" ;; + (*.zip) unzip "${archive_name}" -d $extract_dir ;; + (*.rar) unrar &> /dev/null \ + && unrar x -ad "${archive_name}" \ + || rar x -ad "${archive_name}" ;; + (*.7z) 7za x "${archive_name}" ;; + (*) print "${0}: unknown archive type: ${archive_name}" ;; +esac diff --git a/modules/archive/init.zsh b/modules/archive/init.zsh new file mode 100644 index 0000000..98e2c2e --- /dev/null +++ b/modules/archive/init.zsh @@ -0,0 +1,29 @@ +# +# Archive aliases +# + +# if pigz/pbzip2 are available, alias them as they are drop-in replacements for gzip and bzip2, respectively. + +# +# pigz +# + +if (( $+commands[pigz] )); then + alias gzip='pigz' +fi + +if (( $+commands[unpigz] )); then + alias gunzip='pigz' +fi + +# +# pbzip2 +# + +if (( $+commands[pbzip2] )); then + alias bzip2='pbzip2' +fi + +if (( $+commands[pbunzip2] )); then + alias bunzip2='pbzip2' +fi