
[[\             $   @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z y d d l Z e j Z Wn! e k
 r d Z e j Z Yn Xy d d l Z Wn e k
 r d Z Yn Xy d d l Z Wn e k
 rd Z Yn Xd d d d d d d	 d
 d d d d g Z Gd d   d e  Z Gd d   d e  Z e Z Z d d >d Z d d >d Z d d >d Z d Z d Z d Z d Z d Z d Z d Z  d Z! d Z" d Z# d Z$ e
 j% e#  Z& d Z' d Z( d Z) d Z* d Z+ d  Z, d! Z- d" Z. d Z/ d# Z0 d$ Z1 d% Z2 e
 j% e1  Z3 d Z4 d Z5 d Z6 d Z7 d Z8 d  Z9 d! Z: d" Z; d Z< d# Z= d& Z> d' Z? d Z@ d( ZA d ZB d) ZC d ZD d* ZE d+ ZF d, ZG d- ZH e
 j% eG  ZI d ZJ d ZK d ZL d ZM d ZN d  ZO d! ZP d" ZQ d ZR d# ZS d& ZT d' ZU d. ZV d/ ZW e
 j% eV  ZX d0 ZY d1 ZZ e
 j% eY  Z[ d Z\ d Z] d Z^ d Z_ d Z` d  Za d! Zb d" Zc d Zd d# Ze d2 d3   Zf d4 d
   Zg d5 d6   Zh d7 d8   Zi Gd9 d   d ej  Zk Gd: d;   d;  Zl Gd< d=   d=  Zm Gd> d?   d?  Zn i d@ d 6dA d 6dB d 6dB d 6dB d 6dB d  6dC d! 6dD d" 6dE d 6dF d# 6dC d& 6dG d 6dH d 6dI d+ 6dJ dK 6dL dM 6dN dO 6Zo dP dQ   Zp dR dS   Zq dT dU   Zr GdV dW   dW e js  Zt GdX d   d  Zu GdY d   d eu  Zv d dZ d[  Zw ex d\ k rew   n  d S)]zP
Read and write ZIP files.

XXX references to utf-8 need further investigation.
    N
BadZipFile
BadZipfileerror
ZIP_STOREDZIP_DEFLATED	ZIP_BZIP2ZIP_LZMA
is_zipfileZipInfoZipFile	PyZipFileLargeZipFilec               @   s   e  Z d  Z d S)r   N)__name__
__module____qualname__ r   r   /usr/lib/python3.4/zipfile.pyr   '   s   c               @   s   e  Z d  Z d Z d S)r   zu
    Raised when writing a zipfile, the zipfile requires ZIP64 extensions
    and those extensions are disabled.
    N)r   r   r   __doc__r   r   r   r   r   +   s                        -   .   ?   s   <4s4H2LHs   PK                  	   z<4s4B4HL2L5H2Ls   PK
                  z<4s2B4HL2L2Hs   PKz<4sLQLs   PKz
<4sQ2H2L4Qs   PKc             C   s-   y t  |   r d SWn t k
 r( Yn Xd S)NTF)_EndRecDataOSError)fpr   r   r   _check_zipfile   s    r.   c             C   sh   d } yI t  |  d  r* t d |   } n$ t |  d   } t |  } Wd QXWn t k
 rc Yn X| S)zQuickly see if a file is a ZIP file by checking the magic number.

    The filename argument may be a file or file-like object too.
    Freadr-   rbN)hasattrr.   openr,   )filenameresultr-   r   r   r   r	      s    c             C   sr  y |  j  | t d  Wn t k
 r0 | SYn X|  j t  } t |  t k rV | St j t |  \ } } } } | t k r | S| d k s | d k r t	 d   n  |  j  | t t
 d  |  j t
  } t |  t
 k r | St j t |  \
 } } }	 }
 } } } } } } | t k r(| S| | t <| | t <| | t <| | t <| | t <| | t <| | t <| S)zM
    Read the ZIP64 end-of-archive records and use that to update endrec
    r   r   r   z3zipfiles that span multiple disks are not supported)seeksizeEndCentDir64Locatorr,   r/   lenstructunpackstructEndArchive64LocatorstringEndArchive64Locatorr   sizeEndCentDir64structEndArchive64stringEndArchive64_ECD_SIGNATURE_ECD_DISK_NUMBER_ECD_DISK_START_ECD_ENTRIES_THIS_DISK_ECD_ENTRIES_TOTAL	_ECD_SIZE_ECD_OFFSET)fpinoffsetendrecdataZsigZdisknoZreloffZdisksZszcreate_versionZread_versionZdisk_numZdisk_dirZdircountZ	dircount2ZdirsizeZ	diroffsetr   r   r   _EndRecData64   s6    	0






rK   c       	      C   s  |  j  d d  |  j   } y |  j  t d  Wn t k
 rI d SYn X|  j   } t |  t k r | d d  t k r | d	 d  d k r t j t	 |  } t
 |  } | j d  | j | t  t |  t |  St | d
 t d  } |  j  | d  |  j   } | j t  } | d k r| | | t  } t |  t k rYd St
 t j t	 |   } | t } | | t | t |  } | j |  | j | |  t |  | | | |  Sd S)zReturn data from the "End of Central Directory" record, or None.

    The data is a list of the nine items in the ZIP "End of central dir"
    record followed by a tenth item, the file seek offset of this record.r   r   Nr    s         r   r   i   )r5   tellsizeEndCentDirr,   r/   r7   stringEndArchiver8   r9   structEndArchivelistappendrK   maxrfind_ECD_COMMENT_SIZE)	rF   ZfilesizerI   rH   ZmaxCommentStartstartZrecDataZcommentSizecommentr   r   r   r+      s>    	
r+   c               @   sU   e  Z d  Z d Z d" Z d d# d d  Z d d d  Z d d   Z d  d!   Z d S)$r
   z>Class with attributes describing each file in the ZIP archive.orig_filenamer3   	date_timecompress_typerX   extracreate_systemrJ   extract_versionreserved	flag_bitsvolumeinternal_attrexternal_attrheader_offsetCRCcompress_size	file_size	_raw_timeZNoName  r   r   c             C   s&  | |  _  | j t d   } | d k r= | d |  } n  t j d k rs t j | k rs | j t j d  } n  | |  _ | |  _ | d d k  r t d   n  t	 |  _
 d |  _ d |  _ t j d k r d |  _ n	 d |  _ t |  _ t |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d  S)Nr   /i  z+ZIP does not support timestamps before 1980rL   Zwin32r   )rY   findchrossepreplacer3   rZ   
ValueErrorr   r[   rX   r\   sysplatformr]   DEFAULT_VERSIONrJ   r^   r_   r`   ra   rb   rc   )selfr3   rZ   Z	null_byter   r   r   __init__<  s.    													zZipInfo.__init__Nc             C   s  |  j  } | d d d >| d d >B| d B} | d d >| d	 d >B| d d B} |  j d
 @rs d } } } n |  j } |  j } |  j } |  j } d }	 | d k r | t k p | t k } n  | r d }
 | t j |
 d t j	 |
  d	 | |  } n  | t k s| t k rA| s,t
 d   n  d } d } t }	 n  |  j t k rbt t |	  }	 n! |  j t k rt t |	  }	 n  t |	 |  j  |  _ t |	 |  j  |  _ |  j   \ } } t j t t |  j |  j | |  j | | | | | t |  t |   } | | | S)z'Return the per-file header as a string.r   i  r$   r   r!   r   r   r&   r    r   Nz<HHQQz'Filesize would require ZIP64 extensionsl    )rZ   r`   re   rf   rg   r\   ZIP64_LIMITr8   packcalcsizer   ZIP64_VERSIONr[   r   rT   BZIP2_VERSIONr   LZMA_VERSIONr^   rJ   _encodeFilenameFlagsstructFileHeaderstringFileHeaderr_   r7   )rt   zip64dtdosdatedostimere   rf   rg   r\   min_versionfmtr3   r`   headerr   r   r   
