memory error with zipfile module

Fredrik Lundh fredrik at pythonware.com
Wed Jun 21 12:34:51 EDT 2006


Hari Sekhon wrote:

> I've seen people using everything from zip to touch, either out of 
> laziness or out of the fact it wouldn't work very well in python, this 
> zip case is a good example.

so based on a limitation in one library, and some random code you've 
seen on the internet, you're making generalizations about the language ?

the zip case is a pretty lousy example, btw; after all, using the 
existing API, it's not that hard to implement an *incremental* read 
function if the provided read-into-string version isn't sufficient:

     import zipfile, zlib

     ##
     # Given a 'zip' instance, copy data from the 'name' to the
     # 'out' stream.

     def explode(out, zip, name):

	zinfo = zip.getinfo(name)

	if zinfo.compress_type == zipfile.ZIP_STORED:
	    decoder = None
	elif zinfo.compress_type == zipfile.ZIP_DEFLATED:
	    decoder = zlib.decompressobj(-zlib.MAX_WBITS)
	else:
	    raise zipfile.BadZipFile("unsupported compression method")

	zip.fp.seek(zinfo.file_offset)

	size = zinfo.compress_size

	while 1:
	    data = zip.fp.read(min(size, 8192))
	    if not data:
		break
	    size -= len(data)
	    if decoder:
		data = decoder.decompress(data)
	    out.write(data)

	if decoder:
	    out.write(decoder.decompress('Z'))
	    out.write(decoder.flush())

</F>




More information about the Python-list mailing list