如何在Python中解压非常大的文件?

21

使用Python 2.4和内置的ZipFile库时,我无法读取非常大的zip文件(大于1或2 GB),因为它要将未压缩文件的全部内容存储在内存中。是否有其他方法可以做到这一点(使用第三方库或其他黑科技),或者我必须通过"shell out"的方式解压缩它(这显然不是跨平台的)。

2个回答

19

以下是大文件解压的概述。

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)
    src.seek( m.header_offset )
    src.read( 30 ) # Good to use struct to unpack this.
    nm= src.read( len(m.filename) )
    if len(m.extra) > 0: ex= src.read( len(m.extra) )
    if len(m.comment) > 0: cm= src.read( 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( src.read( m.compress_size ) )
    out.write( result )
    result = decomp.flush()
    out.write( result )
    # end of the loop
    out.close()

zf.close()
src.close()

4
“@s-lott What does ex= src.read( len(m.extra) ) and cm= src.read( len(m.comment) ) what do you use the variables ex and cm for?”的意思是:ex= src.read( len(m.extra) )cm= src.read( len(m.comment) )语句的作用是什么?变量excm有什么用处?“What do you mean it's good to use a struct to unpack this? And what is the magic number 30 used for?”的意思是:“使用结构体进行解压缩是什么意思?魔术数字30用来做什么?” - Jonathan
每个文件的头部包含文件名,相对偏移量为30字节,参见https://en.wikipedia.org/wiki/Zip_(file_format)。除此之外,额外字段和注释字段并不相关,我们只需读取这些字节以前进到正确的位置。 - Benjamin

15

从Python 2.6开始,您可以使用ZipFile.open()打开文件句柄,并将内容有效地复制到您选择的目标文件中:

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
           try:
               os.makedirs(target_path)
           except (OSError, IOError) as err:
               # Windows may complain if the folders already exist
               if err.errno != errno.EEXIST:
                   raise
           continue
       with open(target_path, 'wb') as outfile, zfile.open(member) as infile:
           shutil.copyfileobj(infile, outfile)

这里使用shutil.copyfileobj()从打开的zipfile对象中高效地读取数据,并将其复制到输出文件中。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接