Unzipping a .zip properly, and from a remote URL
Christopher Culver
crculver at christopherculver.com
Tue Feb 3 08:07:09 EST 2009
Returning to Python after several years away, I'm working on a little
script that will download a ZIP archive from a website and unzip it to
a mounted filesystem. The code is below, and it works so far, but I'm
unsure of a couple of things.
The first is, is there a way to read the .zip into memory without the
use of a temporary file? If I do archive = zipfile.ZipFile(remotedata.read())
directly without creating a temporary file, the zipfile module
complains that the data is in the wrong string type.
The second issue is that I don't know if this is the correct way to
unpack a file onto the filesystem. It's strange that the zipfile
module has no one simple function to unpack a zip onto the disk. Does
this code seem especially liable to break?
try:
remotedata = urllib2.urlopen(theurl)
except IOError:
print("Network down.")
sys.exit()
data = os.tmpfile()
data.write(remotedata.read())
archive = zipfile.ZipFile(data)
if archive.testzip() != None:
print "Invalid zipfile"
sys.exit()
contents = archive.namelist()
for item in contents:
try:
os.makedirs(os.path.join(mountpoint, os.path.dirname(item)))
except OSError:
# OSError means that the dir already exists, but no matter.
pass
if item[-1] != "/":
outputfile = open(os.path.join(mountpoint, item), 'w')
outputfile.write(archive.read(item))
outputfile.close()
More information about the Python-list
mailing list