Python 自动获取DMG文件的内容

Python 自动获取DMG文件的内容,python,bash,dmg,Python,Bash,Dmg,我有一个Python程序,可以应用或删除文件中的多层加密。在这种情况下,我必须打开一个DMG来访问其中的ZIP文件。我使用了hidutil来制作DMG,但我一直在研究如何打开它并访问文件——我所看到的任何方法都依赖于挂载、访问挂载点和卸载,如果不智能地搜索它的挂载位置,我就无法做到这一点 我该怎么做?它不必使用Python,Bash解决方案也可以。基于此,您可以使用hdiutil info-plist通过Python获取dmg的装载点。通过这种方式,您可以智能地装载、查找装载点、列出内容和卸载。

我有一个Python程序,可以应用或删除文件中的多层加密。在这种情况下,我必须打开一个DMG来访问其中的ZIP文件。我使用了
hidutil
来制作DMG,但我一直在研究如何打开它并访问文件——我所看到的任何方法都依赖于挂载、访问挂载点和卸载,如果不智能地搜索它的挂载位置,我就无法做到这一点


我该怎么做?它不必使用Python,Bash解决方案也可以。

基于此,您可以使用
hdiutil info-plist
通过Python获取dmg的装载点。通过这种方式,您可以智能地装载、查找装载点、列出内容和卸载。

基于此,您可以使用
hdiutil info-plist
通过Python获取dmg的装载点。通过这种方式,您可以智能地装载、查找装载点、列出内容和卸载。

您可以使用7zip列出和提取DMG文件的内容-该网站是

在macOS上,7zip可以与自制软件一起安装,使用:

brew install p7zip
然后,如果您有DMG文件,您可以列出以下内容:

7z l SomeDisk.dmg
样本输出

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)

...
...

Modified = 2018-01-14 13:28:17

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2018-01-14 13:28:16 D....                            MyFunkyDMG
2018-01-14 13:28:16 D....                            MyFunkyDMG/.HFS+ Private Directory Data
2018-01-14 13:28:17 .....       524288       524288  MyFunkyDMG/.journal
2018-01-14 13:28:16 .....         4096         4096  MyFunkyDMG/.journal_info_block
2017-08-27 13:50:45 .....          255         4096  MyFunkyDMG/client.py
2017-08-27 13:49:22 .....          356         4096  MyFunkyDMG/server.py
2018-01-14 13:28:16 D....                            MyFunkyDMG/[HFS+ Private Data]
------------------- ----- ------------ ------------  ------------------------
2018-01-14 13:28:17             528995       536576  4 files, 3 folders

然后,您可以提取,比如说提取到一个名为
FRED
的新目录,方法是:

7z x -oFRED SomeDisk.dmg 

使用7zip的一个好处是,磁盘在装入时不会突然在桌面上闪烁。

您可以使用7zip列出并提取DMG文件的内容-网站是

在macOS上,7zip可以与自制软件一起安装,使用:

brew install p7zip
然后,如果您有DMG文件,您可以列出以下内容:

7z l SomeDisk.dmg
样本输出

7-Zip [64] 16.02 : Copyright (c) 1999-2016 Igor Pavlov : 2016-05-21
p7zip Version 16.02 (locale=utf8,Utf16=on,HugeFiles=on,64 bits,8 CPUs x64)

...
...

Modified = 2018-01-14 13:28:17

   Date      Time    Attr         Size   Compressed  Name
------------------- ----- ------------ ------------  ------------------------
2018-01-14 13:28:16 D....                            MyFunkyDMG
2018-01-14 13:28:16 D....                            MyFunkyDMG/.HFS+ Private Directory Data
2018-01-14 13:28:17 .....       524288       524288  MyFunkyDMG/.journal
2018-01-14 13:28:16 .....         4096         4096  MyFunkyDMG/.journal_info_block
2017-08-27 13:50:45 .....          255         4096  MyFunkyDMG/client.py
2017-08-27 13:49:22 .....          356         4096  MyFunkyDMG/server.py
2018-01-14 13:28:16 D....                            MyFunkyDMG/[HFS+ Private Data]
------------------- ----- ------------ ------------  ------------------------
2018-01-14 13:28:17             528995       536576  4 files, 3 folders

