python解压zip,如何解压缩非常大的文件在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).

解决方案

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)

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()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值