<s> """<STR_LIT>""" <EOL> import warnings <EOL> import zipfile <EOL> import os . path <EOL> import zlib <EOL> import struct <EOL> _fileHeaderSize = struct . calcsize ( zipfile . structFileHeader ) <EOL> class ChunkingZipFile ( zipfile . ZipFile ) : <EOL> """<STR_LIT>""" <EOL> def readfile ( self , name ) : <EOL> """<STR_LIT>""" <EOL> if self . mode not in ( "<STR_LIT:r>" , "<STR_LIT:a>" ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> if not self . fp : <EOL> raise RuntimeError ( <EOL> "<STR_LIT>" ) <EOL> zinfo = self . getinfo ( name ) <EOL> self . fp . seek ( zinfo . header_offset , <NUM_LIT:0> ) <EOL> fheader = self . fp . read ( _fileHeaderSize ) <EOL> if fheader [ <NUM_LIT:0> : <NUM_LIT:4> ] != zipfile . stringFileHeader : <EOL> raise zipfile . BadZipfile ( "<STR_LIT>" ) <EOL> fheader = struct . unpack ( zipfile . structFileHeader , fheader ) <EOL> fname = self . fp . read ( fheader [ zipfile . _FH_FILENAME_LENGTH ] ) <EOL> if fheader [ zipfile . _FH_EXTRA_FIELD_LENGTH ] : <EOL> self . fp . read ( fheader [ zipfile . _FH_EXTRA_FIELD_LENGTH ] ) <EOL> if fname != zinfo . orig_filename : <EOL> raise zipfile . BadZipfile ( <EOL> '<STR_LIT>' % ( <EOL> zinfo . orig_filename , fname ) ) <EOL> if zinfo . compress_type == zipfile . ZIP_STORED : <EOL> return ZipFileEntry ( self , zinfo . compress_size ) <EOL> elif zinfo . compress_type == zipfile . ZIP_DEFLATED : <EOL> return DeflatedZipFileEntry ( self , zinfo . compress_size ) <EOL> else : <EOL> raise zipfile . BadZipfile ( <EOL> "<STR_LIT>" % <EOL> ( zinfo . compress_type , name ) ) <EOL> class _FileEntry ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , chunkingZipFile , length ) : <EOL> """<STR_LIT>""" <EOL> self . chunkingZipFile = chunkingZipFile <EOL> self . fp = self . chunkingZipFile . fp <EOL> self . length = length <EOL> self . finished = <NUM_LIT:0> <EOL> self . closed = False <EOL> def isatty ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . closed = True <EOL> self . finished = <NUM_LIT:1> <EOL> del self . fp <EOL> def readline ( self ) : <EOL> """<STR_LIT>""" <EOL> bytes = "<STR_LIT>" <EOL> for byte in iter ( lambda : self . read ( <NUM_LIT:1> ) , "<STR_LIT>" ) : <EOL> bytes += byte <EOL> if byte == "<STR_LIT:\n>" : <EOL> break <EOL> return bytes <EOL> def next ( self ) : <EOL> """<STR_LIT>""" <EOL> nextline = self . readline ( ) <EOL> if nextline : <EOL> return nextline <EOL> raise StopIteration ( ) <EOL> def readlines ( self ) : <EOL> """<STR_LIT>""" <EOL> return list ( self ) <EOL> def xreadlines ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self <EOL> class ZipFileEntry ( _FileEntry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , chunkingZipFile , length ) : <EOL> _FileEntry . __init__ ( self , chunkingZipFile , length ) <EOL> self . readBytes = <NUM_LIT:0> <EOL> def tell ( self ) : <EOL> return self . readBytes <EOL> def read ( self , n = None ) : <EOL> if n is None : <EOL> n = self . length - self . readBytes <EOL> if n == <NUM_LIT:0> or self . finished : <EOL> return '<STR_LIT>' <EOL> data = self . chunkingZipFile . fp . read ( <EOL> min ( n , self . length - self . readBytes ) ) <EOL> self . readBytes += len ( data ) <EOL> if self . readBytes == self . length or len ( data ) < n : <EOL> self . finished = <NUM_LIT:1> <EOL> return data <EOL> class DeflatedZipFileEntry ( _FileEntry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , chunkingZipFile , length ) : <EOL> _FileEntry . __init__ ( self , chunkingZipFile , length ) <EOL> self . returnedBytes = <NUM_LIT:0> <EOL> self . readBytes = <NUM_LIT:0> <EOL> self . decomp = zlib . decompressobj ( - <NUM_LIT:15> ) <EOL> self . buffer = "<STR_LIT>" <EOL> def tell ( self ) : <EOL> return self . returnedBytes <EOL> def read ( self , n = None ) : <EOL> if self . finished : <EOL> return "<STR_LIT>" <EOL> if n is None : <EOL> result = [ self . buffer , ] <EOL> result . append ( <EOL> self . decomp . decompress ( <EOL> self . chunkingZipFile . fp . read ( <EOL> self . length - self . readBytes ) ) ) <EOL> result . append ( self . decomp . decompress ( "<STR_LIT>" ) ) <EOL> result . append ( self . decomp . flush ( ) ) <EOL> self . buffer = "<STR_LIT>" <EOL> self . finished = <NUM_LIT:1> <EOL> result = "<STR_LIT>" . join ( result ) <EOL> self . returnedBytes += len ( result ) <EOL> return result <EOL> else : <EOL> while len ( self . buffer ) < n : <EOL> data = self . chunkingZipFile . fp . read ( <EOL> min ( n , <NUM_LIT> , self . length - self . readBytes ) ) <EOL> self . readBytes += len ( data ) <EOL> if not data : <EOL> result = ( self . buffer <EOL> + self . decomp . decompress ( "<STR_LIT>" ) <EOL> + self . decomp . flush ( ) ) <EOL> self . finished = <NUM_LIT:1> <EOL> self . buffer = "<STR_LIT>" <EOL> self . returnedBytes += len ( result ) <EOL> return result <EOL> else : <EOL> self . buffer += self . decomp . decompress ( data ) <EOL> result = self . buffer [ : n ] <EOL> self . buffer = self . buffer [ n : ] <EOL> self . returnedBytes += len ( result ) <EOL> return result <EOL> def unzip ( filename , directory = "<STR_LIT:.>" , overwrite = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> for i in unzipIter ( filename , directory , overwrite ) : <EOL> pass <EOL> DIR_BIT = <NUM_LIT:16> <EOL> def unzipIter ( filename , directory = '<STR_LIT:.>' , overwrite = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> zf = zipfile . ZipFile ( filename , '<STR_LIT:r>' ) <EOL> names = zf . namelist ( ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> remaining = len ( zf . namelist ( ) ) <EOL> for entry in names : <EOL> remaining -= <NUM_LIT:1> <EOL> isdir = zf . getinfo ( entry ) . external_attr & DIR_BIT <EOL> f = os . path . join ( directory , entry ) <EOL> if isdir : <EOL> if not os . path . exists ( f ) : <EOL> os . makedirs ( f ) <EOL> else : <EOL> fdir = os . path . split ( f ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( fdir ) : <EOL> os . makedirs ( fdir ) <EOL> if overwrite or not os . path . exists ( f ) : <EOL> outfile = file ( f , '<STR_LIT:wb>' ) <EOL> outfile . write ( zf . read ( entry ) ) <EOL> outfile . close ( ) <EOL> yield remaining <EOL> def countZipFileChunks ( filename , chunksize ) : <EOL> """<STR_LIT>""" <EOL> totalchunks = <NUM_LIT:0> <EOL> zf = ChunkingZipFile ( filename ) <EOL> for info in zf . infolist ( ) : <EOL> totalchunks += countFileChunks ( info , chunksize ) <EOL> return totalchunks <EOL> def countFileChunks ( zipinfo , chunksize ) : <EOL> """<STR_LIT>""" <EOL> count , extra = divmod ( zipinfo . file_size , chunksize ) <EOL> if extra > <NUM_LIT:0> : <EOL> count += <NUM_LIT:1> <EOL> return count or <NUM_LIT:1> <EOL> def countZipFileEntries ( filename ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" , <EOL> DeprecationWarning , <NUM_LIT:2> ) <EOL> zf = zipfile . ZipFile ( filename ) <EOL> return len ( zf . namelist ( ) ) <EOL> def unzipIterChunky ( filename , directory = '<STR_LIT:.>' , overwrite = <NUM_LIT:0> , <EOL> chunksize = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> czf = ChunkingZipFile ( filename , '<STR_LIT:r>' ) <EOL> if not os . path . exists ( directory ) : <EOL> os . makedirs ( directory ) <EOL> remaining = countZipFileChunks ( filename , chunksize ) <EOL> names = czf . namelist ( ) <EOL> infos = czf . infolist ( ) <EOL> for entry , info in zip ( names , infos ) : <EOL> isdir = info . external_attr & DIR_BIT <EOL> f = os . path . join ( directory , entry ) <EOL> if isdir : <EOL> if not os . path . exists ( f ) : <EOL> os . makedirs ( f ) <EOL> remaining -= <NUM_LIT:1> <EOL> yield remaining <EOL> else : <EOL> fdir = os . path . split ( f ) [ <NUM_LIT:0> ] <EOL> if not os . path . exists ( fdir ) : <EOL> os . makedirs ( fdir ) <EOL> if overwrite or not os . path . exists ( f ) : <EOL> outfile = file ( f , '<STR_LIT:wb>' ) <EOL> fp = czf . readfile ( entry ) <EOL> if info . file_size == <NUM_LIT:0> : <EOL> remaining -= <NUM_LIT:1> <EOL> yield remaining <EOL> while fp . tell ( ) < info . file_size : <EOL> hunk = fp . read ( chunksize ) <EOL> outfile . write ( hunk ) <EOL> remaining -= <NUM_LIT:1> <EOL> yield remaining <EOL> outfile . close ( ) <EOL> else : <EOL> remaining -= countFileChunks ( info , chunksize ) <EOL> yield remaining </s>