FileHeaderf  sF    	&&				#	zZipInfo.FileHeaderc             C   sS   y |  j  j d  |  j f SWn/ t k
 rN |  j  j d  |  j d Bf SYn Xd  S)Nasciizutf-8i   )r3   encoder`   UnicodeEncodeError)rt   r   r   r   r|     s    zZipInfo._encodeFilenameFlagsc             C   s  |  j  } t j } xt |  d k r| d | d  d   \ } } | d k r| d k rz | d | d d   } nx | d k r | d | d d	   } nP | d
 k r | d | d d   } n( | d k r f  } n t d | f   d } |  j d k r!| | |  _ | d 7} n  |  j d k rJ| | |  _ | d 7} n  |  j d k r|  j } | | |  _ | d 7} qn  | | d d   } q Wd  S)Nr    z<HHr      z<QQQ   r   z<QQr   r   z<Qr   r   zCorrupt extra field %s        )r   r   )r\   r8   r9   r7   RuntimeErrorrg   rf   rd   )rt   r\   r9   ZtpZlnZcountsidxoldr   r   r   _decodeExtra  s4    				zZipInfo._decodeExtra)zorig_filenamezfilenamez	date_timezcompress_typezcommentzextrazcreate_systemzcreate_versionzextract_versionzreservedz	flag_bitszvolumezinternal_attrzexternal_attrzheader_offsetzCRCzcompress_sizez	file_sizez	_raw_time)ri   r   r   r   r   r   )	r   r   r   r   	__slots__ru   r   r|   r   r   r   r   r   r
   #  s0                     *.c               @   sX   e  Z d  Z d Z d d   Z d Z d d   Z d d   Z d	 d
   Z d d   Z	 d S)_ZipDecryptera  Class to handle decryption of files stored within a ZIP archive.

    ZIP supports a password-based form of encryption. Even though known
    plaintext attacks have been found against it, it is still useful
    to be able to get data out of such a file.

    Usage:
        zd = _ZipDecrypter(mypwd)
        plain_char = zd(cypher_char)
        plain_text = map(zd, cypher_text)
    c              C   s   d }  d g d } xh t  d  D]Z } | } xA t  d  D]3 } | d @r^ | d ?d @|  A} q9 | d ?d @} q9 W| | | <q  W| S)zGenerate a CRC-32 table.

        ZIP encryption uses the CRC32 one-byte primitive for scrambling some
        internal keys. We noticed that a direct implementation is faster than
        relying on binascii.crc32().
        l    q[ r      r   r   i)range)Zpolytableicrcjr   r   r   _GenerateCRCTable  s    
z_ZipDecrypter._GenerateCRCTableNc             C   s   | d ?d @|  j  | | Ad @AS)z(Compute the CRC32 primitive on one byte.r   i    )crctable)rt   Zchr   r   r   r   _crc32  s    z_ZipDecrypter._crc32c             C   s^   t  j d  k r! t  j   t  _ n  d |  _ d |  _ d |  _ x | D] } |  j |  qC Wd  S)NixV4igE#ixV4)r   r   r   key0key1key2_UpdateKeys)rt   pwdpr   r   r   ru     s    			z_ZipDecrypter.__init__c             C   sr   |  j  | |  j  |  _ |  j |  j d @d @|  _ |  j d d d @|  _ |  j  |  j d ?d @|  j  |  _ d  S)Nr   l    ir   r   )r   r   r   r   )rt   cr   r   r   r     s    z_ZipDecrypter._UpdateKeysc             C   sM   t  | t  s t  |  j d B} | | | d Ad ?d @A} |  j |  | S)zDecrypt a single character.r   r   r   r   )
isinstanceintAssertionErrorr   r   )rt   r   kr   r   r   __call__  s
    z_ZipDecrypter.__call__)
r   r   r   r   r   r   r   ru   r   r   r   r   r   r   r     s   	r   c               @   s@   e  Z d  Z d d   Z d d   Z d d   Z d d   Z d	 S)
LZMACompressorc             C   s   d  |  _  d  S)N)_comp)rt   r   r   r   ru     s    zLZMACompressor.__init__c             C   sf   t  j i t  j d 6 } t  j t  j d t  j t  j |  g |  _ t j d d d t	 |   | S)Nidfiltersz<BBHr$   r    )
lzmaZ_encode_filter_propertiesFILTER_LZMA1r   
FORMAT_RAW_decode_filter_propertiesr   r8   rw   r7   )rt   Zpropsr   r   r   _init  s    zLZMACompressor._initc             C   s9   |  j  d  k r) |  j   |  j  j |  S|  j  j |  S)N)r   r   compress)rt   rI   r   r   r   r     s    zLZMACompressor.compressc             C   s3   |  j  d  k r& |  j   |  j  j   S|  j  j   S)N)r   r   flush)rt   r   r   r   r     s    zLZMACompressor.flushN)r   r   r   ru   r   r   r   r   r   r   r   r     s   r   c               @   s(   e  Z d  Z d d   Z d d   Z d S)LZMADecompressorc             C   s   d  |  _  d |  _ d |  _ d  S)NrL   F)_decomp_unconsumedeof)rt   r   r   r   ru     s    		zLZMADecompressor.__init__c          	   C   s   |  j  d  k r |  j | 7_ t |  j  d k r7 d St j d |  j d d   \ } t |  j  d | k rv d St j t j d t j t j	 |  j d d |   g |  _  |  j d | d   } |  ` n  |  j  j
 |  } |  j  j |  _ | S)Nr    rL   z<Hr   r   )r   r   r7   r8   r9   r   r   r   r   r   
