The "DECOMPRESSION" test fails on CygWin, one of the most potentiaally useful platforms for it, due to the "grep" check for "xz" being case sensitive. The result of the "COMPRESSION:" check is:
COMPRESSION='/dev/stdin: XZ compressed data'
Simply replacing 'grep -q' with 'grep -q -i' everywhere seems to resolve the issue well.
I've done a few updates, particularly adding some comments and using "case" instead of stacked "if" statements, and included that fix below
#!/bin/sh
#
# rpm2cpio.sh - extract 'cpio' contents of RPM
#
# Typical usage: rpm2cpio.sh rpmname | cpio -idmv
#
if [ "$# -ne 1" ]; then
echo "Usage: $0 file.rpm" 1>&2
exit 1
fi
rpm="$1"
if [ -e "$rpm" ]; then
echo "Error: missing $rpm"
fi
leadsize=96
o=`expr $leadsize + 8`
set `od -j $o -N 8 -t u1 $rpm`
il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`
# echo "sig il: $il dl: $dl"
sigsize=`expr 8 + 16 \* $il + $dl`
o=`expr $o + $sigsize + \( 8 - \( $sigsize \% 8 \) \) \% 8 + 8`
set `od -j $o -N 8 -t u1 $rpm`
il=`expr 256 \* \( 256 \* \( 256 \* $2 + $3 \) + $4 \) + $5`
dl=`expr 256 \* \( 256 \* \( 256 \* $6 + $7 \) + $8 \) + $9`
# echo "hdr il: $il dl: $dl"
hdrsize=`expr 8 + 16 \* $il + $dl`
o=`expr $o + $hdrsize`
EXTRACTOR="dd if=$rpm ibs=$o skip=1"
COMPRESSION=`($EXTRACTOR |file -) 2>/dev/null`
DECOMPRESSOR="cat"
case $COMPRESSION in
*gzip*|*GZIP*)
DECOMPRESSOR=gunzip
;;
*bzip2*|*BZIP2*)
DECOMPRESSOR=bunzip2
;;
*xz*|*XZ*)
DECOMPRESSOR=unxz
;;
*cpio*|*cpio*)
;;
*)
# Most versions of file don't support LZMA, therefore we assume
# anything not detected is LZMA
DECOMPRESSOR="`which unlzma 2>/dev/null`"
case "$DECOMPRESSOR" in
/*)
DECOMPRESSOR="$DECOMPRESSOR"
;;
*)
DECOMPRESSOR=`which lzmash 2>/dev/null`
case "$DECOMPRESSOR" in
/* )
DECOMPRESSOR="lzmash -d -c"
;;
* )
echo "Warning: DECOMPRESSOR not found, assuming 'cat'" 1>&2
;;
esac
;;
esac
esac
$EXTRACTOR 2>/dev/null | $DECOMPRESSOR