This repository has been archived on 2024-03-04. You can view files and clone it, but cannot push or open issues or pull requests.
linux/.config/bash/module/Pack.sh

168 lines
3 KiB
Bash
Raw Normal View History

2023-12-07 04:02:47 +03:00
export _unpack_supported=".tar$|.tgz$|.txz$|.tar.gz$|.tar.xz$|.zip$|.iso$|.rar$"
2023-11-02 23:36:10 +03:00
# Pack files into desired format.
2023-12-07 04:02:47 +03:00
# All files and directories by default.
2023-11-21 23:32:08 +03:00
# Usage: pack <TARGET.ext> [FILES]
2023-12-07 01:44:42 +03:00
function pack() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
local output="${1}"
local targets=("${@:2}")
local format="${output##*.}"
local name="${output%.*}"
# report no output.
if [[ "${output}" = "" ]]; then
2023-12-07 04:02:47 +03:00
help pack
2023-12-05 21:50:45 +03:00
return 2
fi
# report no format.
if [[ "${format}" = "" ]]; then
2023-12-17 20:22:06 +03:00
_error "Could not determine output format."
2023-12-07 04:02:47 +03:00
help pack
2023-12-05 21:50:45 +03:00
return 2
fi
# All targets by default.
2023-12-17 20:22:06 +03:00
[[ "${targets}" = "" ]] && targets=(*)
2023-12-05 21:50:45 +03:00
case "${format}" in
"tgz")
_pack_tgz "${output}" "${targets[@]}"
;;
"txz")
_pack_txz "${output}" "${targets[@]}"
;;
"tar")
_pack_tar "${output}" "${targets[@]}"
;;
"zip")
_pack_zip "${output}" "${targets[@]}"
;;
"gz")
_pack_gz "${output}" "${targets[@]}"
;;
"xz")
_pack_xz "${output}" "${targets[@]}"
;;
*)
2023-12-17 20:22:06 +03:00
_error "${target}: Format not supported."
return 2
2023-12-05 21:50:45 +03:00
;;
esac
2023-11-02 23:36:10 +03:00
}
2023-12-07 04:02:47 +03:00
# Attempt to unpack.
# All supported formats by default.
# Usage: unpack [FILES]
2023-12-07 01:44:42 +03:00
function unpack() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
2023-12-17 20:22:06 +03:00
local targets=(${@})
[[ "${targets}" = "" ]] && targets=($(_ls_files | grep -E ${_unpack_supported}))
2023-12-05 21:50:45 +03:00
2023-12-17 20:22:06 +03:00
process() {
2023-12-05 21:50:45 +03:00
# unpack file type.
local type="${target##*.}"
[[ "${target}" =~ .tar.gz$ ]] && type="tar.gz"
[[ "${target}" =~ .tar.xz$ ]] && type="tar.xz"
# unpack content.
case "${type,,}" in
"zip")
_unpack_zip "${target}"
;;
"7z")
_unpack_7z "${target}"
;;
"tgz"|"tar.gz")
_unpack_tgz "${target}"
;;
"txz"|"tar.xz")
_unpack_txz "${target}"
;;
"tar")
_unpack_tar "${target}"
;;
"iso")
_unpack_iso "${target}"
;;
"rar")
_unpack_rar "${target}"
;;
"xz")
_unpack_xz "${target}"
;;
"gz")
_unpack_gz "${target}"
;;
*)
2023-12-17 20:22:06 +03:00
_error "${target}: Format not supported."
return 2
2023-12-05 21:50:45 +03:00
;;
esac
2023-12-17 20:22:06 +03:00
}
2023-12-05 21:50:45 +03:00
2023-12-17 20:22:06 +03:00
_iterate_targets process ${targets[@]}
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _pack_zip() {
2023-12-05 21:50:45 +03:00
zip -9 -r "${@}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _pack_tgz() {
2024-01-27 20:05:17 +03:00
tar -c "${@:2}" | pv -s $(/usr/bin/env du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _pack_txz() {
2024-01-27 20:05:17 +03:00
tar -c "${@:2}" | pv -s $(/usr/bin/env du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _pack_tar() {
2024-01-27 20:05:17 +03:00
tar -c "${@:2}" | pv -s $(/usr/bin/env du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _pack_gz() {
2023-12-05 21:50:45 +03:00
pv "${2}" | gzip -1 > "${1}"
}
2023-12-07 01:44:42 +03:00
function _pack_xz() {
2023-12-05 21:50:45 +03:00
pv "${2}" | xz -9e > "${1}"
}
2023-12-07 01:44:42 +03:00
function _unpack_zip() {
2023-12-05 21:50:45 +03:00
unzip "${1}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _unpack_7z() {
2023-12-05 21:50:45 +03:00
7za x "${1}"
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _unpack_tgz() {
2023-12-05 21:50:45 +03:00
pv "${1}" | gzip -d | tar -xf -
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _unpack_txz() {
2023-12-05 21:50:45 +03:00
pv "${1}" | xz -d | tar -xf -
2023-11-02 23:36:10 +03:00
}
2023-12-07 01:44:42 +03:00
function _unpack_tar() {
2023-12-05 21:50:45 +03:00
pv "${1}" | tar -xf -
}
2023-12-07 01:44:42 +03:00
function _unpack_iso() {
2023-12-05 21:50:45 +03:00
7za x "${1}"
2023-11-02 23:36:10 +03:00
}
2023-11-03 00:15:40 +03:00
2023-12-07 01:44:42 +03:00
function _unpack_rar() {
2023-12-05 21:50:45 +03:00
unrar x "${1}"
}
2023-12-07 01:44:42 +03:00
function _unpack_gz() {
2023-12-05 21:50:45 +03:00
pv "${1}" | gzip -d > "${1%.gz}"
}
2023-12-07 01:44:42 +03:00
function _unpack_xz() {
2023-12-05 21:50:45 +03:00
pv "${1}" | xz -d > "${1%.xz}"
2023-11-12 16:50:46 +03:00
}