#!/bin/sh # untar --- extract contents of archive files into a single directory # Author: Noah Friedman # Created: 1996-01-15 # Public domain # $Id: untar,v 1.18 2010/03/23 00:18:40 friedman Exp $ # Commentary: # This started out just unpacking tar files, but now supports more formats. # Code: tmpdir=untar$$ pre() { pkgdir=`echo "$file" | sed -e 's=.*/==' -e 's=\.[^.]*$=='` case $pkgdir in *.tar | *.xtar ) pkgdir=`echo "$pkgdir" | sed -e 's=\.[^.]*$=='` ;; esac if [ -d $pkgdir ]; then echo $pkgdir: directory already exists 1>&2 return 1 fi if [ -d $tmpdir ]; then echo $tmpdir: temp directory already exists, and it shouldn\'t 1>&2 return 1 fi mkdir -p $tmpdir || return 1 cd $tmpdir || return 1 case $file in /* ) : ;; * ) file=../$file ;; esac return 0 } post() { nfiles=`ls -1A | wc -l` ndir= if [ $nfiles -eq 1 ]; then ndir=`ls -1A` fi if [ $nfiles -gt 1 ] || { [ $nfiles -eq 1 ] && ! [ ".$ndir" = ".$pkgdir" ]; } then cd .. && mv $tmpdir $pkgdir elif [ $nfiles -eq 1 ]; then mv $ndir .. && cd .. && rmdir $tmpdir else echo $tmpdir: empty directory\? 1>&2 cd .. && rmdir $tmpdir return 1 fi } extract() { case $file in *.t[ag]z | *.tar.gz | *.tar.[Zz] | *.xtar.gz | *.xtar.[Zz] | *.nif ) ${GZIP-gzip} -dc "$file" | ${TAR-tar} -xpvf - ;; *.tbz | *.tbz2 | *.tar.bz2 | *.xtar.bz2 | *.tar.bz | *.cbt ) ${BZIP2-bzip2} -dc "$file" | ${TAR-tar} -xpvf - ;; *.tar.lzma ) ${LZMA-lzma} -dc "$file" | ${TAR-tar} -xpvf - ;; *.tar | *.xtar ) ${TAR-tar} -xpvf "$file" ;; *.zip | *.cbz | *.fbu | *.jar | *.apk ) ${UNZIP-unzip} -o -d . "$file" ;; *.rpm ) ${RPM2CPIO-rpm2cpio} "$file" | ${CPIO-cpio} -dimv --no-absolute-filenames ;; *.lzh ) ${LHA-lha} x "$file" ;; *.rar | *.cbr ) ${UNRAR-unrar} x "$file" ;; *.a | *.lib ) ${AR-ar} xv "$file" ;; *.msi ) # ${MSIEXEC-msiexec} /a "$file" /qb TARGETDIR=./$pkgdir ;; 7z x "$file" ;; esac } main() { for file in ${1+"$@"}; do (pre; extract; post) done } main ${1+"$@"} # eof