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

202 lines
3.6 KiB
Bash
Raw Normal View History

2023-12-07 01:44:42 +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-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
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# report no format.
if [[ "${format}" = "" ]]; then
echo "Could not determine output format."
echo "Usage: pack <TARGET.ext> [FILES]"
return 2
fi
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls))
fi
# process.
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[@]}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
return 1
;;
esac
# Show error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
fi
2023-11-02 23:36:10 +03:00
}
# attempt to unpack everything.
2023-11-21 23:32:08 +03:00
# usage: unpack [FILES]
2023-12-07 01:44:42 +03:00
function unpack() {
2023-12-05 21:50:45 +03:00
local IFS=$'\n'
local targets=("${@}")
local count=0
local total=${#targets[@]}
local failed=0
# All targets by default.
if [[ "${targets}" = "" ]]; then
targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
total=${#targets[@]}
fi
# process all files.
for target in "${targets[@]}"; do
# increment counter.
((count++))
# prepare status.
local status="[${count}/${total}] ${target}"
# show status.
echo -e "${status}"
# 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}"
;;
*)
echo -e "${color_bred}${target}: Format not supported.${color_default}"
((failed++))
continue
;;
esac
# actions on error.
if [[ $? != 0 ]]; then
echo -e "${color_bred}${target}: Failed.${color_default}"
((failed++))
fi
done
if [[ ${failed} != 0 ]]; then
echo -e "${color_bred}Failed: ${failed}.${color_default}"
false
fi
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() {
2023-12-05 21:50:45 +03:00
tar -c "${@:2}" | pv -s $(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() {
2023-12-05 21:50:45 +03:00
tar -c "${@:2}" | pv -s $(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() {
2023-12-05 21:50:45 +03:00
tar -c "${@:2}" | pv -s $(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
}