decompressr   )rt   rI   Zpsizer4   r   r   r   r     s    "#	zLZMADecompressor.decompressN)r   r   r   ru   r   r   r   r   r   r     s   r   ZstoreZshrinkreduceZimplodetokenizeZdeflateZ	deflate64Zbzip2r   ZterseZlz77   Zwavpacka   Zppmdb   c             C   s   |  t  k r nx |  t k r3 t s t d   q nT |  t k rW t s t d   q n0 |  t k r{ t s t d   q n t d   d  S)Nz.Compression requires the (missing) zlib modulez-Compression requires the (missing) bz2 modulez.Compression requires the (missing) lzma modulez(That compression method is not supported)r   r   zlibr   r   bz2r   r   )compressionr   r   r   _check_compressionC  s    r   c             C   sV   |  t  k r% t j t j t j d  S|  t k r; t j   S|  t k rN t	   Sd  Sd  S)Nr(   i)
r   r   ZcompressobjZZ_DEFAULT_COMPRESSIONZDEFLATEDr   r   ZBZ2Compressorr   r   )r[   r   r   r   _get_compressorV  s    
r   c             C   s   |  t  k r d  S|  t k r) t j d  S|  t k r? t j   S|  t k rR t   St	 j
 |   } | r t d |  | f   n t d |  f   d  S)Nr(   zcompression type %d (%s)zcompression type %di)r   r   r   Zdecompressobjr   r   ZBZ2Decompressorr   r   compressor_namesgetNotImplementedError)r[   Zdescrr   r   r   _get_decompressorb  s    
r   c                   s   e  Z d  Z d Z d Z d Z e j d  Z d d d d	  Z	 d d
 d  Z
 d d d  Z d d   Z d d d  Z d d   Z d d   Z d d   Z d d   Z   f d d   Z   S) 
ZipExtFilezZFile-like object for reading an archive member.
       Is returned by ZipFile.open().
    r   r   i   s*   ^(?P<chunk>[^\r\n]+)|(?P<newline>\n|\r\n?)NFc             C   s   | |  _  | |  _ | |  _ | j |  _ | j |  _ | j |  _ t	 |  j  |  _
 d |  _ d |  _ d |  _ d | k |  _ d  |  _ |  j d  k	 r |  j d 8_ n  | |  _ | j |  _ t | d  r | j |  _ t d  d @|  _ n	 d  |  _ d  S)NFrL   r   Ur   re   l    )_fileobj
_decrypter_close_fileobjr[   _compress_typerf   _compress_leftrg   _leftr   _decompressor_eof_readbuffer_offset
_universalnewlinesmoder3   namer1   re   _expected_crccrc32_running_crc)rt   Zfileobjr   zipinfoZ	decrypterclose_fileobjr   r   r   ru     s(    								zZipExtFile.__init__c             C   s  |  j  rd | d k  rd |  j j d |  j  d } | d k rd |  j |  j |  } | |  _ | Sn  |  j  s t j j |  |  Sd } x| d k  s t |  | k  r|  j d  } | d k r | S|  j	 j
 |  } | j d  } | d k	 rM|  j d k rg  |  _ n  | |  j k r0|  j j |  n  |  j t |  7_ | d S| j d  } | d k r| d | t |   } n  |  j t |  7_ | | 7} q W| S)	zrRead and return a line from the stream.

        If limit is specified, at most limit bytes will be read.
        r   s   
r   rL   r   newlineNchunk)r   r   rk   r   ioBufferedIOBasereadliner7   peekPATTERNsearchgroupr   rS   )rt   limitr   lineZ	readaheadmatchr   r   r   r   r   r     s8    		!
zZipExtFile.readlinec             C   s   | t  |  j  |  j k r |  j |  } t  |  |  j k ri | |  j |  j d  |  _ d |  _ q |  j t  |  8_ n  |  j |  j |  j d  S)z6Returns buffered bytes without advancing the position.Nr   i   )r7   r   r   r/   )rt   nr   r   r   r   r     s    zZipExtFile.peekc             C   s   d S)NTr   )rt   r   r   r   readable  s    zZipExtFile.readablec             C   sp  | d k s | d k  rj |  j  |  j d  } d |  _  d |  _ x# |  j se | |  j |  j  7} qC W| S| |  j } | t |  j   k  r |  j  |  j |  } | |  _ | S| t |  j   } |  j  |  j d  } d |  _  d |  _ x | d k rk|  j rk|  j |  } | t |  k  rN| |  _  | |  _ | | d |  7} Pn  | | 7} | t |  8} q W| S)zRead and return up to n bytes.
        If the argument is omitted, None, or negative, data is read and returned until EOF is reached..
        Nr   rL   )r   r   r   _read1MAX_Nr7   )rt   r   bufendrI   r   r   r   r/     s4    							
