Commit: a3ecc42f58130d1c14aad6ed541e9c77b467211f Parent: 1225b626606f720d497a3bed2ef611d225a282a3 Author: Randy Palamar Date: Tue, 10 Jan 2023 21:48:24 -0700 add unpack: script for extracting archives Diffstat:
A | bin/unpack | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
1 file changed, 38 insertions(+), 0 deletions(-)
diff --git a/bin/unpack b/bin/unpack @@ -0,0 +1,38 @@ +#!/bin/sh + +# unpacks an archive + +die() +{ + printf "%s\n" "$*" >&2 + exit 1 +} + +run() +{ + command -v "$1" >/dev/null 2>&1 || + die "unpack: missing program: $1" + + "$@" || die "unpack: failed to extract" +} + +case "$#" in +1) set -- "$1" "$PWD" ;; +2) ;; +*) die "usage: unpack file [outdir]" ;; +esac + +[ -f "$1" ] || die "unpack: $1: not a regular file" +mkdir "$2" >/dev/null 2>&1 || die "unpack: $2: not a directory" + +case $(file -ib -- "$1" | cut -d ';' -f 1 | cut -d '/' -f 2) in +bzip2|x-bzip2) run bzip2 -dc "$1" | tar -C "$2" -x ;; +gzip|x-gzip) run gzip -dc "$1" | tar -C "$2" -x ;; +tar|x-tar) run tar -C "$2" -xf "$1" ;; +x-xz) run xz -dc "$1" | tar -C "$2" -x ;; +zip) run unzip -d "$2" "$1" ;; +zstd) run zstd -dcf "$1" | tar -C "$2" -x ;; +*) die "unpack: $1: unsupported archive type" +esac + +printf "unpacked %s into %s\n" "$1" "$2"