unpack (985B)
1 #!/bin/sh 2 3 # unpacks an archive 4 5 die() 6 { 7 printf "%s\n" "$*" >&2 8 exit 1 9 } 10 11 run() 12 { 13 command -v "$1" >/dev/null 2>&1 || 14 die "unpack: missing program: $1" 15 16 "$@" >/dev/null || die "unpack: failed to extract" 17 } 18 19 case "$#" in 20 1) set -- "$1" "$PWD" ;; 21 2) ;; 22 *) die "usage: unpack file [outdir]" ;; 23 esac 24 25 [ -f "$1" ] || die "unpack: $1: not a regular file" 26 mkdir -p "$2" >/dev/null 2>&1 || die "unpack: $2: not a directory" 27 28 case $(file -ib -- "$1" | cut -d ';' -f 1 | cut -d '/' -f 2) in 29 bzip2|x-bzip2) run bzip2 -dc "$1" | tar -C "$2" -x ;; 30 gzip|x-gzip) run gzip -dc "$1" | tar -C "$2" -x ;; 31 tar|x-tar) run tar -C "$2" -xf "$1" ;; 32 x-7z-compressed) run 7z e -bd -o"$2" "$1" ;; 33 x-xz) run xz -dc "$1" | tar -C "$2" -x ;; 34 zip) run unzip -d "$2" "$1" ;; 35 zstd) run zstd -dcf "$1" | tar -C "$2" -x ;; 36 *) die "unpack: $1: unsupported archive type" 37 esac 38 39 printf "unpacked %s into %s\n" "$1" "$2"