zZipExtFile.readc             C   sa   |  j  d  k r d  St | |  j  d @|  _ |  j r] |  j |  j  k r] t d |  j   n  d  S)Nl    zBad CRC-32 for file %r)r   r   r   r   r   r   )rt   newdatar   r   r   _update_crc  s
    zZipExtFile._update_crcc             C   s|  | d k s | d k  rz |  j  |  j d  } d |  _  d |  _ x3 |  j su |  j |  j  } | rC | | 7} PqC qC W| S| |  j } | t |  j   k  r |  j  |  j |  } | |  _ | S| t |  j   } |  j  |  j d  } d |  _  d |  _ | d k rxxo |  j st|  j |  } | t |  k  r]| |  _  | |  _ | | d |  7} Pn  | r	| | 7} Pq	q	Wn  | S)z7Read up to n bytes with at most one read() system call.Nr   rL   )r   r   r   r   r   r7   )rt   r   r   rI   r   r   r   r   read1  s>    		
					
zZipExtFile.read1c             C   s  |  j  s | d k r d S|  j t k ri |  j j } | t |  k rx | |  j | t |   7} qx n |  j |  } |  j t k r |  j d k |  _  n |  j t k rt	 | |  j
  } |  j j | |  } |  j j p |  j d k o |  j j |  _  |  j  rO| |  j j   7} qOn0 |  j j |  } |  j j pI|  j d k |  _  | d  |  j  } |  j t |  8_ |  j d k rd |  _  n  |  j |  | S)Nr   rL   T)r   r   r   r   Zunconsumed_tailr7   _read2r   r   rT   MIN_READ_SIZEr   r   r   r   r   )rt   r   rI   r   r   r   r   4  s2    #	zZipExtFile._read1c             C   s   |  j  d k r d St | |  j  } t | |  j   } |  j j |  } |  j  t |  8_  | sm t  n  |  j d  k	 r t	 t
 |  j |   } n  | S)Nr   rL   )r   rT   r   minr   r/   r7   EOFErrorr   bytesmap)rt   r   rI   r   r   r   r   X  s    	zZipExtFile._read2c          
      s2   z |  j  r |  j j   n  Wd  t   j   Xd  S)N)r   r   closesuper)rt   )	__class__r   r   r   h  s    	zZipExtFile.close   i   @r   )r   r   r   r   r   r   recompiler   ru   r   r   r   r/   r   r   r   r   r   r   r   )r   r   r   s  s    2!
%$r   c               @   sx  e  Z d  Z d Z d Z d Z d e d d d  Z d d   Z d	 d
   Z	 d d   Z
 d d   Z d d   Z d d d  Z d d   Z d d   Z d d   Z e d d    Z e j d d    Z d d d  Z d d d d  Z d d d  d!  Z d d d d" d#  Z e d$ d%    Z d& d'   Z d( d)   Z d d d* d+  Z d d, d-  Z d. d/   Z d0 d1   Z d S)2r   a   Class with methods to open, read, write, close, list zip files.

    z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True)

    file: Either the path to the file, or a file-like object.
          If it is a path, the file will be opened and closed by ZipFile.
    mode: The mode can be either read "r", write "w" or append "a".
    compression: ZIP_STORED (no compression), ZIP_DEFLATED (requires zlib),
                 ZIP_BZIP2 (requires bz2) or ZIP_LZMA (requires lzma).
    allowZip64: if True ZipFile will create files with ZIP64 extensions when
                needed, otherwise it will raise an exception when this would
                be necessary.

    NrTc             C   s6  | d k r t  d   n  t |  | |  _ d |  _ d |  _ i  |  _ g  |  _ | |  _ | j d d  d |  _	 } d	 |  _
 d
 |  _ t | t  r0d |  _ | |  _ i d d 6d d 6d d 6} y t j | | |  |  _ WqWt k
 r,| d k r%d } } t j | | |  |  _ n   YqWXn' d |  _ | |  _ t | d d	  |  _ y | d k rs|  j   n | d k rd |  _ np | d k ry$ |  j   |  j j |  j d  Wqt k
 r|  j j d d  d |  _ YqXn t  d   Wn3 |  j } d	 |  _ |  j s*| j   n    Yn Xd	 S)z>Open the ZIP file with mode read "r", write "w" or append "a".r   waz(ZipFile() requires mode "r", "w", or "a"Fr   b NrL   r0   wbzr+br   r   Tr   zMode must be "r", "w" or "a")r   r   r  )r   r   _allowZip64
