
    Ϫf%                         d Z ddlZddlZddlZddlZ G d dej                        Z G d d      Z G d de      Z	 G d	 d
e      Z
dZd Zd ZddZy)z
An incremental approach to unzipping files.  This allows you to unzip a little
bit of a file at a time, which means you can report progress as a file unzips.
    Nc                       e Zd ZdZd Zy)ChunkingZipFilez~
    A L{zipfile.ZipFile} object which, with L{readfile}, also gives you access
    to a file-like object for each entry.
    c                 x   | j                   dvrt        d      | j                  st        d      | j                  |      }| j                  j	                  |j
                  d       | j                  j                  t        j                        }|dd t        j                  k7  rt        j                  d      t        j                  t        j                  |      }| j                  j                  |t        j                           }|t        j                     r,| j                  j                  |t        j                            |j                   dz  r|j#                  d      }n|j#                  d	      }||j$                  k7  r&t        j                  d
|j$                  d|d      |j&                  t        j(                  k(  rt+        | |j,                        S |j&                  t        j.                  k(  rt1        | |j,                        S t        j                  d|j&                  |fz        )z3
        Return file-like object for name.
        )razread() requires mode "r" or "a"z3Attempt to read ZIP archive that was already closedr      z Bad magic number for file headeri   zutf-8cp437zFile name in directory "z" and header "z	" differ.z-Unsupported compression method %d for file %s)modeRuntimeErrorfpgetinfoseekheader_offsetreadzipfilesizeFileHeaderstringFileHeader
BadZipFilestructunpackstructFileHeader_FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTH	flag_bitsdecodeorig_filenamecompress_type
ZIP_STOREDZipFileEntrycompress_sizeZIP_DEFLATEDDeflatedZipFileEntry)selfnamezinfofheaderfname	fname_strs         :/usr/lib/python3/dist-packages/twisted/python/zipstream.pyreadfilezChunkingZipFile.readfile   s    99J&@AAwwTUUT"U((!,'',,w5561Q<7333$$%GHH-- 8 8'BWW%@%@AB7112GGLL!?!?@A??U"W-IW-I+++$$&&	3 
 '"4"44e&9&9::  G$8$88'e.A.ABB$$?&&-.     N)__name__
__module____qualname____doc__r*    r+   r)   r   r      s    
*r+   r   c                   P    e Zd ZdZd Zd Zd Zd Zd ZeZ	d Z
d Zd	 Zd
 Zd Zy)
_FileEntrya!  
    Abstract superclass of both compressed and uncompressed variants of
    file-like objects within a zip archive.

    @ivar chunkingZipFile: a chunking zip file.
    @type chunkingZipFile: L{ChunkingZipFile}

    @ivar length: The number of bytes within the zip file that represent this
    file.  (This is the size on disk, not the number of decompressed bytes
    which will result from reading it.)

    @ivar fp: the underlying file object (that contains pkzip data).  Do not
    touch this, please.  It will quite likely move or go away.

    @ivar closed: File-like 'closed' attribute; True before this file has been
    closed, False after.
    @type closed: L{bool}

    @ivar finished: An older, broken synonym for 'closed'.  Do not touch this,
    please.
    @type finished: L{int}
    c                 r    || _         | j                   j                  | _        || _        d| _        d| _        y)zC
        Create a L{_FileEntry} from a L{ChunkingZipFile}.
        r   FN)chunkingZipFiler   lengthfinishedclosedr#   r4   r5   s      r)   __init__z_FileEntry.__init__[   s4      /&&))r+   c                      y)zD
        Returns false because zip files should not be ttys
        Fr0   r#   s    r)   isattyz_FileEntry.isattye   s     r+   c                 $    d| _         d| _        | `y)z/
        Close self (file-like object)
        T   N)r7   r6   r   r;   s    r)   closez_FileEntry.closek   s     Gr+   c                 N     d}t         fdd      D ]  }||z  }|dk(  s |S  |S )z
        Read a line.
        r+   c                  &     j                  d      S )Nr>   )r   r;   s   r)   <lambda>z%_FileEntry.readline.<locals>.<lambda>x   s    1 r+      
)iter)r#   linebytes   `  r)   readlinez_FileEntry.readlines   s@     -s3 	DDLDu}		 r+   c                 >    | j                         }|r|S t               )zi
        Implement next as file does (like readline, except raises StopIteration
        at EOF)
        )rG   StopIteration)r#   nextlines     r)   __next__z_FileEntry.__next__~   s    
 ==?Oor+   c                     t        |       S )z1
        Returns a list of all the lines
        )listr;   s    r)   	readlinesz_FileEntry.readlines   s     Dzr+   c                     | S z/
        Returns an iterator (so self)
        r0   r;   s    r)   
xreadlinesz_FileEntry.xreadlines   	     r+   c                     | S rP   r0   r;   s    r)   __iter__z_FileEntry.__iter__   rR   r+   c                     | S Nr0   r;   s    r)   	__enter__z_FileEntry.__enter__   s    r+   c                 $    | j                          y rV   )r?   )r#   exc_type	exc_value	tracebacks       r)   __exit__z_FileEntry.__exit__   s    

