How do you unzip very large files in python?

Posted on

Question :

How do you unzip very large files in python?

Using python 2.4 and the built-in ZipFile library, I cannot read very large zip files (greater than 1 or 2 GB) because it wants to store the entire contents of the uncompressed file in memory. Is there another way to do this (either with a third-party library or some other hack), or must I “shell out” and unzip it that way (which isn’t as cross-platform, obviously).

Answer #1:

Here’s an outline of decompression of large files.

import zipfile
import zlib
import os

src = open( doc, "rb" )
zf = zipfile.ZipFile( src )
for m in  zf.infolist():

    # Examine the header
    print m.filename, m.header_offset, m.compress_size, repr(m.extra), repr(m.comment) m.header_offset ) 30 ) # Good to use struct to unpack this.
    nm= len(m.filename) )
    if len(m.extra) > 0: ex= len(m.extra) )
    if len(m.comment) > 0: cm= len(m.comment) ) 

    # Build a decompression object
    decomp= zlib.decompressobj(-15)

    # This can be done with a loop reading blocks
    out= open( m.filename, "wb" )
    result= decomp.decompress( m.compress_size ) )
    out.write( result )
    result = decomp.flush()
    out.write( result )
    # end of the loop

Answered By: S.Lott

Answer #2:

As of Python 2.6, you can use to open a file handle on a file, and copy contents efficiently to a target file of your choosing:

import errno
import os
import shutil
import zipfile

TARGETDIR = '/foo/bar/baz'

with open(doc, "rb") as zipsrc:
    zfile = zipfile.ZipFile(zipsrc)
    for member in zfile.infolist():
       target_path = os.path.join(TARGETDIR, member.filename)
       if target_path.endswith('/'):  # folder entry, create
           except (OSError, IOError) as err:
               # Windows may complain if the folders already exist
               if err.errno != errno.EEXIST:
       with open(target_path, 'wb') as outfile, as infile:
           shutil.copyfileobj(infile, outfile)

This uses shutil.copyfileobj() to efficiently read data from the open zipfile object, copying it over to the output file.

Answered By: Martijn Pieters

Leave a Reply

Your email address will not be published.