1
0
Fork 0
mirror of synced 2024-12-27 16:33:21 -05:00
zimfw/modules/archive/functions/unarchive
Eric Nielsen 2e3ba7996b
[archive] Allow archives in any directory
Current code will create an archive in the current directory with:

    % archive ../test.tar.gz test.*

or will complain that the following archive doesn't exist in the current
directory (given it actually exists in the parent one):

    % unarchive ../test.tar.gz

Fix that and allow archives in any directory.

Other changes:

* Use `<required_param>` instead of `[required_param]` in the usage text
* Don't explictly check if archive exists in `unarchive`, but let the
  respective tool fail with its own message

Closes #312
2018-11-24 22:45:08 -05:00

34 lines
1.2 KiB
Bash

# vim:et sts=2 sw=2 ft=zsh
#
# Unarchives files
#
if (( # != 1 )); then
print "usage: ${0} <archive_name.ext>" >&2
return 1
fi
local archive_name="${1}"
# using unpigz/pbunzip2 provides little to decompression time; the benefit is mainly in compression time.
# setting it as an alias in the init.zsh file should be sufficient here.
case "${archive_name}" in
(*.tar.gz|*.tgz) tar -xvzf "${archive_name}" ;;
(*.tar.bz|*.tar.bz2|*.tbz|*.tbz2) tar -xvjf "${archive_name}" ;;
(*.tar.xz|*.txz) tar -J --help &>/dev/null && tar -xvJf "${archive_name}" \
|| xzcat "${archive_name}" | tar xvf - ;;
(*.tar.lzma|*.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}" ;;
(*.bz|*.bz2) bunzip2 "${archive_name}" ;;
(*.xz) unxz "${archive_name}" ;;
(*.lzma) unlzma "${archive_name}" ;;
(*.Z) uncompress "${archive_name}" ;;
(*.zip) unzip "${archive_name}";;
(*.rar) (( $+{commands[unrar]} )) && unrar x -ad "${archive_name}" \
|| rar x -ad "${archive_name}" ;;
(*.7z|*.001) 7za x "${archive_name}" ;;
(*) print "${0}: unknown archive type: ${archive_name}" ;;
esac