_didModifydebug
NameToInfofilelistr   ro   r   r   _commentr   str_filePassedr3   r   r2   r-   r,   getattr_RealGetContentsr5   	start_dirr   r   )rt   filer   r   
allowZip64keyZmodeDictr-   r   r   r   ru     sZ    
										
		
			zZipFile.__init__c             C   s   |  S)Nr   )rt   r   r   r   	__enter__  s    zZipFile.__enter__c             C   s   |  j    d  S)N)r   )rt   typevalue	tracebackr   r   r   __exit__  s    zZipFile.__exit__c             C   s  |  j  } y t |  } Wn t k
 r9 t d   Yn X| sO t d   n  |  j d k rk t |  n  | t } | t } | t |  _	 | t
 | | } | t t k r | t t 8} n  |  j d k r | | } t d | | |  n  | | |  _ | j |  j d  | j |  } t j |  } d } xx| | k  r| j t  }	 t |	  t k rqt d   n  t j t |	  }	 |	 t t k rt d   n  |  j d k rt |	  n  | j |	 t  }
 |	 d } | d	 @r|
 j d
  }
 n |
 j d  }
 t |
  } | j |	 t  | _ | j |	 t   | _! |	 t" | _# |	 d d  \ | _$ | _% | _& | _' | _( | _) } } | _* | _+ | _, | j& t- k rt. d | j& d   n  |	 d d  \ | _/ | _0 | _1 | | _2 | d ?d | d ?d @| d @| d ?| d ?d @| d @d f | _3 | j4   | j# | | _# |  j5 j6 |  | |  j7 | j8 <| t |	 t |	 t |	 t  } |  j d k r5t d |  q5q5Wd S)z/Read in the table of contents for the ZIP file.zFile is not a zip filer   r   zgiven, inferred, offsetr   zTruncated central directoryz&Bad magic number for central directoryr!   i   zutf-8cp437r   zzip file version %.1fr%   r(   r*   r$   i  r   r&   r   totalN)9r-   r+   r,   r   r  printrD   rE   _ECD_COMMENTr
  _ECD_LOCATIONr?   r>   r<   r6   r  r5   r/   r   BytesIOsizeCentralDirr7   r8   r9   structCentralDir_CD_SIGNATUREstringCentralDir_CD_FILENAME_LENGTHdecoder
   _CD_EXTRA_FIELD_LENGTHr\   _CD_COMMENT_LENGTHrX   _CD_LOCAL_HEADER_OFFSETrd   rJ   r]   r^   r_   r`   r[   re   rf   rg   MAX_EXTRACT_VERSIONr   ra   rb   rc   rh   rZ   r   r	  rS   r  r3   )rt   r-   rH   Zsize_cdZ	offset_cdconcatZinferredrI   r  centdirr3   flagsxtdr   r   r   r    sp    	




L"	&
"zZipFile._RealGetContentsc             C   s   d d   |  j  D S)z+Return a list of file names in the archive.c             S   s   g  |  ] } | j   q Sr   )r3   ).0rI   r   r   r   
<listcomp>  s   	 z$ZipFile.namelist.<locals>.<listcomp>)r	  )rt   r   r   r   namelist  s    zZipFile.namelistc             C   s   |  j  S)zJReturn a list of class ZipInfo instances for files in the
        archive.)r	  )rt   r   r   r   infolist  s    zZipFile.infolistc             C   sf   t  d d
 d | xK |  j D]@ } d | j d d  } t  d	 | j | | j f d | q Wd S)z+Print a table of contents for the zip file.z%-46s %19s %12s	File NameModified    Sizer  z%d-%02d-%02d %02d:%02d:%02dNr"   z%-46s %s %12d)r2  r3  r4  )r  r	  rZ   r3   rg   )rt   r  zinfoZdater   r   r   printdir!  s    zZipFile.printdirc             C   sr   d } xe |  j  D]Z } y8 |  j | j d   } x | j |  rF q4 WWd QXWq t k
 ri | j SYq Xq Wd S)z%Read all the files and check the CRC.r   r   r   Ni   )r	  r2   r3   r/   r   )rt   Z
chunk_sizer5  fr   r   r   testzip*  s    zZipFile.testzipc             C   s5   |  j  j |  } | d k r1 t d |   n  | S)z,Return the instance of ZipInfo given 'name'.Nz(There is no item named %r in the archive)r  r   KeyError)rt   r   infor   r   r   getinfo7  s
    zZipFile.getinfoc             C   sN   | r/ t  | t  r/ t d t |    n  | rA | |  _ n	 d |  _ d S)z)Set default password for encrypted files.zpwd: expected bytes, got %sN)r   r   	TypeErrorr  r   )rt   r   r   r   r   setpassword@  s
    zZipFile.setpasswordc             C   s   |  j  S)z.The comment text associated with the ZIP file.)r
  )rt   r   r   r   rX   I  s    zZipFile.commentc             C   s   t  | t  s( t d t |    n  t |  t k rp d d  l } | j d t d d | d  t  } n  | |  _ d |  _	 d  S)Nzcomment: expected bytes, got %sr   z3Archive comment is too long; truncating to %d bytes
stacklevelr   T)
r   r   r<  r  r7   ZIP_MAX_COMMENTwarningswarnr
  r  )rt   rX   r@  r   r   r   rX   N  s    		c             C   s,   |  j  | d |   } | j   SWd QXd S)z)Return file bytes (as a string) for name.r   N)r2   r/   )rt   r   r   r-   r   r   r   r/   [  s    zZipFile.readc             C   s5  | d k r t  d   n  d | k rI d d l } | j d t d  n  | rx t | t  rx t d	 t |    n  |  j s t  d
   n  |  j	 r |  j } n t
 j |  j d  } ySt | t  r | } n |  j |  } | j | j d  | j t  } t |  t k r't d   n  t j t |  } | t t k rXt d   n  | j | t  } | t r| j | t  n  | j d @rt d   n  | j d @rt d   n  | j d @r| j d  }	 n | j d  }	 |	 | j k rt d | j | f   n  | j d @}
 d } |
 r| sE|  j  } n  | s^t  d |   n  t! |  } | j d  } t" t# | | d d    } | j d @r| j$ d ?d @} n | j% d ?d @} | d | k rt  d |   qn  t& | | | | d |  j	 SWn! |  j	 s)| j'   n    Yn Xd S) z#Return file-like object for 'name'.r   r   rUz&open() requires mode "r", "U", or "rU"r   Nz'U' mode is deprecatedr   zpwd: expected bytes, got %sz3Attempt to read ZIP archive that was already closedr0   zTruncated file headerz Bad magic number for file header    z$compressed patched data (flag bit 5)@   zstrong encryption (flag bit 6)i   zutf-8r  z/File name in directory %r and header %r differ.r   z6File %s is encrypted, password required for extractionr   r   r   r   r&   zBad password for filer   )r   r   rB  )(r   r@  rA  DeprecationWarningr   r   r<  r  r-   r  r   r2   r3   r
   r;  r5   rd   r/   sizeFileHeaderr7   r   r8   r9   r}   _FH_SIGNATUREr~   _FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTHr`   r   r#  rY   r   r   rR   r   rh   re   r   r   )rt   r   r   r   r@  Zzef_filer5  ZfheaderfnameZ	fname_strZis_encryptedZzdr   hZ
