Skip to content

Instantly share code, notes, and snippets.

@yellowcrescent
Created April 14, 2021 04:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yellowcrescent/cd27604c6ab424c99caa673d45224a10 to your computer and use it in GitHub Desktop.
Save yellowcrescent/cd27604c6ab424c99caa673d45224a10 to your computer and use it in GitHub Desktop.
Extracting MacOS pkg files on Linux
#!/bin/bash
#
# Super basic MacOS .pkg file extractor using p7zip & cpio
# @yellowcrescent
#
# Usage: xpkg.sh MyPkg.pkg /path/to/output/dir
#
ZBIN=7z
PLFILE="Payload~"
SRCPKG="$1"
OUTDIR=$(readlink -m "${2:-.}")
if [[ -z "${SRCPKG}" || "${SRCPKG}" == "-h" || "${SRCPKG}" == "--help" ]]; then
echo "usage: $0 INPUT_PKG.pkg [OUTPUT_DIR]"
exit 2
fi
if [[ ! $(which ${ZBIN}) ]]; then
echo "7-zip is not installed (command ${ZBIN} not found). Try installing p7zip-full or similar package."
exit 3
fi
# Requires the full version of 7-zip (eg. "p7zip-full" or similar)
$ZBIN x "${SRCPKG}" "-o${OUTDIR}"
if [[ "$1" != 0 ]]; then
echo "Extraction failed."
fi
# Extract payload via cpio if it exists
CPIOFILE="${OUTDIR}/${PLFILE}"
if [[ -e "${CPIOFILE}" ]]; then
cpio -idmv -F "${CPIOFILE}" -D "${OUTDIR}"
fi
echo "Complete."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment