2023-11-12 16:50:46 +03:00
|
|
|
_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-11-02 23:36:10 +03:00
|
|
|
pack()
|
|
|
|
{
|
2023-11-22 13:20:01 +03:00
|
|
|
local IFS=$'\n'
|
2023-11-02 23:36:10 +03:00
|
|
|
local output="${1}"
|
|
|
|
local targets=("${@:2}")
|
|
|
|
local format="${output##*.}"
|
|
|
|
local name="${output%.*}"
|
|
|
|
|
|
|
|
# report no output.
|
|
|
|
if [[ "${output}" = "" ]]; then
|
2023-11-21 23:32:08 +03:00
|
|
|
echo "Usage: pack <TARGET.ext> [FILES]"
|
2023-11-20 18:39:04 +03:00
|
|
|
return 2
|
2023-11-02 23:36:10 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# report no format.
|
|
|
|
if [[ "${format}" = "" ]]; then
|
|
|
|
echo "Could not determine output format."
|
2023-11-21 23:32:08 +03:00
|
|
|
echo "Usage: pack <TARGET.ext> [FILES]"
|
2023-11-02 23:36:10 +03:00
|
|
|
return 2
|
|
|
|
fi
|
|
|
|
|
2023-11-21 23:32:08 +03:00
|
|
|
# All targets by default.
|
2023-11-02 23:36:10 +03:00
|
|
|
if [[ "${targets}" = "" ]]; then
|
2023-11-21 23:32:08 +03:00
|
|
|
targets=($(ls))
|
2023-11-02 23:36:10 +03:00
|
|
|
fi
|
|
|
|
|
|
|
|
# process.
|
|
|
|
case "${format}" in
|
|
|
|
"tgz")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_tgz "${output}" "${targets[@]}"
|
2023-11-02 23:36:10 +03:00
|
|
|
;;
|
|
|
|
"txz")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_txz "${output}" "${targets[@]}"
|
2023-11-02 23:36:10 +03:00
|
|
|
;;
|
|
|
|
"tar")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_tar "${output}" "${targets[@]}"
|
2023-11-02 23:36:10 +03:00
|
|
|
;;
|
|
|
|
"zip")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_zip "${output}" "${targets[@]}"
|
2023-11-02 23:36:10 +03:00
|
|
|
;;
|
2023-11-14 06:13:26 +03:00
|
|
|
"gz")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_gz "${output}" "${targets[@]}"
|
2023-11-14 06:13:26 +03:00
|
|
|
;;
|
|
|
|
"xz")
|
2023-11-22 13:20:01 +03:00
|
|
|
_pack_xz "${output}" "${targets[@]}"
|
2023-11-24 01:19:49 +03:00
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo -e "${color_bred}${target}: Format not supported.${color_default}"
|
|
|
|
return 1
|
|
|
|
;;
|
2023-11-02 23:36:10 +03:00
|
|
|
esac
|
2023-11-02 23:52:05 +03:00
|
|
|
|
2023-11-24 01:19:49 +03:00
|
|
|
# Show error.
|
|
|
|
if [[ $? != 0 ]]; then
|
|
|
|
echo -e "${color_bred}${target}: Failed.${color_default}"
|
2023-11-02 23:52:05 +03:00
|
|
|
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-11-02 23:36:10 +03:00
|
|
|
unpack()
|
|
|
|
{
|
2023-11-22 13:20:01 +03:00
|
|
|
local IFS=$'\n'
|
2023-11-02 23:36:10 +03:00
|
|
|
local targets=("${@}")
|
|
|
|
local count=0
|
|
|
|
local total=${#targets[@]}
|
2023-11-24 01:19:49 +03:00
|
|
|
local failed=0
|
2023-11-02 23:36:10 +03:00
|
|
|
|
2023-11-21 23:32:08 +03:00
|
|
|
# All targets by default.
|
2023-11-02 23:36:10 +03:00
|
|
|
if [[ "${targets}" = "" ]]; then
|
2023-11-03 21:28:11 +03:00
|
|
|
targets=($(ls | grep -E ${_UNPACK_SUPPORTED}))
|
2023-11-02 23:36:10 +03:00
|
|
|
total=${#targets[@]}
|
|
|
|
fi
|
|
|
|
|
|
|
|
# process all files.
|
|
|
|
for target in "${targets[@]}"; do
|
|
|
|
# increment counter.
|
|
|
|
((count++))
|
|
|
|
|
|
|
|
# prepare status.
|
|
|
|
local status="[${count}/${total}] ${target}"
|
|
|
|
|
|
|
|
# show status.
|
2023-11-22 11:07:29 +03:00
|
|
|
echo -e "${status}"
|
2023-11-02 23:36:10 +03:00
|
|
|
|
|
|
|
# unpack file type.
|
|
|
|
local type="${target##*.}"
|
|
|
|
|
2023-11-14 06:13:26 +03:00
|
|
|
[[ "${target}" =~ .tar.gz$ ]] && type="tar.gz"
|
|
|
|
[[ "${target}" =~ .tar.xz$ ]] && type="tar.xz"
|
|
|
|
|
2023-11-02 23:36:10 +03:00
|
|
|
# unpack content.
|
|
|
|
case "${type,,}" in
|
|
|
|
"zip")
|
|
|
|
_unpack_zip "${target}"
|
|
|
|
;;
|
|
|
|
"7z")
|
|
|
|
_unpack_7z "${target}"
|
|
|
|
;;
|
2023-11-14 06:13:26 +03:00
|
|
|
"tgz"|"tar.gz")
|
2023-11-02 23:36:10 +03:00
|
|
|
_unpack_tgz "${target}"
|
|
|
|
;;
|
2023-11-14 06:13:26 +03:00
|
|
|
"txz"|"tar.xz")
|
2023-11-02 23:36:10 +03:00
|
|
|
_unpack_txz "${target}"
|
|
|
|
;;
|
|
|
|
"tar")
|
|
|
|
_unpack_tar "${target}"
|
|
|
|
;;
|
2023-11-03 21:28:11 +03:00
|
|
|
"iso")
|
|
|
|
_unpack_iso "${target}"
|
|
|
|
;;
|
2023-11-12 16:50:46 +03:00
|
|
|
"rar")
|
|
|
|
_unpack_rar "${target}"
|
|
|
|
;;
|
2023-11-14 06:13:26 +03:00
|
|
|
"xz")
|
|
|
|
_unpack_xz "${target}"
|
|
|
|
;;
|
|
|
|
"gz")
|
|
|
|
_unpack_gz "${target}"
|
|
|
|
;;
|
2023-11-02 23:36:10 +03:00
|
|
|
*)
|
2023-11-24 01:19:49 +03:00
|
|
|
echo -e "${color_bred}${target}: Format not supported.${color_default}"
|
|
|
|
((failed++))
|
|
|
|
continue
|
2023-11-02 23:36:10 +03:00
|
|
|
;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
# actions on error.
|
|
|
|
if [[ $? != 0 ]]; then
|
2023-11-24 01:19:49 +03:00
|
|
|
echo -e "${color_bred}${target}: Failed.${color_default}"
|
2023-11-02 23:36:10 +03:00
|
|
|
((failed++))
|
|
|
|
fi
|
2023-11-24 01:19:49 +03:00
|
|
|
done
|
2023-11-02 23:36:10 +03:00
|
|
|
|
2023-11-24 01:19:49 +03:00
|
|
|
if [[ ${failed} != 0 ]]; then
|
|
|
|
echo -e "${color_bred}Failed: ${failed}.${color_default}"
|
|
|
|
false
|
|
|
|
fi
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_pack_zip()
|
|
|
|
{
|
|
|
|
zip -9 -r "${@}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_pack_tgz()
|
|
|
|
{
|
|
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_pack_txz()
|
|
|
|
{
|
|
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_pack_tar()
|
|
|
|
{
|
|
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}"
|
|
|
|
}
|
|
|
|
|
2023-11-14 06:13:26 +03:00
|
|
|
_pack_gz()
|
|
|
|
{
|
|
|
|
pv "${2}" | gzip -1 > "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_pack_xz()
|
|
|
|
{
|
|
|
|
pv "${2}" | xz -9e > "${1}"
|
|
|
|
}
|
|
|
|
|
2023-11-02 23:36:10 +03:00
|
|
|
_unpack_zip()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
unzip "${1}"
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_7z()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
7za x "${1}"
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_tgz()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
pv "${1}" | gzip -d | tar -xf -
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_txz()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
pv "${1}" | xz -d | tar -xf -
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_tar()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
pv "${1}" | tar -xf -
|
2023-11-03 21:28:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_iso()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
7za x "${1}"
|
2023-11-02 23:36:10 +03:00
|
|
|
}
|
2023-11-03 00:15:40 +03:00
|
|
|
|
2023-11-12 16:50:46 +03:00
|
|
|
_unpack_rar()
|
|
|
|
{
|
2023-11-14 06:13:26 +03:00
|
|
|
unrar x "${1}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_gz()
|
|
|
|
{
|
|
|
|
pv "${1}" | gzip -d > "${1%.gz}"
|
|
|
|
}
|
|
|
|
|
|
|
|
_unpack_xz()
|
|
|
|
{
|
|
|
|
pv "${1}" | xz -d > "${1%.xz}"
|
2023-11-12 16:50:46 +03:00
|
|
|
}
|
|
|
|
|
2023-11-03 00:15:40 +03:00
|
|
|
# export functions.
|
2023-11-24 01:19:49 +03:00
|
|
|
export -f pack unpack _pack_tgz _pack_txz _pack_tar _pack_zip _unpack_zip _unpack_7z _unpack_tgz _unpack_txz _unpack_tar _unpack_iso _unpack_rar _pack_gz _pack_xz _unpack_gz _unpack_xz
|