Unzip: Memory Error

mcl mcl.office at googlemail.com
Thu Aug 30 07:13:51 EDT 2007


On 29 Aug, 21:18, David Bolen <db3l.... at gmail.com> wrote:
> mcl <mcl.off... at googlemail.com> writes:
> > I am trying to unzip an 18mb zip containing just a single 200mb file
> > and I get a Memory Error.  When I run the code on a smaller file 1mb
> > zip, 11mb file, it works fine.
> (...)
> > def unzip_file_into_dir(file, dir):
> >    #os.mkdir(dir, 0777)
> >    zfobj = zipfile.ZipFile(file)
> >    for name in zfobj.namelist():
> >            if name.endswith('/'):
> >                    os.mkdir(os.path.join(dir, name))
> >            else:
> >                    outfile = open(os.path.join(dir, name), 'wb')
> >                    outfile.write(zfobj.read(name))
> >                    outfile.close()
>
> The "zfobj.read(name)" call is reading the entire file out of the zip
> into a string in memory.  It sounds like it's exceeding the resources
> you have available (whether overall or because the Apache runtime
> environment has stricter limits).
>
> You may want to peek at a recent message from me in the "Unable to
> read large files from zip" thread, as the suggestion there may also be
> suitable for your purposes.
>
> http://groups.google.com/group/comp.lang.python/msg/de04105c170fc805?...
> -- David

David,

Thank you. I read your post and I basically understood the concept,
butI could not get my head around the code, I need to write for my
solution. (Newbie and a bit long in the tooth)

To solve my problem, I think my best approach would be to read my
zipped file / files from the zip archive when I need them.  Max three
users, occasional use.  So no big overloading of host's server.

pseudo code

zfhdl = zopen(zip,filename)          # Open File in Zip Archive for
Reading

while True:
    ln = zfhdl.readline()            # Get nextline of file
    if not ln:                       # if EOF file
      break
    dealwithline(ln)                 # do whatever is necessary with
file
zfhdl.close

That is probably over simplified, and probably wrong but you may get
the idea of what I am trying to achieve.

Richard




More information about the Python-list mailing list