Extract files from a macOS installer .pkg


Want to extract files from a macOS installer package with a .pkg extension?

TL;DR: Just use tar... multiple times!

New installers are a single XAR (eXtensible ARchiver) file, with the extension .pkg. Within the single file, there may be more than one .pkg file, and within those folders, you'll find another file called Payload. Use tar xvf to extract each .pkg and Payload.

For example, download the Wine .pkg from WineHQ, then:

tar xvf winehq-staging-1.9.22.pkg

And you'll extract its contents:

x org.winehq.wine-staging32.pkg
x org.winehq.wine-staging32.pkg/PackageInfo
x org.winehq.wine-staging32.pkg/BOM
x org.winehq.wine-staging32.pkg/Payload
x Resources
x Resources/conclusion.html
x Resources/background.png
x Resources/welcome.html
x Distribution
x org.winehq.wine-staging-deps64.pkg
x org.winehq.wine-staging-deps64.pkg/PackageInfo
x org.winehq.wine-staging-deps64.pkg/BOM
x org.winehq.wine-staging-deps64.pkg/Payload
x org.winehq.wine-staging64.pkg
x org.winehq.wine-staging64.pkg/PackageInfo
x org.winehq.wine-staging64.pkg/BOM
x org.winehq.wine-staging64.pkg/Payload
x org.winehq.wine-staging-deps.pkg
x org.winehq.wine-staging-deps.pkg/PackageInfo
x org.winehq.wine-staging-deps.pkg/BOM
x org.winehq.wine-staging-deps.pkg/Payload
x org.winehq.wine-staging.pkg
x org.winehq.wine-staging.pkg/PackageInfo
x org.winehq.wine-staging.pkg/BOM
x org.winehq.wine-staging.pkg/Payload
x org.winehq.wine-staging.pkg/Scripts

So, to extract the files within a particular package:

cd org.winehq.wine-staging.pkg/
tar xvf Payload 

My initial search on Google pointed me to various rather complex alternatives, e.g. using xar, pbzx, or using cat Payload | gzip -d | cpio -id. Maybe this is required to both pack and unpack, but I don't know. See How to unpack and pack pkg file? on Stack Overflow or How to unpackage and repackage a pkg (OS X Lion+ / Xcode 4.3+)