문제

Python 2.4 및 내장 사용 ZipFile 라이브러리, 비 압축 파일의 전체 내용을 메모리에 저장하려고하기 때문에 매우 큰 지퍼 파일 (1 또는 2GB 이상)을 읽을 수 없습니다. 이 작업을 수행하는 또 다른 방법이 있습니까 (타사 라이브러리 또는 다른 해킹으로), 아니면 "껍질을 벗기고"그런 식으로 압축을 풀어야합니다 (이것은 크로스 플랫폼이 아니 었습니다).

도움이 되었습니까?

해결책

다음은 큰 파일의 감압에 대한 개요입니다.

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

다른 팁

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 객체에서 데이터를 효율적으로 읽으려면 출력 파일에 복사하십시오.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top