From fb37539f439fc5e382d223c3f9b2abb2b4267cb3 Mon Sep 17 00:00:00 2001 From: Matt Hamilton Date: Mon, 5 Oct 2015 18:47:41 -0400 Subject: [PATCH] archive: add archive function --- modules/archive/README.md | 12 ++++-- modules/archive/functions/archive | 67 +++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 3 deletions(-) create mode 100644 modules/archive/functions/archive diff --git a/modules/archive/README.md b/modules/archive/README.md index fca34cf..28a2c3b 100644 --- a/modules/archive/README.md +++ b/modules/archive/README.md @@ -1,11 +1,12 @@ Archive ======= -Provides functions to list and extract archives. +Provides functions to create, list, and extract archives. Functions --------- + - `archive` creates an archive based on the provided archive name. - `lsarchive` lists the contents of one or more archives. - `unarchive` extracts the contents of one or more archives. @@ -15,8 +16,8 @@ Supported Formats The following archive formats are supported when the required utilities are installed: - - *.tar.gz*, *.tgz* require `tar`. - - *.tar.bz2*, *.tbz* require `tar`. + - *.tar.gz*, *.tgz* require `tar` (optionally `pigz`). + - *.tar.bz2*, *.tbz* require `tar` (optionally `pbzip2`). - *.tar.xz*, *.txz* require `tar` with *xz* support. - *.tar.zma*, *.tlz* require `tar` with *lzma* support. - *.tar* requires `tar`. @@ -30,11 +31,16 @@ installed: - *.7z* requires `7za`. - *.deb* requires `ar`, `tar`. +Additionally, if `pigz' and/or `pbzip2` are installed, `archive` will use them over +their traditional counterparts, `gzip` and `bzip2` respectively, to take full advantage +of all available CPU cores for compression. + Authors ------- *The authors of this module should be contacted via the [issue tracker][1].* - [Sorin Ionescu](https://github.com/sorin-ionescu) + - [Matt Hamilton](https://github.com/Eriner) [1]: https://github.com/sorin-ionescu/prezto/issues diff --git a/modules/archive/functions/archive b/modules/archive/functions/archive new file mode 100644 index 0000000..ab0d56d --- /dev/null +++ b/modules/archive/functions/archive @@ -0,0 +1,67 @@ +#!/usr/bin/env zsh +# +# Creates archive file +# +# Authors: +# Matt Hamilton +# + +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 +# this should eventually be moved to modules/archive/init.zsh +# as a global alias +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 -cvJf "${archive_name}" "${dir_to_archive}" ;; + (*.tar.lzma|*.tlz) tar -cvf "${archive_name}" --lzma "${dir_to_archive}" ;; + (*.tar) tar -cvf "${archive_name}" "${dir_to_archive}" ;; + (*.zip|*.jar) 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 "\n.gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;; + (*.bz2) print "\n.bzip2 is only useful for single files, and does not capture permissions. Use .tar.bz2" ;; + (*.xz) print "\n.xz is only useful for single files, and does not capture permissions. Use .tar.xz" ;; + (*.lzma) print "\n.lzma is only useful for single files, and does not capture permissions. Use .tar.lzma" ;; + (*) print "\nunknown archive type for archive: ${archive_name}" ;; +esac