check_byter   r   r   r2   `  sz    				
	zZipFile.openc             C   sO   t  | t  s! |  j |  } n  | d k r< t j   } n  |  j | | |  S)a#  Extract a member from the archive to the current working directory,
           using its full name. Its file information is extracted as accurately
           as possible. `member' may be a filename or a ZipInfo object. You can
           specify a different directory using `path'.
        N)r   r
   r;  rm   getcwd_extract_member)rt   memberpathr   r   r   r   extract  s
    zZipFile.extractc             C   sC   | d k r |  j    } n  x! | D] } |  j | | |  q" Wd S)zExtract all members from the archive to the current working
           directory. `path' specifies a different directory to extract to.
           `members' is optional and must be a subset of the list returned
           by namelist().
        N)r0  rP  )rt   rO  Zmembersr   r   r   r   r   
extractall  s    zZipFile.extractallc             C   s   |  j  } | s= d } t j | d t |   } | |  _  n  | j |  } d d   | j |  D } | j d d   | D  } | S)z;Replace bad characters and remove trailing dots from parts.z:<>|"?*_c             s   s   |  ] } | j  d   Vq d S).N)rstrip)r.  r+  r   r   r   	<genexpr>  s    z1ZipFile._sanitize_windows_name.<locals>.<genexpr>c             s   s   |  ] } | r | Vq d  S)Nr   )r.  r+  r   r   r   rU    s    )!_windows_illegal_name_trans_tabler  	maketransr7   	translatesplitjoin)clsarcnamepathsepr   Zillegalr   r   r   _sanitize_windows_name  s    	zZipFile._sanitize_windows_namec                s  | j  j d t j j  } t j j rH | j t j j t j j  } n  t j j |  d } d t j j t j j f   t j j j	   f d d   | j
 t j j  D  } t j j d k r |  j | t j j  } n  t j j	 | |  } t j j |  } t j j |  } | r?t j j |  r?t j |  n  | j  d
 d k rxt j j |  stt j |  n  | S|  j | d | / } t | d   } t j | |  Wd	 QXWd	 QX| S)zbExtract the ZipInfo object 'member' to a physical
           file on the path targetpath.
        rj   r   r  c             3   s!   |  ] } |   k r | Vq d  S)Nr   )r.  r+  )invalid_path_partsr   r   rU    s    z*ZipFile._extract_member.<locals>.<genexpr>\r   r  Nr   )r3   ro   rm   rO  rn   altsep
splitdrivecurdirpardirrZ  rY  r^  normpathdirnameexistsmakedirsisdirmkdirr2   shutilZcopyfileobj)rt   rN  Z
targetpathr   r\  Z	upperdirssourcetargetr   )r_  r   rM    s*    !7zZipFile._extract_memberc             C   s   | j  |  j k r; d d l } | j d | j  d d n  |  j d k rY t d   n  |  j sq t d	   n  t | j  |  j	 s d } t
 |  j  t k r d
 } n0 | j t k r d } n | j t k r d } n  | r t | d   q n  d S)z6Check for errors before writing a file to the archive.r   NzDuplicate name: %rr>  r   r   r  z write() requires mode "w" or "a"z4Attempt to write ZIP archive that was already closedzFiles countZFilesizezZipfile sizez would require ZIP64 extensions)r   r  )r3   r  r@  rA  r   r   r-   r   r[   r  r7   r	  ZIP_FILECOUNT_LIMITrg   rv   rd   r   )rt   r5  r@  requires_zip64r   r   r   _writecheck  s(    					zZipFile._writecheckc          
   C   s  |  j  s t d   n  t j |  } t j | j  } t j | j  } | d d  } | d k rp | } n  t j	 j
 t j	 j |  d  } x0 | d t j t j f k r | d d  } q W| r | d 7} n  t | |  } | d d @d >| _ | d k r|  j | _ n	 | | _ | j | _ d | _ |  j  j   | _ | j t k rh| j d	 O_ n  |  j |  d
 |  _ | rd | _ d | _ d | _ | j d O_ |  j j |  | |  j | j <|  j  j  | j! d   d St" | j  }	 t# | d   }
 d | _ } d | _ } |  j$ o?| j d t% k } |  j  j  | j! |   d } xw |
 j& d  } | s}Pn  | t' |  } t( | |  d @} |	 r|	 j) |  } | t' |  } n  |  j  j  |  qdWd QX|	 r|	 j*   } | t' |  } |  j  j  |  | | _ n	 | | _ | | _ | | _ | r|  j$ r| t% k ret d   n  | t% k rt d   qn  |  j  j   } |  j  j+ | j d  |  j  j  | j! |   |  j  j+ | d  |  j j |  | |  j | j <d S)zLPut the bytes from filename into the archive under the name
        arcname.z7Attempt to write to ZIP archive that was already closedr   r"   Nr   rj   i  r   r   TFr0   g?i   r   l    z*File size has increased during compressingz-Compressed size larger than uncompressed sizei    ),r-   r   rm   statS_ISDIRst_modetime	localtimest_mtimerO  re  rb  rn   ra  r
   rc   r   r[   st_sizerg   r`   rN   rd   r   rp  r  rf   re   r	  rS   r  r3   writer   r   r2   r  rv   r/   r7   r   r   r   r5   )rt   r3   r\  r[   stri  mtimerZ   r5  Zcmprr-   re   rf   r   rg   r   Zpositionr   r   r   rx  '  s    		"										zZipFile.writec             C   su  t  | t  r! | j d  } n  t  | t  s t d | d t j t j    d d   } |  j | _ | j d d k r d | _	 | j	 d	 O_	 q d | _	 n | } |  j
 s t d   n  t |  | _ |  j
 j   | _ | d k	 r | | _ n  | j t k r| j d O_ n  |  j |  d |  _ t |  d @| _ t | j  } | r| j |  | j   } t |  | _ n | j | _ | j t k p| j t k } | r|  j rt d   n  |  j
 j | j |   |  j
 j |  | j d @rD| rd n d } |  j
 j t j  | | j | j | j   n  |  j
 j   |  j! j" |  | |  j# | j <d S)a  Write a file into the archive.  The contents is 'data', which
        may be either a 'str' or a 'bytes' instance; if it is a 'str',
        it is encoded as UTF-8 first.
        'zinfo_or_arcname' is either a ZipInfo instance or
        the name of the file in the archive.zutf-8r3   rZ   Nr"   r   rj   iA  r   i  z7Attempt to write to ZIP archive that was already closedr   Tl    z'Filesize would require ZIP64 extensionsr   z<LQQz<LLLr   i  Ai  )$r   r  r   r
   rt  ru  r   r[   r3   rc   r-   r   r7   rg   rN   rd   r   r`   rp  r  r   re   r   r   r   rf   rv   r  r   rx  r   r8   rw   r	  rS   r  )rt   Zzinfo_or_arcnamerI   r[   r5  cor   r   r   r   r   writestr  sR    "			zZipFile.writestrc             C   s   |  j    d S)z2Call the "close()" method in case the user forgot.N)r   )rt   r   r   r   __del__  s    zZipFile.__del__c          "   C   s  |  j  d k r d Sz|  j d k r|  j r|  j  j   } x|  j D]} | j } | d d d >| d d >B| d	 B} | d
 d >| d d >B| d d	 B} g  } | j t k s | j t k r | j	 | j  | j	 | j  d } d } n | j } | j } | j
 t k r/| j	 | j
  d }	 n	 | j
 }	 | j }
 d } | rt j d d t |  d d t |  |  |
 }
 t } n  | j t k rt t |  } n! | j t k rt t |  } n  t | | j  } t | | j  } y | j   \ } } t j t t | | j | | j | | j | | | j | | t |  t |
  t | j  d | j | j  |	  } Wn t! k
 rt" t t | | j | | j | j# | j | | | j | | t | j$  t |
  t | j  d | j | j  |	 f d t% j&   Yn X|  j  j' |  |  j  j' |  |  j  j' |
  |  j  j' | j  qG W|  j  j   } t |  j  } | | } | } d } | t( k rd } n* | t k rd } n | t k rd } n  | r}|  j) st* | d   n  t j t+ t, d d d d d | | | |  } |  j  j' |  t j t- t. d | d  } |  j  j' |  t/ | d  } t/ | d  } t/ | d  } n  t j t0 t1 d d | | | | t |  j2  	 } |  j  j' |  |  j  j' |  j2  |  j  j3   n  Wd |  j  } d |  _  |  j4 s| j5   n  Xd S)zJClose the file, and for mode "w" and "a" write the ending
        records.Nr   r  r   i  r$   r   r!   r   r   r&   r    l    z<HHQr   r  zFiles countzCentral directory offsetzCentral directory sizez would require ZIP64 extensions,   r   i  )r   r  )6r-   r   r  rN   r	  rZ   rg   rv   rf   rS   rd   r\   r8   rw   r7   ry   r[   r   rT   rz   r   r{   r^   rJ   r|   r  r!  r]   r_   re   rX   rb   rc   rE  r  r`   r3   rq   stderrrx  rn  r  r   r=   r>   r:   r;   r   rQ   rP   r
  r   r  r   )rt   Zpos1r5  r   r   r   r\   rg   rf   rd   Z
