195 lines
3.5 KiB
Bash
195 lines
3.5 KiB
Bash
_UNPACK_SUPPORTED=".tar$|.tgz$|.txz$|.zip$|.iso$"
|
|
|
|
# Pack files into desired format.
|
|
# Usage: pack <TARGET.ext> <FILES>
|
|
pack()
|
|
{
|
|
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 1
|
|
fi
|
|
|
|
# report no format.
|
|
if [[ "${format}" = "" ]]; then
|
|
echo "Could not determine output format."
|
|
echo "Usage: pack <TARGET.ext> <FILES>"
|
|
return 2
|
|
fi
|
|
|
|
# report no targets.
|
|
if [[ "${targets}" = "" ]]; then
|
|
echo "Usage: pack <TARGET.ext> <FILES>"
|
|
return 3
|
|
fi
|
|
|
|
# process.
|
|
case "${format}" in
|
|
"tgz")
|
|
_pack_tgz "${@}"
|
|
;;
|
|
"txz")
|
|
_pack_txz "${@}"
|
|
;;
|
|
"tar")
|
|
_pack_tar "${@}"
|
|
;;
|
|
"zip")
|
|
_pack_zip "${@}"
|
|
;;
|
|
esac
|
|
|
|
# actions on error.
|
|
if [[ $? = 0 ]]; then
|
|
echo -e "${color_green}All successful.${color_default}"
|
|
else
|
|
echo -e "${color_bred}There were errors.${color_default}"
|
|
fi
|
|
}
|
|
|
|
# attempt to unpack everything.
|
|
# usage: unpack <FILES>
|
|
unpack()
|
|
{
|
|
local targets=("${@}")
|
|
local count=0
|
|
local failed=0
|
|
local total=${#targets[@]}
|
|
|
|
# show error if no target specified.
|
|
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##*.}"
|
|
|
|
# unpack content.
|
|
case "${type,,}" in
|
|
"zip")
|
|
_unpack_zip "${target}"
|
|
;;
|
|
"7z")
|
|
_unpack_7z "${target}"
|
|
;;
|
|
"tgz")
|
|
_unpack_tgz "${target}"
|
|
;;
|
|
"txz")
|
|
_unpack_txz "${target}"
|
|
;;
|
|
"tar")
|
|
_unpack_tar "${target}"
|
|
;;
|
|
"iso")
|
|
_unpack_iso "${target}"
|
|
;;
|
|
*)
|
|
echo -e "${color_yellow}${target}: format not supported.${color_default}"
|
|
return 2
|
|
;;
|
|
esac
|
|
|
|
# actions on error.
|
|
if [[ $? != 0 ]]; then
|
|
_pack_error "${target}"
|
|
((failed++))
|
|
fi
|
|
done
|
|
|
|
# print report.
|
|
if [[ ${count} -gt 1 ]] || [[ "${*}" = "" ]]; then
|
|
if [[ ${failed} -gt 0 ]]; then
|
|
echo -e "${color_bred}Items failed to unpack: ${failed}.${color_default}"
|
|
else
|
|
echo -e "${color_green}All successful.${color_default}"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# show error.
|
|
_pack_error()
|
|
{
|
|
echo -e "${color_bred}${1}: failed.${color_default}"
|
|
}
|
|
|
|
# Pack zip.
|
|
_pack_zip()
|
|
{
|
|
zip -9 -r "${@}"
|
|
}
|
|
|
|
# pack tgz.
|
|
_pack_tgz()
|
|
{
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | gzip -1 > "${1}"
|
|
}
|
|
|
|
# pack txz.
|
|
_pack_txz()
|
|
{
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') | xz -9e > "${1}"
|
|
}
|
|
|
|
# pack tar.
|
|
_pack_tar()
|
|
{
|
|
tar -c "${@:2}" | pv -s $(du -csb "${@:2}" | sed -n -e '$p' | awk '{print $1}') > "${1}"
|
|
}
|
|
|
|
# unpack zip archive.
|
|
_unpack_zip()
|
|
{
|
|
unzip "${@}"
|
|
}
|
|
|
|
# unpack 7z archive.
|
|
_unpack_7z()
|
|
{
|
|
7za x "${@}"
|
|
}
|
|
|
|
# unpack tgz archive.
|
|
_unpack_tgz()
|
|
{
|
|
pv "${@}" | gzip -d | tar -xf -
|
|
}
|
|
|
|
# unpack txz archive.
|
|
_unpack_txz()
|
|
{
|
|
pv "${@}" | xz -d | tar -xf -
|
|
}
|
|
|
|
# unpack tar archive.
|
|
_unpack_tar()
|
|
{
|
|
pv "${@}" | tar -xf -
|
|
}
|
|
|
|
# unpack iso image.
|
|
_unpack_iso()
|
|
{
|
|
7za x "${@}"
|
|
}
|
|
|
|
# export functions.
|
|
export -f pack unpack _pack_tgz _pack_txz _pack_tar _pack_zip _unpack_zip _unpack_7z _unpack_tgz _unpack_txz _unpack_tar _pack_error _unpack_iso
|