I have a pkg file created by Install Maker for Mac. I want to replace one file in pkg. But I must do this under Linux system, because this is a part of download process. When user starts to download file server must replace one file in pkg. I have a solution how unpack pkg and replace a file but I dont know how pack again to pkg. http://emresaglam.com/blog/1035 http://ilostmynotes.blogspot.com/2012/06/mac-os-x-pkg-bom-files-package.html
-
1(For future visitors) http://gabrielrinaldi.me/how-to-install-jdk-7-on-yosemite-10-10/ might give you insight – james_womack Feb 11 '15 at 23:49
-
MacOS appears to come with the xar command. – MikeP Aug 26 '16 at 20:39
7 Answers
Packages are just .xar archives with a different extension and a specified file hierarchy. Unfortunately, part of that file hierarchy is a cpio.gz archive of the actual installables, and usually that's what you want to edit. And there's also a Bom file that includes information on the files inside that cpio archive, and a PackageInfo file that includes summary information.
If you really do just need to edit one of the info files, that's simple:
mkdir Foo
cd Foo
xar -xf ../Foo.pkg
# edit stuff
xar -cf ../Foo-new.pkg *
But if you need to edit the installable files:
mkdir Foo
cd Foo
xar -xf ../Foo.pkg
cd foo.pkg
cat Payload | gunzip -dc |cpio -i
# edit Foo.app/*
rm Payload
find ./Foo.app | cpio -o | gzip -c > Payload
mkbom Foo.app Bom # or edit Bom
# edit PackageInfo
rm -rf Foo.app
cd ..
xar -cf ../Foo-new.pkg
I believe you can get mkbom (and lsbom) for most linux distros. (If you can get ditto, that makes things even easier, but I'm not sure if that's nearly as ubiquitously available.)

- 354,177
- 51
- 601
- 671
-
Yes. I have tried this method before (only unpack/pack without changing any files), but after installed this pkg nothing happend. In console I got this message: posix_spawn("/Library/Application Support/Project1/Project1.app/Contents/MacOS/Project1", ...): No such file or directory. I have investigated that old Payload file has /./Project1.app/... and new payload has /Project1.app/... inside. – dream2work Jul 03 '12 at 08:33
-
Well, that last part is easy. I don't know if it makes a difference, but it might (maybe because the Bom and the Payload don't match?). Just use ./Foo.app instead of Foo.app in the find|cpio|gzip command. I'll edit the answer. – abarnert Jul 03 '12 at 16:58
-
5@abarnert: instead of `cat Payload | gunzip -dc |cpio -i` you could simply use `tar xzvf Payload` – ccpizza May 21 '14 at 16:52
-
I get an error running `cat Payload | gunzip -dc |cpio -i`: `gunzip: unknown compression format`;`0 blocks`. I'm trying to open `BSD.pkg` from OS X Yosemite installation package. – shrx Oct 29 '14 at 10:43
-
@ccpizza Your suggestion also doesn't work, I get: `tar: Unrecognized archive format`;`tar: Error exit delayed from previous errors.`. – shrx Oct 29 '14 at 10:45
-
@shrx: It's possible that Apple has extended the `.pkg` format in 10.10, or that this is not actually a package file (there are two other file types plus a directory bundle type that can be given the `.pkg` or `.mpkg` extension and work…). – abarnert Oct 29 '14 at 18:56
-
@ccpizza: New BSD `tar` can handle cpio transparently. (It can also handle gzip transparently, so you can just use `xvf` without the `z`.) But I don't think GNU `tar` can, and I think many linux distros still use GNU `tar`. (Old BSD and SysV `tar` definitely can't handle `cpio`, but those are less likely to be an issue for Linux users…) – abarnert Oct 29 '14 at 18:59
-
@abarnert this particular `.pkg` was extractable with this method at least in 10.7, so it should still be a package. Looks like a change in format. – shrx Oct 29 '14 at 19:14
-
@shrx: It sounds like you want to create a new question asking about how Apple has changed their pkg format in 10.10 and how you can deal with it (along with whatever information, links, etc. you have). If it's documented and you can't find it by searching, someone else will. If it's not documented, someone will reverse engineer it. – abarnert Oct 29 '14 at 19:19
-
1
-
hey @abarnert, I am trying this. But even if I just use xar unpack the .pkg file, change NOTHING, and use xar -cf pack it again. The package file is not ok to install, when I try to install it on mac, I got 'com.apple.installer.pagecontroller error -1' could you please suggest on this? – lucky_start_izumi Oct 08 '15 at 22:13
-
when i tape : xar -cf ../new_name.pkg i had this error : xar -cf new_name.pkg Usage: xar -[ctx][v] -f
... – Badre Aug 12 '16 at 12:33 -
Here is a bash script inspired by abarnert's answer which will unpack a package named MyPackage.pkg
into a subfolder named MyPackage_pkg
and then open the folder in Finder.
#!/usr/bin/env bash
filename="$*"
dirname="${filename/\./_}"
pkgutil --expand "$filename" "$dirname"
cd "$dirname"
tar xvf Payload
open .
Usage:
pkg-upack.sh MyPackage.pkg
Warning: This will not work in all cases, and will fail with certain files, e.g. the PKGs inside the OSX system installer. If you want to peek inside the pkg file and see what's inside, you can try SuspiciousPackage (free app), and if you need more options such as selectively unpacking specific files, then have a look at Pacifist (nagware).