extra_datar   r^   rJ   r3   r`   r)  Zpos2ZcentDirCountZcentDirSizeZcentDirOffsetro  Zzip64endrecZzip64locrecrH   r-   r   r   r   r     s    	&&								!
							zZipFile.close) r   r   r   r   r-   rV  r   ru   r  r  r  r0  r1  r6  r8  r;  r=  propertyrX   setterr/   r2   rP  rQ  classmethodr^  rM  rp  rx  r|  r}  r   r   r   r   r   r   p  s4   AN			`'Y8c               @   sL   e  Z d  Z d Z d e d d d d  Z d d d	 d
  Z d d   Z d S)r   zDClass to create ZIP archives with Python library files and packages.r   Tr   c          	   C   s/   t  j |  | d | d | d | | |  _ d  S)Nr   r   r  )r   ru   	_optimize)rt   r  r   r   r  optimizer   r   r   ru   2  s    zPyZipFile.__init__r  Nc             C   s  | rU | |  rU |  j  rQ t j j |  r4 d n d } t d | | f  n  d St j j |  \ } } t j j |  rXt j j | d  } t j j |  rh| r d | | f } n | } |  j  r t d | d |  n  |  j | d	 d  |  \ } }	 |  j  rt d |	  n  |  j	 | |	  t j
 |  }
 |
 j d  x|
 D]} t j j | |  } t j j |  \ } } t j j |  rt j j t j j | d   ra|  j | | d | qaqR| d k rR| r| |  r|  j  rRt d |  qRqRn  |  j | d	 d  |  \ } }	 |  j  rNt d |	  n  |  j	 | |	  qRqRWq|  j  rt d |  n  xDt j
 |  D] } t j j | |  } t j j |  \ } } | d k r| r| |  r|  j  rt d |  qqn  |  j | d	 d  |  \ } }	 |  j  r>t d |	  n  |  j	 | |	  qqWnp | d d  d k r}t d   n  |  j | d	 d  |  \ } }	 |  j  rt d |	  n  |  j	 | |	  d S)a  Add all files from "pathname" to the ZIP archive.

        If pathname is a package directory, search the directory and
        all package subdirectories recursively for all *.py and enter
        the modules into the archive.  If pathname is a plain
        directory, listdir *.py and enter all modules.  Else, pathname
        must be a Python *.py file and the module will be put into the
        archive.  Added modules are always module.pyo or module.pyc.
        This method will compile the module.py into module.pyc if
        necessary.
        If filterfunc(pathname) is given, it is called with every argument.
        When it is False, the file or directory is skipped.
        rO  r  z%s "%s" skipped by filterfuncNz__init__.pyz%s/%szAdding package inasr   r   ZAdding
