├── .gitignore ├── LICENSE ├── README.md ├── functions ├── _unarchive ├── archive ├── lsarchive └── unarchive └── init.zsh /.gitignore: -------------------------------------------------------------------------------- 1 | *.swp 2 | *.zwc 3 | *.zwc.old 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2015-2016 Matt Hamilton and contributors 4 | Copyright (c) 2016-2023 Eric Nielsen, Matt Hamilton and contributors 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a copy 7 | of this software and associated documentation files (the "Software"), to deal 8 | in the Software without restriction, including without limitation the rights 9 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | copies of the Software, and to permit persons to whom the Software is 11 | furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in all 14 | copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 22 | SOFTWARE. 23 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | archive 2 | ======= 3 | 4 | Provides `archive`, `unarchive` and `lsarchive` functions for easy archive 5 | manipulation based on file extensions. 6 | 7 | [Don't remember the complete tar command to compress, uncompress or list the 8 | contents of an archive?](https://xkcd.com/1168/) Use `archive myarchive.tar.gz 9 | /path/to/be/archived` to compress, `unarchive myarchive.tar.gz` to uncompress or 10 | `lsarchive myarchive.tar.gz` to list, for example, and the respective functions 11 | will take care of the underlying command for you. They also work with the other 12 | archive extensions listed below. 13 | 14 | Suffix aliases are also provided so files can be directly uncompressed. 15 | 16 | This module will make use of `pbzip2` and `pigz` if available to make use of all available CPU cores. 17 | 18 | Functions 19 | --------- 20 | 21 | | Name | Usage 22 | | ---- | ----- 23 | | `archive` compresses files into an archive. | `archive ...` 24 | | `unarchive` uncompresses from archives. | `unarchive ...` 25 | | `lsarchive` lists the contents of archives. | `lsarchive ...` 26 | 27 | Archive extensions 28 | ------------------ 29 | 30 | | Extensions | Required commands 31 | | ---------- | ----------------- 32 | | .7z, .001 | `7za` 33 | | .rar | `unrar` or `rar` 34 | | .tar.bz, .tar.bz2, .tbz, .tbz2 | `tar` 35 | | .tar.gz, .tgz | `tar` 36 | | .tar.lzma, .tlz | `tar` with lzma support or with `lzcat` 37 | | .tar.xz, .txz | `tar` with xz support or with `xzcat` 38 | | .tar.zst, .tzst | `tar` with `unzstd` 39 | | .tar | `tar` 40 | | .zip | `unzip` 41 | | .bz, .bz2 | `pbunzip2` or `bunzip2` 42 | | .gz | `unpigz` or `gunzip` 43 | | .lzma | `unzlma` 44 | | .xz | `unxz` 45 | | .zst | `zstd` 46 | | .Z | `uncompress` 47 | -------------------------------------------------------------------------------- /functions/_unarchive: -------------------------------------------------------------------------------- 1 | #compdef unarchive lsarchive 2 | 3 | _arguments \ 4 | "*:archive:_files -g '(#i)*.(7z|001|rar|bz|bz2|tbz|tbz2|gz|tgz|lzma|tlz|xz|txz|tar|zip|zst|tzst|Z)(-.)'" 5 | -------------------------------------------------------------------------------- /functions/archive: -------------------------------------------------------------------------------- 1 | # vim:et sts=2 sw=2 ft=zsh 2 | # 3 | # Creates archive files 4 | # 5 | builtin emulate -L zsh 6 | 7 | if (( # < 2 )); then 8 | print -u2 "usage: ${0} ..." 9 | return 2 10 | fi 11 | 12 | # we are quitting (above) if there are less than 2 vars, 13 | # so we don't need any argc check here. 14 | 15 | case ${1} in 16 | (*.7z) 7za a "${@}" ;; 17 | (*.rar) rar a "${@}" ;; 18 | (*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar --use-compress-program=${${(k)commands[pbzip2]}:-bzip2} -cvf "${@}" ;; 19 | (*.tar.gz|*.tgz) tar --use-compress-program=${${(k)commands[pigz]}:-gzip} -cvf "${@}" ;; 20 | (*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && XZ_OPT=-T0 tar --lzma -cvf "${@}" ;; 21 | (*.tar.xz|*.txz) tar -J --help &>/dev/null && XZ_OPT=-T0 tar -cvJf "${@}" ;; 22 | (*.tar.zst|*.tzst) XZ_OPT=-T0 tar --use-compress-program=zstd -cvf "${@}" ;; 23 | (*.tar) tar -cvf "${@}" ;; 24 | (*.zip) zip -r "${@}" ;; 25 | (*.bz|*.bz2) print -u2 "${0}: .bzip2 is only useful for single files, and does not capture permissions. Use .tar.bz2" ;; 26 | (*.gz) print -u2 "${0}: .gz is only useful for single files, and does not capture permissions. Use .tar.gz" ;; 27 | (*.lzma) print -u2 "${0}: .lzma is only useful for single files, and does not capture permissions. Use .tar.lzma" ;; 28 | (*.xz) print -u2 "${0}: .xz is only useful for single files, and does not capture permissions. Use .tar.xz" ;; 29 | (*.zst) print -u2 "${0}: .zst is only useful for single files, and does not capture permissions. Use .tar.zst" ;; 30 | (*.Z) print -u2 "${0}: .Z is only useful for single files, and does not capture permissions." ;; 31 | (*) print -u2 "${0}: unknown archive type: ${1}" ;; 32 | esac 33 | -------------------------------------------------------------------------------- /functions/lsarchive: -------------------------------------------------------------------------------- 1 | # vim:et sts=2 sw=2 ft=zsh 2 | # 3 | # Lists files in archives 4 | # 5 | builtin emulate -L zsh -o ERR_RETURN 6 | 7 | if (( # < 1 )); then 8 | print -u2 "usage: ${0} ..." 9 | return 2 10 | fi 11 | 12 | while (( # > 0 )); do 13 | case ${1} in 14 | (*.7z|*.001) 7za l ${1} ;; 15 | (*.rar) (( ${+commands[unrar]} )) && unrar l ${1} || rar l ${1} ;; 16 | (*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -tvjf ${1} ;; 17 | (*.tar.gz|*.tgz) tar -tvzf ${1} ;; 18 | (*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && XZ_OPT=-T0 tar --lzma -tvf ${1} \ 19 | || lzcat ${1} | tar -tvf - ;; 20 | (*.tar.xz|*.txz) tar -J --help &>/dev/null && XZ_OPT=-T0 tar -tvJf ${1} \ 21 | || xzcat ${1} | tar -tvf - ;; 22 | (*.tar.zst|*.tzst) XZ_OPT=-T0 tar --use-compress-program=unzstd -tvf "${@}" ;; 23 | (*.tar) tar -tvf ${1} ;; 24 | (*.zip) unzip -l ${1} ;; 25 | (*.gz) gunzip -l ${1} ;; 26 | (*.xz) unxz -T0 -l ${1} ;; 27 | (*.zst) zstd -T0 -l ${1} ;; 28 | (*) print -u2 "${0}: unknown archive type: ${1}" ;; 29 | esac 30 | shift 31 | done 32 | -------------------------------------------------------------------------------- /functions/unarchive: -------------------------------------------------------------------------------- 1 | # vim:et sts=2 sw=2 ft=zsh 2 | # 3 | # Unarchives files 4 | # 5 | builtin emulate -L zsh -o ERR_RETURN 6 | 7 | if (( # < 1 )); then 8 | print -u2 "usage: ${0} ..." 9 | return 2 10 | fi 11 | 12 | # using pbunzip2/unpigz provides little to decompression time; the benefit is mainly in compression time. 13 | 14 | while (( # > 0 )); do 15 | case ${1} in 16 | (*.7z|*.001) 7za x ${1} ;; 17 | (*.rar) (( ${+commands[unrar]} )) && unrar x -ad ${1} || rar x -ad ${1} ;; 18 | (*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -xvjf ${1} ;; 19 | (*.tar.gz|*.tgz) tar -xvzf ${1} ;; 20 | (*.tar.lzma|*.tlz) tar --lzma --help &>/dev/null && XZ_OPT=-T0 tar --lzma -xvf ${1} \ 21 | || lzcat ${1} | tar -xvf - ;; 22 | (*.tar.xz|*.txz) tar -J --help &>/dev/null && XZ_OPT=-T0 tar -xvJf ${1} \ 23 | || xzcat ${1} | tar -xvf - ;; 24 | (*.tar.zst|*.tzst) XZ_OPT=-T0 tar --use-compress-program=unzstd -xvf ${1} ;; 25 | (*.tar) tar -xvf ${1} ;; 26 | (*.zip) unzip ${1};; 27 | (*.bz|*.bz2) bunzip2 ${1} ;; 28 | (*.gz) gunzip ${1} ;; 29 | (*.lzma) unlzma -T0 ${1} ;; 30 | (*.xz) unxz -T0 ${1} ;; 31 | (*.zst) zstd -T0 -d ${1} ;; 32 | (*.Z) uncompress ${1} ;; 33 | (*) print -u2 "${0}: unknown archive type: ${1}" ;; 34 | esac 35 | shift 36 | done 37 | -------------------------------------------------------------------------------- /init.zsh: -------------------------------------------------------------------------------- 1 | # 2 | # Archive aliases 3 | # 4 | 5 | # If pbzip2/pigz are available, alias them as they are drop-in replacements for bzip2/gzip, respectively. 6 | 7 | # 8 | # pbzip2 9 | # 10 | if (( ${+commands[pbzip2]} )) alias bzip2=pbzip2 11 | if (( ${+commands[pbunzip2]} )) alias bunzip2=pbunzip2 12 | 13 | # 14 | # pigz 15 | # 16 | if (( ${+commands[pigz]} )) alias gzip=pigz 17 | if (( ${+commands[unpigz]} )) alias gunzip=unpigz 18 | 19 | # 20 | # Suffix alias 21 | # 22 | alias -s {7z,001,rar,bz,bz2,tbz,tbz2,gz,tgz,lzma,tlz,xz,txz,tar,zip,zst,tzst,Z}=unarchive 23 | --------------------------------------------------------------------------------