zim/modules/archive/functions/unarchive

45 lines
1.3 KiB
Text
Raw Normal View History

2015-12-16 23:00:25 +01:00
#
# Unarchives files
#
local archive_name _gunzip_bin _bunzip2_bin
2015-12-19 15:48:29 +01:00
if (( ${#} != 1 )); then
2015-12-16 23:00:25 +01:00
cat >&2 <<EOF
2015-12-19 15:48:29 +01:00
usage: ${0} [archive.zip]
2015-12-16 23:00:25 +01:00
EOF
return 1
fi
if [[ ! -s ${1} ]]; then
print "archive \"${1}\" was not found or has size 0." >&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}" ;;
2015-12-17 20:04:33 +01:00
(*.zip) unzip "${archive_name}";;
2015-12-16 23:00:25 +01:00
(*.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