然后,您可以提取,比如说提取到一个名为
FRED
的新目录,方法是:

7z x -oFRED SomeDisk.dmg 

使用7zip的一个好处是,磁盘在装入时不会突然在桌面上闪烁。

下面是一个bash版本,它解析hdiutil输出以提取用于zip文件访问的装入点和用于随后分离的dev条目:

#!/bin/bash

dmg_path="$1"

# use process redirection to capture the mount point and dev entry
IFS=$'\n' read -rd '\n' mount_point dev_entry < <(
    # mount the diskimage; leave out -readonly if making changes to the filesystem
    hdiutil attach -readonly -plist "$dmg_path" | \

    # convert output plist to json
    plutil -convert json - -o - | \

    # extract mount point and dev entry
    jq -r '
        .[] | .[] |
        select(."volume-kind" == "hfs") |
        ."mount-point" + "\n" + ."dev-entry"
    '
)

# work with the zip file
ls "$mount_point/*.zip"

# unmount the disk image
hdiutil detach "$dev_entry"
#/bin/bash
dmg_path=“$1”
#使用进程重定向捕获装载点和dev条目

IFS=$'\n'read-rd'\n'mount_point dev_entry<下面是一个bash版本,它解析hdiutil输出以提取用于zip文件访问的装载点和用于随后分离的dev条目:

#!/bin/bash

dmg_path="$1"

# use process redirection to capture the mount point and dev entry
IFS=$'\n' read -rd '\n' mount_point dev_entry < <(
    # mount the diskimage; leave out -readonly if making changes to the filesystem
    hdiutil attach -readonly -plist "$dmg_path" | \

    # convert output plist to json
    plutil -convert json - -o - | \

    # extract mount point and dev entry
    jq -r '
        .[] | .[] |
        select(."volume-kind" == "hfs") |
        ."mount-point" + "\n" + ."dev-entry"
    '
)

# work with the zip file
ls "$mount_point/*.zip"

# unmount the disk image
hdiutil detach "$dev_entry"
#/bin/bash
dmg_path=“$1”
#使用进程重定向捕获装载点和dev条目

IFS=$'\n'read-rd'\n'mount\u point dev_entry/test.sh:第12行:jq:command not found
并从那里
ls://*.zip:没有这样的文件或目录
hdiutil:detach失败-没有这样的文件或目录
。这是在macOS上,这些软件包是默认安装的吗?对不起,我应该把jq列为依赖项。它太方便了,我开始想当然了。它类似于json的sed。如果您愿意,可以用XPathXML工具替换它。问题是,“hdiutil attach-plist”仅在已安装的dmg上运行时返回装载点详细信息。所以它失败了一次,但当您再次运行它时,它会工作。您必须再添加一个“hdiutil attach”才能正常工作。感谢您的提示!我得到错误
/test.sh:line 12:jq:command not found
,从那里
ls://*.zip:没有这样的文件或目录
hdiutil:detach失败-没有这样的文件或目录
。这是在macOS上,这些软件包是默认安装的吗?对不起,我应该把jq列为依赖项。它太方便了,我开始想当然了。它类似于json的sed。如果您愿意,可以用XPathXML工具替换它。问题是,“hdiutil attach-plist”仅在已安装的dmg上运行时返回装载点详细信息。所以它失败了一次,但当您再次运行它时,它会工作。您必须再添加一个“hdiutil attach”才能正常工作。感谢您的提示!是否有一种方法可以做到这一点,它已经包含在macOS上,而不是让用户下载其他工具?是否有一种方法可以做到这一点,它已经包含在macOS上,而不是让用户下载其他工具?