#!/bin/sh # get command name - do we make or unpack archives? isunpack=unpack case "`basename $0`" in unztar) ;; ztar) isunpack="" ;; *) ;; esac # usage usage () { name="`basename $0`" cat 1>&2 << END $name [opts] archive-files [opts]: -t list archive contents -x extract archive contents -nodir extract to current directory (otherwise use path and basename of archive" as directory path) -h list help information and exit archive-files: extensions .tar plain tar files .tar.Z, .tz, .taz, .TZ, .TAZ compressed tar files .tar.gz, .tgz, .TGZ GNU compressed tar files .zip, .ZIP ZIP files END exit } # process options upto unrecognised minus=minus makedir=makedir while [ -n "$minus" ]; do case "$1" in -gnu) compress=gzip shift ;; -zip) compress=zip shift ;; -t) info=info shift ;; -x) extract=extract shift ;; -c) archive=archive shift ;; -nodir) makedir="" ;; -h | --help | -help) usage ;; -*) echo "[unknown option $1]" usage ;; *) minus="" ;; esac done if [ -n "$info" ]; then # content of archives for f in "$@"; do echo "File ${f}:" case "$f" in *.tar) tar tvf "$f" ;; *.tar.Z | *.tz | *.TZ | *.taz | *.TAZ) zcat < "$f" | tar tvf - ;; *.tar.gz | *.tgz | *.TGZ) gzcat < "$f" | tar tvf - ;; *.zip) zipinfo -lzhT "$f" ;; *) echo "[unrecognised file format for $0]" ;; esac echo "" done elif [ -n "$extract" ]; then # extract from the archive for f in "$@"; do echo "File ${f}:" case "$f" in # plain tar *.tar) compress=cat ; extn='\.tar' ; tar="tar xvf -" ;; # compressed tar *.tar.Z) compress=zcat ; extn='\.tar\.Z' ; tar="tar xvf -" ;; *.tz) compress=zcat ; extn='\.tz' ; tar="tar xvf -" ;; *.TZ) compress=zcat ; extn='\.TZ' ; tar="tar xvf -" ;; *.taz) compress=zcat ; extn='\.taz' ; tar="tar xvf -" ;; *.TAZ) compress=zcat ; extn='\.TAZ' ; tar="tar xvf -" ;; # gnu compressed tar *.tar.gz) compress=gzcat ; extn='\.tar\.gz' ; tar="tar xvf -" ;; *.tgz) compress=gzcat ; extn='\.tgz' ; tar="tar xvf -" ;; *.TGZ) compress=gzcat ; extn='\.TGZ' ; tar="tar xvf -" ;; # zip *.zip) zip=unzip ; extn='\.zip' ;; *.ZIP) zip=unzip ; extn='\.ZIP' ;; # anything else is unrecognised *) echo "[unrecognised file format for $0]" ;; esac if [ -n "$makedir" ] && [ -n "$extn" ]; then dir=`echo "$f" | sed -e "s/$extn$//"` fi if [ -n "$tar" ]; then # tar format if [ -n "$dir" ]; then if [ -d "$dir" ] || mkdir "$dir"; then echo "unpacking $f to directory $dir" $compress "$f" | ( cd $dir; $tar ) else echo "[directory $dir inaccessible, cannot unpack $f]" $compress "$f" | $tar fi else echo "unpacking $f locally" $compress "$f" | $tar fi elif [ -n "$zip" ]; then # zip format if [ -n "$dir" ]; then if [ -d "$dir" ] || mkdir "$dir"; then echo "unpacking to directory $dir" unzip -ov "$f" -d "$dir" else echo "[directory $dir inaccessible, cannot unpack $f]" fi else echo "unpacking $f locally" unzip -ov "$f" fi fi echo "" done elif [ -n "$archive" ]; then echo "[archive creation not supported in $0]" fi