r+   N)r,   r-   r.   r/   r9   r<   r?   rG   rK   nextrN   rQ   rT   rW   r\   r0   r+   r)   r2   r2   C   s@    .	 Dr+   r2   c                   $    e Zd ZdZd Zd ZddZy)r   zJ
    File-like object used to read an uncompressed entry in a ZipFile
    c                 @    t         j                  | ||       d| _        y )Nr   )r2   r9   	readBytesr8   s      r)   r9   zZipFileEntry.__init__   s    D/6:r+   c                     | j                   S rV   )r`   r;   s    r)   tellzZipFileEntry.tell   s    ~~r+   Nc                    || j                   | j                  z
  }|dk(  s| j                  ry| j                  j                  j                  t        || j                   | j                  z
              }| xj                  t        |      z  c_        | j                  | j                   k(  st        |      |k  rd| _        |S )Nr   r+   r>   )r5   r`   r6   r4   r   r   minlen)r#   ndatas      r)   r   zZipFileEntry.read   s    9dnn,A6T]]##&&++C4;;3O,PQ#d)#>>T[[(CIMDMr+   rV   r,   r-   r.   r/   r9   rb   r   r0   r+   r)   r   r      s    	r+   r   c                   $    e Zd ZdZd Zd ZddZy)r"   zE
    File-like object used to read a deflated entry in a ZipFile
    c                     t         j                  | ||       d| _        d| _        t	        j
                  d      | _        d| _        y )Nr   ir+   )r2   r9   returnedBytesr`   zlibdecompressobjdecompbufferr8   s      r)   r9   zDeflatedZipFileEntry.__init__   s<    D/6:((-r+   c                     | j                   S rV   )rk   r;   s    r)   rb   zDeflatedZipFileEntry.tell   s    !!!r+   Nc                     | j                   ry|| j                  g}|j                  | j                  j	                  | j
                  j                  j                  | j                  | j                  z
                     |j                  | j                  j	                  d             |j                  | j                  j                                d| _        d| _         dj                  |      }| xj                  t        |      z  c_        |S t        | j                        |k  r | j
                  j                  j                  t        |d| j                  | j                  z
              }| xj                  t        |      z  c_	        |sq| j                  | j                  j	                  d      z   | j                  j                         z   }d| _         d| _        | xj                  t        |      z  c_        |S | xj                  | j                  j	                  |      z  c_        t        | j                        |k  r | j                  d | }| j                  |d  | _        | xj                  t        |      z  c_        |S )Nr+      Zr>   i   )r6   ro   appendrn   
decompressr4   r   r   r5   r`   flushjoinrk   re   rd   )r#   rf   resultrg   s       r)   r   zDeflatedZipFileEntry.read   s   ==9F MM&&((++00t~~1MN
 MM$++0067MM$++++-.DKDMXXf%F#f+-Mdkk"Q&++..334t~~!=> #d)+dkk&<&<T&BBT[[EVEVEXX  %&DM"%DK&&#f+5&!MKK4;;#9#9$#??K dkk"Q& [[!_F++ab/DK#f+-Mr+   rV   rh   r0   r+   r)   r"   r"      s    "&r+   r"      c                 h    d}t        |       }|j                         D ]  }|t        ||      z  } |S )zr
    Predict the number of chunks that will be extracted from the entire
    zipfile, given chunksize blocks.
    r   )r   infolistcountFileChunks)filename	chunksizetotalchunkszfinfos        r)   countZipFileChunksr      s?    
 K		"B 8tY778r+   c                 T    t        | j                  |      \  }}|dkD  r|dz  }|xs dS )a9  
    Count the number of chunks that will result from the given C{ZipInfo}.

    @param zipinfo: a C{zipfile.ZipInfo} instance describing an entry in a zip
    archive to be counted.

    @return: the number of chunks present in the zip file.  (Even an empty file
    counts as one chunk.)
    @rtype: L{int}
    r   r>   )divmod	file_size)zipinfor}   countextras       r)   r{   r{     s4     '++Y7LE5qy
:Ar+   c              #   p  K   t        | d      }t        j                  j                  |      st        j                  |       t        | |      }|j                         }|j                         }t        ||      D ]  \  }}	|	j                  t        z  }
t        j                  j                  ||      }|
r>t        j                  j                  |      st        j                  |       |dz  }| zt        j                  j                  |      d   }t        j                  j                  |      st        j                  |       |st        j                  j                  |      s|j                  |      }|	j                  dk(  r	|dz  }| t        |d      5 }|j!                         |	j                  k  rI|j#                  |      }|j%                  |       |dz  }| |j!                         |	j                  k  rIddd       |t'        |	|      z  }|  y# 1 sw Y   xY ww)z
    Return a generator for the zipfile.  This implementation will yield after
    every chunksize uncompressed bytes, or at the end of a file, whichever
    comes first.

    The value it yields is the number of chunks left to unzip.
    r   r>   r   wbN)r   ospathexistsmakedirsr   namelistrz   zipexternal_attrDIR_BITrv   splitr*   r   openrb   r   writer{   )r|   	directory	overwriter}   czf	remainingnamesinfosentryr   isdirffdirr   outfilehunks                   r)   unzipIterChunkyr     s     (C
(C77>>)$
I"8Y7ILLNELLNE5%(  t""W,GGLLE*77>>!$ANIO 77==#A&D77>>$'D!q 1\\%(>>Q&NI#O!T] (g'')dnn4!wwy1d+!Q	'	 '')dnn4( ( _T9==	9 (( (s   FH6!A&H)!H6)H3	.H6).r   i   )r/   os.pathr   r   r   rl   ZipFiler   r2   r   r"   r   r   r{   r   r0   r+   r)   <module>r      se   

    0goo 0f^ ^B: 05: 5p 	"+ r+   