2010-05-28 67 views
1

我試圖從它們的快照中提取Chromium.app的新修訂版,並且我可以很好地下載該文件,但是當提取它時,ZipFile要麼將文件內的chrome-mac文件夾提取爲文件,說目錄不存在等。我對python非常陌生,所以這些錯誤對我來說毫無意義。這是我到目前爲止。使用ZipFile從一個zip文件中提取一個.app應用ZipFile

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() 

任何想法?

回答

4

看來你遇到了bug in Python。這other question詳細說明了問題和解決方法。您可以選擇使用其中一種解決方法,或者更新到Python 2.6.5或2.7b2。

其中一種解決方法是建議從固定Python複製patched zipfile.py module

祝你好運!

+0

好了,我的屁股和我打電話給蘇珊,我升級了,現在可以工作了。非常感謝! – skylerl 2010-05-29 14:38:05

0

這似乎是爲我工作:

import os 
import urllib2 
import zipfile 
from StringIO import StringIO 

response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST') 
latestRev = response.read() 
print 'getting revision', latestRev 

# we have the revision, now we need to download the zip and extract it 
locRef='http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)) 
latestZip = StringIO(urllib2.urlopen(locRef).read()) 

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/ 
zippedChromium = zipfile.ZipFile(latestZip) 
# find all zip members in chrome-mac/Chromium.app 
members = [m for m in zippedChromium.namelist() if m.startswith('chrome-mac/Chromium.app/')] 
#zippedChromium.extract(chromiumAppPath, workingDir) 
target = 'chromium-%s' % latestRev 
if os.path.isdir(target): 
    print 'destination already exists, exiting' 
    raise SystemExit(1) 
os.makedirs(target) 
zippedChromium.extractall(target, members) 

#zippedChromium.close() 
+0

當我運行它時,它返回:目標已經存在,正在退出 – skylerl 2010-05-28 11:31:47

+0

我注意到了引發SystemExit(1)並且它使目錄,但是當它提取.app時,它並沒有得到整個東西,只是文件本身。使用.apps時,在被調用的內容中有一個文件夾,根本沒有被下載。 – skylerl 2010-05-28 13:28:32

+0

而不是註釋掉SystemExit,在運行腳本之前刪除chromium-NNNNN。運行腳本時,它將創建目錄chromium-NNNNN並將chrome-mac/Chromium.app/*提取到chromium-NNNNN。 – 2010-05-28 14:20:02

0

這裏的另一個切 - 這是相同的技術,但它走的結果證明,它的作品。

import os 
import urllib2 
import zipfile 
from StringIO import StringIO 

response = urllib2.urlopen('http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/LATEST') 
latestRev = response.read() 
print 'getting revision', latestRev 

# we have the revision, now we need to download the zip and extract it 
locRef='http://build.chromium.org/buildbot/snapshots/chromium-rel-mac/%i/chrome-mac.zip' % (int(latestRev)) 
latestZip = StringIO(urllib2.urlopen(locRef).read()) 

# we have the .zip now we need to extract the Chromium.app file, it's in chrome-mac/Chromium.app/ 
zippedChromium = zipfile.ZipFile(latestZip) 
# find all zip members in chrome-mac/Chromium.app 
members = [m for m in zippedChromium.namelist() if m.startswith('chrome-mac/Chromium.app/')] 
#zippedChromium.extract(chromiumAppPath, workingDir) 
target = 'chromium-%s' % latestRev 
if os.path.isdir(target): 
    print 'destination already exists, exiting' 
    raise SystemExit(1) 
os.makedirs(target) 
zippedChromium.extractall(target, members) 

lengths = [ 
    (len(dirnames), len(filenames)) 
    for dirpath, dirnames, filenames in os.walk(target) 
    ] 
dirlengths, filelengths = zip(*lengths) 
ndirs = sum(dirlengths) 
nfiles = sum(filelengths) 
print 'extracted %(nfiles)d files in %(ndirs)d dirs' % vars() 
#zippedChromium.close() 

當我運行它,我得到的輸出是

> .\getapp.py 
getting revision 48479 
extracted 537 files in 184 dirs 
+0

same thing =(IOError:[Errno 20]不是目錄:'鉻-48479/chrome-mac/Chromium.app /目錄' – skylerl 2010-05-28 15:19:23

相關問題