- 28,968
- 18
- 162
- 169
-
2Use pbzx -n macOSUpd10.12.1.pkg/Payload | cpio -i for newer archives (taken from here: https://stackoverflow.com/a/41598227/5688277) – Adrian O'Connor Nov 30 '17 at 12:10
You might want to look into my fork of pbzx
here: https://github.com/NiklasRosenstein/pbzx
It allows you to stream pbzx files that are not wrapped in a XAR archive. I've experienced this with recent XCode Command-Line Tools Disk Images (eg. 10.12 XCode 8).
pbzx -n Payload | cpio -i

- 16,299
- 28
- 108
- 203
In addition to what @abarnert said, I today had to find out that the default cpio
utility on Mountain Lion uses a different archive format per default (not sure which), even with the man page stating it would use the old cpio/odc format. So, if anyone stumbles upon the cpio read error: bad file format
message while trying to install his/her manipulated packages, be sure to include the format in the re-pack step:
find ./Foo.app | cpio -o --format odc | gzip -c > Payload

- 190
- 1
- 7
@shrx I've succeeded to unpack the BSD.pkg (part of the Yosemite installer) by using "pbzx" command.
pbzx <pkg> | cpio -idmu
The "pbzx" command can be downloaded from the following link:

- 385
- 4
- 8
Bash script to extract pkg: (Inspired by this answer:https://stackoverflow.com/a/23950738/16923394)
Save the following code to a file named pkg-upack.sh on the $HOME/Downloads folder
#!/usr/bin/env bash
filename="$*"
dirname="${filename/\./_}"
mkdir "$dirname"
# pkgutil --expand "$filename" "$dirname"
xar -xf "$filename" -C "$dirname"
cd "$dirname"/*.pkg
pwd
# tar xvf Payload
cat Payload | gunzip -dc |cpio -i
# cd usr/local/bin
# pwd
# ls -lt
# cp -i * $HOME/Downloads/
Uncomment the last four lines, if you are using a rudix package.
Usage:
cd $HOME/Downloads
chmod +x ./pkg-upack.sh
./pkg-upack.sh MyPackage.pkg
This was tested with the ffmpeg and mawk package from rudix.org (https://rudix.org) search for ffmpeg and mawk packages on this site.
Source : My open source projects : https://sourceforge.net/u/nathan-sr/profile/

- 29
- 2
If you are experiencing errors during PKG installation following the accepted answer, I will give you another procedure that worked for me (please note the little changes to xar, cpio and mkbom commands):
mkdir Foo
cd Foo
xar -xf ../Foo.pkg
cd foo.pkg
cat Payload | gunzip -dc | cpio -i
# edit Foo.app/*
rm Payload
find ./Foo.app | cpio -o --format odc --owner 0:80 | gzip -c > Payload
mkbom -u 0 -g 80 Foo.app Bom # or edit Bom
# edit PackageInfo
rm -rf Foo.app
cd ..
xar --compression none -cf ../Foo-new.pkg
The resulted PKG will have no compression, cpio now uses odc format and specify the owner of the file as well as mkbom.

- 30,962
- 25
- 85
- 135

- 2,431
- 2
- 7
- 9