使用 ZipFile 从 Python 中的 zip 文件中提取 .app
我正在尝试从他们的快照中提取 Chromium.app 的新版本,并且我可以很好地下载该文件,但是在提取它时,ZipFile 要么将其中的 chrome-mac 文件夹提取为文件,说目录不'我对 python 很陌生,所以这些错误对我来说没有什么意义。这是我到目前为止所拥有的。
import urllib2
response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST')
latestRev = response.read()
print latestRev
# we have the revision, now we need to download the zip and extract it
latestZip = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)), '~/Desktop/ChromiumUpdate/%i-update' % (int(latestRev)))
#declare some vars that hold paths n shit
workingDir = '/Users/slehan/Desktop/ChromiumUpdate/'
chromiumZipPath = '%s%i-update.zip' % (workingDir, (int(latestRev)))
chromiumAppPath = 'chrome-mac/' #the path of the chromium executable within the zip file
chromiumAppExtracted = '%s/Chromium.app' % (workingDir) # path of the extracted executable
output = open(chromiumZipPath, 'w') #delete any current file there
output.write(latestZip.read())
output.close()
# we have the .zip now we need to extract the Chromium.app file, it's in ziproot/chrome-mac/Chromium.app
import zipfile, os
zippedFile = open(chromiumZipPath)
zippedChromium = zipfile.ZipFile(zippedFile, 'r')
zippedChromium.extract(chromiumAppPath, workingDir)
#print zippedChromium.namelist()
zippedChromium.close()
#zippedChromium.close()
有什么想法吗?
I'm trying to extract new revisions of Chromium.app from their snapshots, and I can download the file fine, but when it comes to extracting it, ZipFile either extracts the chrome-mac folder within as a file, says that directories don't exist, etc. I am very new to python, so these errors make little sense to me. Here is what I have so far.
import urllib2
response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST')
latestRev = response.read()
print latestRev
# we have the revision, now we need to download the zip and extract it
latestZip = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)), '~/Desktop/ChromiumUpdate/%i-update' % (int(latestRev)))
#declare some vars that hold paths n shit
workingDir = '/Users/slehan/Desktop/ChromiumUpdate/'
chromiumZipPath = '%s%i-update.zip' % (workingDir, (int(latestRev)))
chromiumAppPath = 'chrome-mac/' #the path of the chromium executable within the zip file
chromiumAppExtracted = '%s/Chromium.app' % (workingDir) # path of the extracted executable
output = open(chromiumZipPath, 'w') #delete any current file there
output.write(latestZip.read())
output.close()
# we have the .zip now we need to extract the Chromium.app file, it's in ziproot/chrome-mac/Chromium.app
import zipfile, os
zippedFile = open(chromiumZipPath)
zippedChromium = zipfile.ZipFile(zippedFile, 'r')
zippedChromium.extract(chromiumAppPath, workingDir)
#print zippedChromium.namelist()
zippedChromium.close()
#zippedChromium.close()
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您似乎遇到了Python 中的错误。此其他问题详细介绍了问题和解决方法。您可以选择使用其中一种解决方法,或更新到 Python 2.6.5 或 2.7b2。
解决方法之一建议复制 修补的 zipfile.py 模块 来自固定的 Python。
祝你好运!
It seems you have encountered a bug in Python. This other question details the problem and workarounds. You can elect to use one of those workarounds, or update to Python 2.6.5 or 2.7b2.
One of the workarounds suggests copying the patched zipfile.py module from the fixed Python.
Best of luck!
这似乎对我有用:
This seems to be working for me:
这是另一个切入点 - 这是相同的技术,但它会遍历结果来证明它是有效的。
我运行时得到的输出是
Here's another cut - this is the same technique, but it walks the result to demonstrate that it works.
The output I get when I run it is
在 Python 中从 zip 中提取 .app 还存在另一个问题(通常的 zip 实用程序不会发生这种情况)。其他人似乎没有提到这一点....
app 可以通过这种方式停止提取后的功能,因为丢失了嵌套二进制文件的执行权限位。不过,您可以通过再次授予该权限来解决此问题。
这是我正在使用的一段松散的代码片段。根据您的目的需要修改此设置(或编写一个更通用的函数以更通用的方式处理这种情况):
我的程序已经知道在哪里可以找到
APP_PATH
(即在APP_PATH
中) >WORK_DIR)。不过,我必须把它拉上拉链,然后用鞋拔子把这个细节塞进去。我将我的 zip 命名为XXXXX.app.zip
。我在这里非常简单地解析了BIN_PATH
,无需知道 .app 内二进制文件的名称,因为我知道其中只有一个文件适合我的用例。我授予它完全(777)权限,因为我只是删除了脚本末尾的 .app 。There is another problem extracting an .app from a zip in Python (which doesn't happen with a typically zip utility). No one else seems to have mentioned this...
The .app can ceases to function post extraction this way, as a result of losing the execution permission bit on the nested binary. You can fix this though, by simply granting that again.
Here's a loose snippet of code that I'm using. Revise this as needed for your purposes (or write a more generic function to handle this situation in a more universal manner):
My program already knew where to expect the
APP_PATH
to be found (i.e. within theWORK_DIR
). I had to zip it up though, and shoe horn that detail in after the fact. I name my zip likeXXXXX.app.zip
. I resolve theBIN_PATH
here pretty simply without the need to know the name of binary inside the .app, because I know there is only going to be one file in there for my use case. I grant full (777) permissions to it, because I simply delete the .app at the end of my script anyway.