Reputation: 216
How to extract content of DMG without mounting it? I want add autoupdate system to my application. It downloads DMG from website, then extract new version of application from it.
Upvotes: 17
Views: 51857
Reputation: 688
import subprocess
import re
import shutil
def extract_dmg(download_path, extract_path):
# Mount the DMG file and capture the output
output = subprocess.check_output(['hdiutil', 'attach', '-nobrowse', '-readonly', download_path]).decode('utf-8')
# Extract the mounted volume name from the output
mounted_volume_name = re.search(r'/Volumes/([^ ]*)', output).group(1)
# Use the mounted volume name to copy files
shutil.copytree(f'/Volumes/{mounted_volume_name}/', extract_path)
# Detach the volume
subprocess.run(['hdiutil', 'detach', f'/Volumes/{mounted_volume_name}'])
# Usage
download_path = "/path/to/your/download.dmg"
extract_path = "/path/to/extract"
extract_dmg(download_path, extract_path)
Upvotes: 1
Reputation: 21
The best and easy way to extract a .dmg is to use the hdituil and the pkgutil. The .dmg contents has a .pkg files in it. Two options is to go from .dmg directly and extract the files or first mount the .dmg and then extract the .pkg. Here is a link to glorious post that saves me from installing MacOS installer when all hope was lost. Credit to this amazing post won't stole that also it's a result of good searching query lol https://medium.com/macoclock/extracting-applications-and-other-files-from-pkg-files-on-macos-f885376f1ef3
Upvotes: 0
Reputation: 2137
Dmg is just a format used for MacOS. It's a not compressed file format like zip
or tar.gz
You have several choices to mount it. Here are some options.
hdiutil attach your.dmg
to mount the dmg file. After mounting on it,
operating your command line to extract the files you want out.Upvotes: 4
Reputation: 850
Some .dmg
files can be extracted by using 7-zip.
$ 7z x your.dmg
$ ls
your.dmg
0.MBR
1.Primary GPT Header
2.Primary GPT Table
3.free
4.hfs
5.free
6.Backup GPT Table
7.Backup GPT Header
...and after extracted the 4.hfs
file:
$ 7z x 4.hfs
...you'll get the content of the .dmg
file.
You could also mount the .dmg
in Mac OS X using hdiutil
command (which is also used by Homebrew Cask).
Please refer to this Ask Ubuntu question for more use cases on Linux.
Upvotes: 17
Reputation: 4343
Doing that is working counter to the design of DMGs, so it's very painful. DMGs are designed to be mounted, that is their whole raison d'être. You would need to rely on experimental code if you wanted to do something like that. You are probably better served by using a non-DMG archive such as a zip file, or by making your automatic updating process download, mount, and unmount the DMG file you provide.
Upvotes: -5