Foolproof way of extracting .pkg files I know there are some paid GUI tools that let you do this, but I'm not interested in those.

Currently, I'm doing this:

gzcat Contents/Archive.pax.gz | pax -r

However, there are some pkg files that are now xar archives and I need pkgutil to extract them. And there's probably other permutations of pkg spec that I'm not aware of.

Bottom line is that I can always find some way to manually get the stuff out of the pkg, but I'd like a one-shot solution that would do this automatically without worrying about the specific pkg implementation, say:

unpkg CrappyPackage.pkg

Does such a thing exist?

Perhaps the most foolproof way is to use the system installer itself but that seems to always need a full system drive as its target. Would be really useful if you could do something along the lines of:

installer -pkg CrappyPackage.pkg -target SomeEmptySandbox.dmg

I know there is this question: How can I open a .pkg file manually?
However, all the offered solutions there are pretty manual and I'm doing those right now anyways.
It's easy enough to create a shell script which checks for a few conditions and then takes the appropriate action.

I've put mine at http://dl.dropbox.com/u/18414/bin/unpkg.sh but will copy the initial version below:

#!/bin/zsh
#
#   Author:     Timothy J. Luoma
#   Email:      luomat at gmail dot com
#   Date:       2011-12-29
#
#   Purpose:    extract .pkg files, either xar archives or directory
#               
#   URL:       http://dl.dropbox.com/u/18414/bin/unpkg.sh
#   
#   See also:  http://apple.stackexchange.com/questions/34957/foolproof-way-of-extracting-pkg-files

NAME="$0:t"

    # Brief explanation of how program works
USAGE="$NAME is meant to extract package files. (Filenames must end in .pkg). Can handle both .pkg directories or xar archives.

Usage: $NAME /path/to/foo.pkg
"

    # in case of emergency, report error and exit
die ()
{
    echo "$NAME: $@"
    exit 1
}

    # if not given any input, explain how to use it
if [ "$#" = "0" ]
then
        die "$USAGE"    
fi

    # put whatever we get as an argument into a variable
INPUT="$@"

    # does the input actual refer to an existing file?
    # if not, exit
[[ -e "$INPUT" ]]       || die "$INPUT does not exist"

    # get the extension of the input. i.e. if we are
    # given "foo.pkg" this will just be "pkg"
EXT="$INPUT:e"

    # check to make sure that the extension is 'pkg'
    # because that's what we're looking for
[[ "$EXT" = "pkg" ]]    || die "$INPUT is not a pkg"

TYPE=`file -b "$INPUT"`

case "$TYPE" in 
    directory)
                gzcat "$INPUT/Contents/Archive.pax.gz" | pax -r

    ;;

    "xar archive - version 1")
                # Put whatever commands you want here
                :
    ;;


    *)
            die "$USAGE"    
    ;;

esac



exit 0
#EOF


zsh has been included in Mac OS X for several releases now. I prefer it over /bin/sh because… well, let's not get into a "my shell is better than your shell" war, because my shell would win :-) 

In any event, zsh makes things a little easier and a little nicer, and it's included, so there's no reason not to use it, IMO. Especially for a OS X-specific script like this.

This was tested on 10.7.