filterfuncz.pyzfile "%s" skipped by filterfunczAdding files from directoryz.Files added with writepy() must end with ".py"zAdding filer  r  r  r  )r  rm   rO  ri  r  rY  rZ  isfile_get_codenamerx  listdirremovesplitextwritepyr   )rt   pathnamebasenamer  Zlabeldirr   ZinitnamerJ  r\  Zdirlistr3   rO  rootZextr   r   r   r  8  sv    		"	!					"	zPyZipFile.writepyc                sz  d   f d d  } | d } | d } | d } t  j j | d  } t  j j | d  }   j d k rt j j |  r t j |  j t j |  j k r | }	 }
 qAt j j |  r t j |  j t j |  j k r | }	 }
 qAt j j |  r7t j |  j t j |  j k r7| }
 | }	 qAt j j |  r|t j |  j t j |  j k r|| }
 | }	 qA| |  rt	 r| n | }
 t	 r| n | }	 qA| }
 }	 n   j d	 k r| }
 | }	 n | }
 | }	 t j j |
  ot j |
  j t j |  j k sA| | d
   j sA| }
 }	 qAn  t j j
 |	  d } | rpd | | f } n  |
 | f S)a   Return (filename, archivename) for the path.

        Given a module name path, return the correct file path and
        archive name, compiling if necessary.  For example, given
        /python/lib/string, return (/python/lib/string.pyc, string).
        r   c                s   d d  l  }   j r% t d |   n  y | j |  d d d | Wn9 | j k
 r} } z t | j  d SWYd  d  } ~ Xn Xd S)Nr   Z	CompilingdoraiseTr  F)
py_compiler  r  r   PyCompileErrormsg)r  r  r  err)rt   r   r   _compile  s    	z)PyZipFile._get_codename.<locals>._compilez.pyz.pycz.pyoTFr   r  z%s/%sr   r   )	importlibutilcache_from_sourcer  rm   rO  r  rq  rv  	__debug__rY  )rt   r  r  r  Zfile_pyZfile_pycZfile_pyoZpycache_pycZpycache_pyor\  rJ  Zarchivenamer   )rt   r   r    sL    


$$$	$		$zPyZipFile._get_codenamer   )r   r   r   r   r   ru   r  r  r   r   r   r   r   /  s
   Qc                s  d d  l  } | j d  } |  d  k r= t j d d   }  n  |  sT |  d d k rn t |  t j d  n  |  d d k r t |   d k r t |  t j d  n  t |  d d	   } | j   Wd  QXn|  d d k r`t |   d k rt |  t j d  n  t |  d d	   } | j	   } Wd  QX| rSt d
 j
 |   n  t d  na|  d d k rt |   d k rt |  t j d  n  t |  d d	   } | j |  d  Wd  QXn |  d d k rt |   d k  rt |  t j d  n    f d d     t |  d d   } x |  d d   D]s } t j j |  } | st j j t j j |   } n  | d t j t j f k rd } n    | | |  qAWWd  QXn  d  S)Nr   aE          Usage:
            zipfile.py -l zipfile.zip        # Show listing of a zipfile
            zipfile.py -t zipfile.zip        # Test if a zipfile is valid
            zipfile.py -e zipfile.zip target # Extract zipfile into target dir
            zipfile.py -c zipfile.zip src ... # Create zipfile from sources
        r   -l-c-e-tr   r   z.The following enclosed file is corrupted: {!r}zDone testingr   c                s   t  j j |  r( |  j | | t  nv t  j j |  r | rS |  j | |  n  xH t  j |  D]4 }   |  t  j j | |  t  j j | |   qc Wn  d  S)N)rm   rO  r  rx  r   ri  r  rZ  )zfrO  zippathZnm)addToZipr   r   r    s    zmain.<locals>.addToZipr   r  )r  r  r  r  )textwrapZdedentrq   argvr  exitr7   r   r6  r8  formatrQ  rm   rO  r  rf  rc  rd  )argsr  ZUSAGEr  ZbadfilerO  r  r   )r  r   main  sT    	




!	r  __main__)yr   r   rm   r   importlib.utilr  rq   rt  rq  rk  r8   Zbinasciir   r   ImportErrorr   r   __all__	Exceptionr   r   r   r   rv   rn  r?  r   r   r   r   rs   ry   rz   r{   r'  rQ   rP   rx   rO   r?   r@   rA   rB   rC   rD   rE   rV   r  r  r  r!  r  r   Z_CD_CREATE_VERSIONZ_CD_CREATE_SYSTEMZ_CD_EXTRACT_VERSIONZ_CD_EXTRACT_SYSTEMZ_CD_FLAG_BITSZ_CD_COMPRESS_TYPEZ_CD_TIMEZ_CD_DATEZ_CD_CRCZ_CD_COMPRESSED_SIZEZ_CD_UNCOMPRESSED_SIZEr"  r$  r%  Z_CD_DISK_NUMBER_STARTZ_CD_INTERNAL_FILE_ATTRIBUTESZ_CD_EXTERNAL_FILE_ATTRIBUTESr&  r}   r~   rF  rG  Z_FH_EXTRACT_VERSIONZ_FH_EXTRACT_SYSTEMZ_FH_GENERAL_PURPOSE_FLAG_BITSZ_FH_COMPRESSION_METHODZ_FH_LAST_MOD_TIMEZ_FH_LAST_MOD_DATEZ_FH_CRCZ_FH_COMPRESSED_SIZEZ_FH_UNCOMPRESSED_SIZErH  rI  r:   r;   r6   r=   r>   r<   Z_CD64_SIGNATUREZ_CD64_DIRECTORY_RECSIZEZ_CD64_CREATE_VERSIONZ_CD64_EXTRACT_VERSIONZ_CD64_DISK_NUMBERZ_CD64_DISK_NUMBER_STARTZ_CD64_NUMBER_ENTRIES_THIS_DISKZ_CD64_NUMBER_ENTRIES_TOTALZ_CD64_DIRECTORY_SIZEZ_CD64_OFFSET_START_CENTDIRr.   r	   rK   r+   objectr
   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   <module>   s"  	

+==
  B