B
    u9a:                @   s  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W n ek
r   dZe	jZY nX yd dlZW n ek
r   dZY nX yd dlZW n ek
r   dZY nX dddddddd	d
dddgZG dd deZG dd deZe ZZdZdZ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$e"Z%d Z&dZ'dZ(dZ)dZ*dZ+d Z,d!Z-dZ.d"Z/d#Z0d$Z1e$e0Z2d Z3dZ4dZ5dZ6dZ7dZ8d Z9d!Z:dZ;d"Z<d%Z=d&Z>dZ?d'Z@dZAd(ZBd)ZCd*ZDd+ZEd,ZFd-ZGe$eFZHd ZIdZJdZKdZLdZMdZNd ZOd!ZPdZQd"ZRd%ZSd&ZTd.ZUd/ZVe$eUZWd0ZXd1ZYe$eXZZd Z[dZ\dZ]dZ^dZ_dZ`d Zad!ZbdZcd"Zdd2Zeefd3Zgd4d5 Zhd6d7 Zid8d	 Zjd9d: Zkd;d< ZlG d=d
 d
emZndaod>d? Zpd@dA ZqG dBdC dCZrG dDdE dEZsdFdGdHdHdHdHdIdJdKdLdIdMdNdOdPdQdRdSZtdTdU ZudgdVdWZvdXdY ZwG dZd[ d[ZxG d\d] d]ZyG d^d_ d_e jzZ{G d`da dae jzZ|G dbd dZ}G dcd de}Z~dhdddeZedfkre  dS )i    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.7/zipfile.pyr   &   s   c               @   s   e Zd ZdS )r   N)r   r   r   r   r   r   r   r   *   s   ii              -   .   ?   s   <4s4H2LHs   PK                     	   z<4s4B4HL2L5H2Ls   PK
                     z<4s2B4HL2L2Hs   PKz<4sLQLs   PKz
<4sQ2H2L4Qs   PKiPKz<HHc       
      C   s   t j}d}g }d }}xf|d t| kr||| ||d  \}}|d | }	||krv||krn|| ||  |	}d}|	}qW |s| S d|S )NFr   r   T    )_EXTRA_FIELD_STRUCTunpacklenappendjoin)
extraZxidsr+   ZmodifiedbufferstartiZxidZxlenjr   r   r   _strip_extra   s     r4   c             C   s*   yt | rdS W n tk
r$   Y nX dS )NTF)_EndRecDataOSError)fpr   r   r   _check_zipfile   s    r8   c          	   C   sV   d}y8t | drt| d}nt| d}t|}W d Q R X W n tk
rP   Y nX |S )NFread)r7   rb)hasattrr8   openr6   )filenameresultr7   r   r   r   r	      s    
c          
   C   s  y|  |t d W n tk
r(   |S X | t}t|tkrD|S tt|\}}}}|tkrd|S |dkst|dkr|t	d|  |t t
 d | t
}t|t
kr|S tt|\
}}}	}
}}}}}}|tkr|S ||t< ||t< ||t< ||t< ||t< ||t< ||t< |S )Nr   r   r   z3zipfiles that span multiple disks are not supported)seeksizeEndCentDir64Locatorr6   r9   r,   structr+   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    

 rS   c       	      C   sf  |  dd |  }y|  t d W n tk
r:   d S X |  }t|tkr|dd tkr|dd  dkrtt	|}t
|}|d ||t  t| t |S t|d t d}|  |d |  }|t}|dkrb|||t  }t|tkrd S t
tt	|}|t }||t |t |  }|| |||  t| || | |S d S )Nr   r   r   s     r)   i   )r?   tellsizeEndCentDirr6   r9   r,   stringEndArchiverA   r+   structEndArchivelistr-   rS   maxrfind_ECD_COMMENT_SIZE)	rN   ZfilesizerQ   rP   ZmaxCommentStartr1   ZrecDataZcommentSizecommentr   r   r   r5      s>    



r5   c               @   sR   e Zd ZdZdddZdd Zdd	d
Zdd Zdd Ze	dddZ
dd ZdS )r
   )orig_filenamer=   	date_timecompress_type_compresslevelr]   r/   create_systemrR   extract_versionreserved	flag_bitsvolumeinternal_attrexternal_attrheader_offsetCRCcompress_size	file_size	_raw_timeNoNamei  r   r   r   r   r   c             C   s   || _ |td}|dkr(|d| }tjdkrJtj|krJ|tjd}|| _|| _|d dk rjtdt	| _
d | _d| _d| _tjdkrd| _nd| _t| _t| _d| _d| _d| _d| _d| _d S )Nr   /i  z+ZIP does not support timestamps before 1980r)   win32r   )r^   findchrossepreplacer=   r_   
ValueErrorr   r`   ra   r]   r/   sysplatformrb   DEFAULT_VERSIONrR   rc   rd   re   rf   rg   rh   )selfr=   r_   Z	null_byter   r   r   __init__S  s0    
zZipInfo.__init__c             C   s   d| j j| jf g}| jtkr8|dt| j| j  | jd? }| jd@ }|rd|dt	
|  |rv|d|  |  }|r| jr|d| j  |r| jr| jtks| j| jkr|d| j  |d	 d
|S )Nz<%s filename=%rz compress_type=%sr&   i  z filemode=%rz external_attr=%#xz file_size=%rz compress_size=%r> )	__class__r   r=   r`   r   r-   compressor_namesgetrh   statfilemodeis_dirrl   rk   r.   )r{   r>   hiloisdirr   r   r   __repr__~  s(    






zZipInfo.__repr__Nc             C   s|  | j }|d d d> |d d> B |d B }|d d> |d	 d> B |d d B }| jd
@ rfd } }}n| j}| j}| j}| j}d}	|d kr|tkp|tk}|rd}
|t|
dt	|
d	 || }|tks|tkr|st
dd}d}t}	| jtkrtt|	}	n| jtkrtt|	}	t|	| j| _t|	| j| _|  \}}ttt| j| j|| j|||||t|t|}|| | S )Nr   i  r!   r   r   r   r   r#   r   r   z<HHQQz'Filesize would require ZIP64 extensionsl    )r_   re   rj   rk   rl   r/   ZIP64_LIMITrA   packcalcsizer   ZIP64_VERSIONr`   r   rZ   BZIP2_VERSIONr   LZMA_VERSIONrc   rR   _encodeFilenameFlagsstructFileHeaderstringFileHeaderrd   r,   )r{   zip64dtdosdatedostimerj   rk   rl   r/   min_versionfmtr=   re   headerr   r   r   
FileHeader  sF    $$



zZipInfo.FileHeaderc             C   s>   y| j d| jfS  tk
r8   | j d| jdB fS X d S )Nasciizutf-8i   )r=   encodere   UnicodeEncodeError)r{   r   r   r   r     s    zZipInfo._encodeFilenameFlagsc             C   sJ  | j }tj}x6t|dkrD|d|d d \}}|d t|krTtd||f |dkr2|dkrz|d|dd }nV|dkr|d	|dd
 }n:|dkr|d|dd }n|dkrd}ntd||f d}| jdkr|| | _|d7 }| jdkr|| | _|d7 }| jdkr2| j}|| | _|d7 }||d d  }qW d S )Nr   z<HHz"Corrupt extra field %04x (size=%d)r      z<QQQ   r&   z<QQr   r   z<Qr   r   r   )l    l    l    )r/   rA   r+   r,   r   rl   rk   ri   )r{   r/   r+   tpZlnZcountsidxoldr   r   r   _decodeExtra  s8    




zZipInfo._decodeExtrac             C   s   t |tjrt|}t|}t|j}t|j	}|dd }|d krP|}tj
tj
|d }x$|d tjtjfkr|dd  }qjW |r|d7 }| ||}|jd@ d> |_|rd|_| jdO  _n|j|_|S )Nr   r   r   rp   i  r&   )
isinstancert   PathLikefspathr   S_ISDIRst_modetime	localtimest_mtimepathnormpath
splitdriveru   altseprh   rl   st_size)clsr=   arcnamestr   mtimer_   zinfor   r   r   	from_file  s(    



zZipInfo.from_filec             C   s   | j d dkS )Nrp   )r=   )r{   r   r   r   r     s    zZipInfo.is_dir)rn   ro   )N)N)r   r   r   	__slots__r|   r   r   r   r   classmethodr   r   r   r   r   r   r
   9  s   
+
.&!c             C   s4   x.t dD ]"}| d@ r$| d? dA } q
| dL } q
W | S )Nr   r   l    q[ )range)crcr3   r   r   r   _gen_crc  s
    r   c                sp   dddt d kr&ttttda t fdd  fddx| D ]}| qNW fd	d
}|S )NixV4igE#ixV4   c                s   |d?  || A d@  A S )Nr      r   )Zchr   )crctabler   r   crc324  s    z_ZipDecrypter.<locals>.crc32c                s<    | d@  d@ d d d@  d? d S )Nr   l    ir   r   r   )c)r   key0key1key2r   r   update_keys8  s    
z"_ZipDecrypter.<locals>.update_keysc                sR   t  }|j}x<| D ]4} dB }|||dA  d? d@ N }| || qW t|S )Nr   r   r   r   )	bytearrayr-   bytes)rQ   r>   r-   r   k)r   r   r   r   	decrypterB  s    
z _ZipDecrypter.<locals>.decrypter)	_crctablerY   mapr   r   )pwdpr   r   )r   r   r   r   r   r   r   _ZipDecrypter*  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)r{   r   r   r   r|   R  s    zLZMACompressor.__init__c             C   sF   t dt ji}t jt jt t j|gd| _tdddt	|| S )Nid)filtersz<BBHr!   r   )
lzmaZ_encode_filter_propertiesFILTER_LZMA1r   
FORMAT_RAW_decode_filter_propertiesr   rA   r   r,   )r{   Zpropsr   r   r   _initU  s    zLZMACompressor._initc             C   s*   | j d kr|  | j | S | j |S )N)r   r   compress)r{   rQ   r   r   r   r   \  s    
zLZMACompressor.compressc             C   s&   | j d kr|  | j   S | j  S )N)r   r   flush)r{   r   r   r   r   a  s    
zLZMACompressor.flushN)r   r   r   r|   r   r   r   r   r   r   r   r   P  s   r   c               @   s   e Zd Zdd Zdd ZdS )LZMADecompressorc             C   s   d | _ d| _d| _d S )Nr)   F)_decomp_unconsumedeof)r{   r   r   r   r|   i  s    zLZMADecompressor.__init__c          	   C   s   | j d kr|  j|7  _t| jdkr*dS td| jdd \}t| jd| krXdS tjtjttj	| jdd|  gd| _ | jd| d  }| `| j 
|}| j j| _|S )Nr   r)   z<Hr   )r   )r   r   r,   rA   r+   r   r   r   r   r   
decompressr   )r{   rQ   Zpsizer>   r   r   r   r   n  s    

zLZMADecompressor.decompressN)r   r   r   r|   r   r   r   r   r   r   g  s   r   storeZshrinkreduceZimplodetokenizeZdeflateZ	deflate64Zbzip2r   ZterseZlz77ZwavpackZppmd)r   r   r   r   r   r   r   r    r   r!   r"   r   r   r(      a   b   c             C   sX   | t kr
nJ| tkr tsTtdn4| tkr6tsTtdn| tkrLtsTtd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   zlibRuntimeErrorr   bz2r   r   NotImplementedError)compressionr   r   r   _check_compression  s    r   c             C   sj   | t kr2|d k	r t|tjdS ttjtjdS | tkrT|d k	rLt|S t S | tkrbt	 S d S d S )Ni)
r   r   ZcompressobjZDEFLATEDZZ_DEFAULT_COMPRESSIONr   r   ZBZ2Compressorr   r   )r`   compresslevelr   r   r   _get_compressor  s    
r   c             C   sn   | t krd S | tkrtdS | tkr.t S | tkr<t S t	
| }|r\td| |f ntd| f d S )Nizcompression type %d (%s)zcompression type %d)r   r   r   Zdecompressobjr   r   ZBZ2Decompressorr   r   r   r   r   )r`   Zdescrr   r   r   _get_decompressor  s    

r   c               @   s0   e Zd Zdd ZdddZdddZd	d
 ZdS )_SharedFilec             C   s2   || _ || _|| _|| _|| _|j| _|j| _d S )N)_file_pos_close_lock_writingseekablerU   )r{   fileposcloselockZwritingr   r   r   r|     s    z_SharedFile.__init__r   c          	   C   sB   | j 2 |  rtd| j|| | j | _| jS Q R X d S )Nz}Can't reposition in the ZIP file while there is an open writing handle on it. Close the writing handle before trying to read.)r   r   rw   r   r?   rU   r   )r{   rO   whencer   r   r   r?     s    z_SharedFile.seekr   c          	   C   sL   | j < |  rtd| j| j | j|}| j | _|S Q R X d S )NzyCan't read from the ZIP file while there is an open writing handle on it. Close the writing handle before trying to read.)r   r   rw   r   r?   r   r9   rU   )r{   nrQ   r   r   r   r9     s    z_SharedFile.readc             C   s$   | j d k	r | j }d | _ | | d S )N)r   r   )r{   fileobjr   r   r   r     s    
z_SharedFile.closeN)r   )r   )r   r   r   r|   r?   r9   r   r   r   r   r   r     s   	


r   c               @   s4   e Zd Zdd Zdd Zdd Zdd Zd	d
 ZdS )	_Tellablec             C   s   || _ d| _d S )Nr   )r7   rO   )r{   r7   r   r   r   r|     s    z_Tellable.__init__c             C   s   | j |}|  j|7  _|S )N)r7   writerO   )r{   rQ   r   r   r   r   r     s    z_Tellable.writec             C   s   | j S )N)rO   )r{   r   r   r   rU     s    z_Tellable.tellc             C   s   | j   d S )N)r7   r   )r{   r   r   r   r     s    z_Tellable.flushc             C   s   | j   d S )N)r7   r   )r{   r   r   r   r     s    z_Tellable.closeN)r   r   r   r|   r   rU   r   r   r   r   r   r   r     s
   r   c                   s   e Zd ZdZdZdZd%ddZ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dd Zd)d!d"Zd#d$ Z  ZS )*
ZipExtFilei   @i   i   NFc             C   s   || _ || _|| _|j| _|j| _|j| _t	| j| _
d| _d| _d| _d | _| jd k	rf|  jd8  _|| _|j| _t|dr|j| _td| _nd | _d| _y4| r| | _|j| _|j| _| j| _d| _W n tk
r   Y nX d S )NFr)   r   r   rj   T)_fileobj
_decrypter_close_fileobjr`   _compress_typerk   _compress_leftrl   _leftr   _decompressor_eof_readbuffer_offsetnewlinesmoder=   namer;   rj   _expected_crcr   _running_crc	_seekabler   rU   _orig_compress_start_orig_compress_size_orig_file_size_orig_start_crcAttributeError)r{   r   r	  zipinfor   Zclose_fileobjr   r   r   r|     s:    



zZipExtFile.__init__c             C   sv   d| j j| j jf g}| jsX|d| j| jf  | jtkrb|dt	
| j| j  n
|d |d d|S )Nz<%s.%sz name=%r mode=%rz compress_type=%sz	 [closed]r}   r~   )r   r   r   closedr-   r
  r	  r  r   r   r   r.   )r{   r>   r   r   r   r   >  s    


zZipExtFile.__repr__r   c             C   sL   |dk r>| j d| jd }|dkr>| j | j| }|| _|S tj| |S )Nr      
r   )r  rr   r  ioBufferedIOBasereadline)r{   limitr2   liner   r   r   r  L  s    zZipExtFile.readliner   c             C   sr   |t | j| j kr\| |}t || jkrJ|| j| jd   | _d| _n|  jt |8  _| j| j| jd  S )Nr   i   )r,   r  r  r9   )r{   r   chunkr   r   r   peek\  s    
zZipExtFile.peekc             C   s   dS )NTr   )r{   r   r   r   readablei  s    zZipExtFile.readablec             C   s  |d ks|dk rL| j | jd  }d| _ d| _x| jsF|| | j7 }q.W |S || j }|t| j k r~| j | j| }|| _|S |t| j  }| j | jd  }d| _ d| _x^|dkr| js| |}|t|k r|| _ || _||d | 7 }P ||7 }|t|8 }qW |S )Nr   r)   )r  r  r  _read1MAX_Nr,   )r{   r   bufendrQ   r   r   r   r9   l  s4    

zZipExtFile.readc             C   s@   | j d krd S t|| j| _| jr<| j| j kr<td| j d S )NzBad CRC-32 for file %r)r  r   r  r  r   r
  )r{   newdatar   r   r   _update_crc  s
    
zZipExtFile._update_crcc             C   s  |d ks|dk rV| j | jd  }d| _ d| _x$| jsP| | j}|r.||7 }P q.W |S || j }|t| j k r| j | j| }|| _|S |t| j  }| j | jd  }d| _ d| _|dkrxN| js
| |}|t|k r|| _ || _||d | 7 }P |r||7 }P qW |S )Nr   r)   )r  r  r  r  r  r,   )r{   r   r   rQ   r!  r   r   r   read1  s>    



zZipExtFile.read1c             C   s"  | j s|dkrdS | jtkrH| jj}|t|krR|| |t| 7 }n
| |}| jtkrj| jdk| _ nx| jtkrt	|| j
}| j||}| jjp| jdko| jj | _ | j r|| j 7 }n | j|}| jjp| jdk| _ |d | j }|  jt|8  _| jdkrd| _ | | |S )Nr   r)   T)r  r  r   r  Zunconsumed_tailr,   _read2r   r  rZ   MIN_READ_SIZEr   r   r   r  r#  )r{   r   rQ   r   r   r   r    s2    





zZipExtFile._read1c             C   sd   | j dkrdS t|| j}t|| j }| j|}|  j t|8  _ |sLt| jd k	r`| |}|S )Nr   r)   )	r  rZ   r&  minr   r9   r,   EOFErrorr   )r{   r   rQ   r   r   r   r%    s    


zZipExtFile._read2c                s&   z| j r| j  W d t   X d S )N)r   r   r   super)r{   )r   r   r   r     s    zZipExtFile.closec             C   s   | j S )N)r  )r{   r   r   r   r     s    zZipExtFile.seekabler   c             C   s*  | j std|  }|dkr&|}n.|dkr8|| }n|dkrL| j| }ntd|| jkrd| j}|dk rpd}|| }|| j }|dkr|t| jk r|| _d}nP|dk r| j	
| j | j| _| j| _| j| _d| _d| _t| j| _d| _|}x,|dkr t| j|}| | ||8 }qW |  S )Nz!underlying stream is not seekabler   r   r   zCwhence must be os.SEEK_SET (0), os.SEEK_CUR (1), or os.SEEK_END (2)r)   F)r  r  UnsupportedOperationrU   r  rw   r  r,   r  r   r?   r  r  r  r  r  r  r   r  r  r  r'  MAX_SEEK_READr9   )r{   rO   r   Zcurr_posZnew_posZread_offsetZbuff_offsetZread_lenr   r   r   r?     sD    




zZipExtFile.seekc             C   s0   | j std| j| j t| j | j }|S )Nz!underlying stream is not seekable)r  r  r*  r  r  r,   r  r  )r{   Zfileposr   r   r   rU   (  s    
zZipExtFile.tell)NF)r   )r   )r   )r   )r   r   r   r  r&  r+  r|   r   r  r  r  r9   r#  r$  r  r%  r   r   r?   rU   __classcell__r   r   )r   r   r     s$    
*


!
%$
.r   c                   s@   e Zd Zdd Zedd Zdd Zdd Z fd	d
Z  Z	S )_ZipWriteFilec             C   s8   || _ || _|| _t|j|j| _d| _d| _d| _	d S )Nr   )
_zinfo_zip64_zipfiler   r`   ra   _compressor
_file_size_compress_size_crc)r{   zfr   r   r   r   r   r|   0  s    
z_ZipWriteFile.__init__c             C   s   | j jS )N)r0  r7   )r{   r   r   r   r   :  s    z_ZipWriteFile._fileobjc             C   s   dS )NTr   )r{   r   r   r   writable>  s    z_ZipWriteFile.writablec             C   sf   | j rtdt|}|  j|7  _t|| j| _| jrV| j|}|  jt|7  _| j	
| |S )NzI/O operation on closed file.)r  rw   r,   r2  r   r4  r1  r   r3  r   r   )r{   rQ   nbytesr   r   r   r   A  s    z_ZipWriteFile.writec          	      sb  | j r
d S zFt   | jrR| j }|  jt|7  _| j| | j| j	_
n
| j| j	_
| j| j	_| j| j	_| j	jd@ r| jrdnd}| jt|t| j	j| j	j
| j	j | j | j_nn| js| jtkrtd| jtkrtd| j | j_| j| j	j | j| j	| j | j| jj | jj| j	 | j	| jj| j	j< W d d| j_ X d S )Nr   z<LLQQz<LLLLz+File size unexpectedly exceeded ZIP64 limitz1Compressed size unexpectedly exceeded ZIP64 limitF)!r  r)  r   r1  r   r3  r,   r   r   r.  rk   r2  r4  rj   rl   re   r/  rA   r   _DD_SIGNATURErU   r0  	start_dirr   r   r?   ri   r   filelistr-   
NameToInfor=   r   )r{   r   r   )r   r   r   r   M  s>    






z_ZipWriteFile.close)
r   r   r   r|   propertyr   r6  r   r   r,  r   r   )r   r   r-  /  s
   
r-  c               @   s  e Zd ZdZdZdeddfddZ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!d"Zd>d#d$Zd?d%d&Zd@d'd(Zed)d* Zd+d, Zd-d. ZdAd/d0ZdBd1d2Zd3d4 Zd5d6 Z d7d8 Z!d9d: Z"dS )Cr   NrTc       	   	   C   sL  |dkrt dt| || _d| _d| _i | _g | _|| _|| _|| _	d | _
d| _t|tjrjt|}t|trd| _|| _dddd	dd
dd}|| }x^yt||| _W n( tk
r   ||kr|| }w Y nX P qW nd| _|| _t|dd | _d| _t | _d| _d| _y|dkr0|   n|dkrd| _y| j | _ W n2 t!tfk
r   t"| j| _d| _ d| _Y n6X y| j#| j  W n  t!tfk
r   d| _Y nX nf|dkry|   | j#| j  W n6 t$k
r   | j#dd d| _| j | _ Y nX nt dW n$   | j}d | _| %|  Y nX d S )N)r=  wxaz+ZipFile requires mode 'r', 'w', 'x', or 'a'Fr   r)   r:   zw+bzx+bzr+bwbZxb)r=  r>  r?  r@  zr+bzw+bzx+br   r
  Tr=  )r>  r?  r@  r   z"Mode must be 'r', 'w', 'x', or 'a')&rw   r   _allowZip64
_didModifydebugr;  r:  r   r   r	  r   _commentr   rt   r   r   str_filePassedr=   r  r<   r7   r6   getattr_fileRefCnt	threadingRLockr   r  r   _RealGetContentsrU   r9  r  r   r?   r   _fpclose)	r{   r   r	  r   
allowZip64r   ZmodeDictr   r7   r   r   r   r|     s    







zZipFile.__init__c             C   s   | S )Nr   )r{   r   r   r   	__enter__  s    zZipFile.__enter__c             C   s   |    d S )N)r   )r{   typevalue	tracebackr   r   r   __exit__  s    zZipFile.__exit__c             C   s   d| j j| j jf g}| jd k	rd| jr8|d| j  n| jd k	rR|d| j  |d| j  n
|d |d d|S )Nz<%s.%sz file=%rz filename=%rz mode=%rz	 [closed]r}   r~   )	r   r   r   r7   rG  r-   r=   r	  r.   )r{   r>   r   r   r   r     s    



zZipFile.__repr__c             C   s  | j }yt|}W n tk
r.   tdY nX |s<td| jdkrNt| |t }|t }|t | _	|t
 | | }|t tkr|tt 8 }| jdkr|| }td||| || | _|| jd ||}t|}d}x||k r|t}	t|	tkrtdtt|	}	|	t tkr0td| jdkrDt|	 ||	t }
|	d }|d	@ rp|
d
}
n
|
d}
t|
}||	t |_||	t  |_!|	t" |_#|	dd \|_$|_%|_&|_'|_(|_)}}|_*|_+|_,|j&t-krt.d|j&d  |	dd \|_/|_0|_1||_2|d? d |d? d@ |d@ |d? |d? d@ |d@ d f|_3|4  |j#| |_#| j56| || j7|j8< |t |	t  |	t  |	t   }| jdkrtd| qW d S )Nz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   total)9r7   r5   r6   r   rD  printrL   rM   _ECD_COMMENTrE  _ECD_LOCATIONrG   rF   rD   r@   r9  r?   r9   r  BytesIOsizeCentralDirr,   rA   r+   structCentralDir_CD_SIGNATUREstringCentralDir_CD_FILENAME_LENGTHdecoder
   _CD_EXTRA_FIELD_LENGTHr/   _CD_COMMENT_LENGTHr]   _CD_LOCAL_HEADER_OFFSETri   rR   rb   rc   rd   re   r`   rj   rk   rl   MAX_EXTRACT_VERSIONr   rf   rg   rh   rm   r_   r   r:  r-   r;  r=   )r{   r7   rP   Zsize_cdZ	offset_cdconcatZinferredrQ   rV  centdirr=   flagsr?  tdr   r   r   rL    sp    









4  
zZipFile._RealGetContentsc             C   s   dd | j D S )Nc             S   s   g | ]
}|j qS r   )r=   ).0rQ   r   r   r   
<listcomp>T  s    z$ZipFile.namelist.<locals>.<listcomp>)r:  )r{   r   r   r   namelistR  s    zZipFile.namelistc             C   s   | j S )N)r:  )r{   r   r   r   infolistV  s    zZipFile.infolistc             C   sP   t dd |d x:| jD ]0}d|jd d  }t d|j||jf |d qW d S )Nz%-46s %19s %12s)z	File NamezModified    ZSize)r   z%d-%02d-%02d %02d:%02d:%02dr   z%-46s %s %12d)rW  r:  r_   r=   rl   )r{   r   r   Zdater   r   r   printdir[  s    zZipFile.printdirc          
   C   s^   d}xT| j D ]J}y.| |jd}x||r0q$W W d Q R X W q tk
rT   |jS X qW d S )Ni   r=  )r:  r<   r=   r9   r   )r{   Z
chunk_sizer   fr   r   r   testzipd  s    zZipFile.testzipc             C   s$   | j |}|d kr td| |S )Nz(There is no item named %r in the archive)r;  r   KeyError)r{   r
  infor   r   r   getinfoq  s
    
zZipFile.getinfoc             C   s6   |r t |ts tdt|j |r,|| _nd | _d S )Nzpwd: expected bytes, got %s)r   r   	TypeErrorrP  r   r   )r{   r   r   r   r   setpasswordz  s
    zZipFile.setpasswordc             C   s   | j S )N)rE  )r{   r   r   r   r]     s    zZipFile.commentc             C   s^   t |tstdt|j t|tkrNdd l}|jdt dd |d t }|| _	d| _
d S )Nzcomment: expected bytes, got %sr   z3Archive comment is too long; truncating to %d bytesr   )
stacklevelT)r   r   rt  rP  r   r,   ZIP_MAX_COMMENTwarningswarnrE  rC  )r{   r]   rx  r   r   r   r]     s    
c          	   C   s"   |  |d|
}| S Q R X d S )Nr=  )r<   r9   )r{   r
  r   r7   r   r   r   r9     s    zZipFile.readF)force_zip64c               sv  |dkrt d|r0t|ts0tdt|j |rD|dkrDt d jsRt dt|trb|}n,|dkrt|} j|_	 j
|_n
 |}|dkr j||dS  jrt d  jd	7  _t j|j j j fd
d}yx|t}t|tkrtdtt|}|t tkr&td||t }|t rL||t  |jd@ r`t d|jd@ rtt d|jd@ r|!d}	n
|!d}	|	|j"krtd|j"|f |jd	@ }
d }|
rL|sԈ j#}|st$d| t%|}|d}||dd }|jd@ r$|j&d? d@ }n|j'd? d@ }|d |krLt$d| t(||||dS    |)   Y nX d S )N>   r>  r=  zopen() requires mode "r" or "w"zpwd: expected bytes, got %sr>  z'pwd is only supported for reading filesz2Attempt to use ZIP archive that was already closed)rz  zyCan't read from the ZIP file while there is an open writing handle on it. Close the writing handle before trying to read.r   c                  s    j S )N)r   r   )r{   r   r   <lambda>  r)   zZipFile.open.<locals>.<lambda>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-8rT  z/File name in directory %r and header %r differ.z6File %r is encrypted, password required for extractionr   r   r   r   r   r#   zBad password for file %rT)*rw   r   r   rt  rP  r   r7   r
   r   r`   r   ra   rs  _open_to_writer   rI  r   ri   rM  r   r9   sizeFileHeaderr,   r   rA   r+   r   _FH_SIGNATUREr   _FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTHre   r   r`  r^   r   r   r   rm   rj   r   r   )r{   r
  r	  r   rz  r   Zzef_fileZfheaderfnameZ	fname_strZis_encryptedZzdr   hZ
check_byter   )r{   r   r<     s    








zZipFile.openc             C   s   |r| j std| jr tdt|ds0d|_d|_d|_d|_|jt	krZ| jdO  _| j
sn| jdO  _|jszd|_| j o|p|jd tk}| j
r| j| j | j |_| | d	| _| j|| d	| _t| ||S )
NzHforce_zip64 is True, but allowZip64 was False when opening the ZIP file.zzCan't write to the ZIP file while there is another write handle open on it. Close the first handle before opening another.rl   r   r   r   i  g?T)rB  rw   r   r;   rl   rk   rj   re   r`   r   r  rh   r   r7   r?   r9  rU   ri   _writecheckrC  r   r   r-  )r{   r   rz  r   r   r   r   r~    s4    



zZipFile._open_to_writec             C   s*   |d krt  }n
t |}| |||S )N)rt   getcwdr   _extract_member)r{   memberr   r   r   r   r   extract7  s    

zZipFile.extractc             C   sL   |d kr|   }|d kr"t }n
t|}x|D ]}| ||| q2W d S )N)rl  rt   r  r   r  )r{   r   membersr   r  r   r   r   
extractallD  s    


zZipFile.extractallc             C   s^   | j }|s(d}t|dt| }|| _ ||}dd ||D }|dd |D }|S )Nz:<>|"?*_c             s   s   | ]}| d V  qdS ).N)rstrip)rj  r?  r   r   r   	<genexpr>_  s    z1ZipFile._sanitize_windows_name.<locals>.<genexpr>c             s   s   | ]}|r|V  qd S )Nr   )rj  r?  r   r   r   r  a  s    )!_windows_illegal_name_trans_tablerF  	maketransr,   	translatesplitr.   )r   r   pathseptableZillegalr   r   r   _sanitize_windows_nameU  s    
zZipFile._sanitize_windows_namec          
      sL  t |ts| |}|jdtjj}tjjrB|tjjtjj}tj	|d }dtjj
tjjf tjj fdd|tjjD }tjjdkr| |tjj}tj||}tj|}tj|}|rtj|st| | rtj|st| |S | j||d(}t|d}t|| W d Q R X W d Q R X |S )	Nrp   r   r~   c             3   s   | ]}| kr|V  qd S )Nr   )rj  r?  )invalid_path_partsr   r   r  u  s    z*ZipFile._extract_member.<locals>.<genexpr>\)r   rA  )r   r
   rs  r=   rv   rt   r   ru   r   r   curdirpardirr.   r  r  r   dirnameexistsmakedirsr   r   mkdirr<   shutilcopyfileobj)r{   r  Z
targetpathr   r   Z	upperdirssourcetargetr   )r  r   r  d  s.    

&


 zZipFile._extract_memberc             C   s   |j | jkr(dd l}|jd|j  dd | jdkr:td| jsHtdt|j | j	sd }t
| jtkrpd}n|jtkrd	}n|jtkrd
}|rt|d d S )Nr   zDuplicate name: %rr   )rv  )r>  r?  r@  z&write() requires mode 'w', 'x', or 'a'z4Attempt to write ZIP archive that was already closedzFiles countZFilesizezZipfile sizez would require ZIP64 extensions)r=   r;  rx  ry  r	  rw   r7   r   r`   rB  r,   r:  ZIP_FILECOUNT_LIMITrl   r   ri   r   )r{   r   rx  requires_zip64r   r   r   r    s(    



zZipFile._writecheckc          
   C   sJ  | j std| jrtdt||}| r>d|_d|_n0|d k	rN||_n| j	|_|d k	rf||_
n| j|_
| r
| j | jr| j | j | j  |_|jtkr| jdO  _| | d| _| j| || j|j< | j |d | j  | _W d Q R X n<t|d,}| |d}t||d	 W d Q R X W d Q R X d S )
Nz7Attempt to write to ZIP archive that was already closedz>Can't write to ZIP archive while an open writing handle existsr   r   TFr:   r>  i    )r7   rw   r   r
   r   r   rk   rj   r`   r   ra   r   r   r  r?   r9  rU   ri   r   re   r  rC  r:  r-   r;  r=   r   r   r<   r  r  )r{   r=   r   r`   r   r   srcdestr   r   r   r     s>    


zZipFile.writec          
   C   s   t |tr|d}t |tsxt|tt d d d}| j|_| j|_	|j
d dkrpd|_| jdO  _q|d|_n|}| jstd	| jrtd
|d k	r||_|d k	r||_	t||_| j* | j|dd}|| W d Q R X W d Q R X d S )Nzutf-8r   )r=   r_   r   rp   i  Ar&   i  z7Attempt to write to ZIP archive that was already closedz?Can't write to ZIP archive while an open writing handle exists.r>  )r	  )r   rF  r   r
   r   r   r   r`   r   ra   r=   rh   r7   rw   r   r,   rl   r   r<   r   )r{   Zzinfo_or_arcnamerQ   r`   r   r   r  r   r   r   writestr  s4    



zZipFile.writestrc             C   s   |    d S )N)r   )r{   r   r   r   __del__  s    zZipFile.__del__c          	   C   s|   | j d krd S | jrtdzB| jdkr\| jr\| j" | jrJ| j | j | 	  W d Q R X W d | j }d | _ | 
| X d S )NzvCan't close the ZIP file while there is an open writing handle on it. Close the writing handle before closing the zip.)r>  r?  r@  )r7   r   rw   r	  rC  r   r  r?   r9  _write_end_recordrM  )r{   r7   r   r   r   r     s    
zZipFile.closec             C   sr  xP| j D ]D}|j}|d d d> |d d> B |d B }|d d> |d	 d> B |d d B }g }|jtksv|jtkr||j ||j d
}d
}n|j}|j}|jtkr||j d
}n|j}|j}	d}
|rt|	d}	t	j
ddt|  ddt| f| |	 }	t}
|jtkr(tt|
}
n|jtkr>tt|
}
t|
|j}t|
|j}yZ| \}}t	
tt||j||j||j|||j||t|t|	t|jd|j|j|}W nl tk
r   ttt||j||j|j |j|||j||t|j!t|	t|jd|j|j|ft"j#d  Y nX | j$%| | j$%| | j$%|	 | j$%|j q
W | j$& }t| j }|| j' }| j'}d }|t(krd}n|tkrd}n|tkrd}|r*| j)st*|d t	
t+t,ddddd||||}| j$%| t	
t-t.d|d}| j$%| t/|d}t/|d
}t/|d
}t	
t0t1dd||||t| j2	}| j$%| | j$%| j2 | j$3  d S )Nr   i  r!   r   r   r   r   r#   r   l    )r   z<HHQr   )r   zFiles countzCentral directory offsetzCentral directory sizez would require ZIP64 extensions,   r   i  )4r:  r_   rl   r   rk   r-   ri   r/   r4   rA   r   r,   r   r`   r   rZ   r   r   r   rc   rR   r   r\  r^  rb   rd   rj   r]   rg   rh   DeprecationWarningrW  re   r=   rx   stderrr7   r   rU   r9  r  rB  r   rE   rF   rB   rC   r'  rX   rW   rE  r   )r{   r   r   r   r   r/   rl   rk   ri   Z
extra_datar   rc   rR   r=   re   rf  Zpos2ZcentDirCountZcentDirSizeZcentDirOffsetr  Zzip64endrecZzip64locrecrP   r   r   r   r    s    $$



















zZipFile._write_end_recordc             C   s&   |  j d8  _ | j s"| js"|  d S )Nr   )rI  rG  r   )r{   r7   r   r   r   rM  ~  s    zZipFile._fpclose)N)N)r=  N)F)NN)NNN)NNN)NN)#r   r   r   r7   r  r   r|   rO  rS  r   rL  rl  rm  rn  rp  rs  ru  r<  r]   setterr9   r<   r~  r  r  r   r  r  r  r   r  r  r   r  rM  r   r   r   r   r   }  s>   XN
			
r
+

* 
0
(fc               @   s0   e Zd ZdeddfddZddd	Zd
d ZdS )r   r=  Tr   c             C   s   t j| ||||d || _d S )N)r	  r   rN  )r   r|   	_optimize)r{   r   r	  r   rN  optimizer   r   r   r|     s    zPyZipFile.__init__r~   Nc             C   s  t |}|rD||sD| jr@t j|r,dnd}td||f  d S t j|\}}t j|rpt j|d}t j|r|rd||f }n|}| jrtd|d| | 	|dd	 |\}}	| jrtd
|	 | 
||	 tt |}
|
d x|
D ]}t j||}t j|\}}t j|rTt jt j|dr| j|||d q|dkr|r||s| jrtd|  q| 	|dd	 |\}}	| jrtd
|	 | 
||	 qW n| jrtd| xtt |D ]}t j||}t j|\}}|dkr|r2||s2| jrtd|  q| 	|dd	 |\}}	| jr\td
|	 | 
||	 qW nP|d	d  dkrtd| 	|dd	 |\}}	| jrtd|	 | 
||	 d S )Nr   r   z%s %r skipped by filterfuncz__init__.pyz%s/%szAdding package inasr   ZAdding)
filterfuncz.pyzfile %r skipped by filterfunczAdding files from directoryz.Files added with writepy() must end with ".py"zAdding file)rt   r   rD  r   r   rW  r  r.   isfile_get_codenamer   sortedlistdirremovesplitextwritepyr   )r{   pathnamebasenamer  Zlabeldirr
  Zinitnamer  r   Zdirlistr=   r   rootZextr   r   r   r    sx    











zPyZipFile.writepyc                s  d fdd	}|d }|d }t jj|dd}t jj|dd}t jj|d	d} jdkr\tj|rt|jt|jkr| }	}
ntj|rt|jt|jkr|}
|}	ntj|rt|jt|jkr|}
|}	nvtj|rt|jt|jkr|}
|}	nD||rRt	j
jd
kr4|}
nt	j
jdkrH|}
n|}
|}	n| }
}	n jd
krr|}
|}	n<|}	 jdkr|}
n& jd	kr|}
nd j}t|tj|
rt|
jt|jks|| jds| }
}	tj|	d }|rd||f }|
|fS )Nr   c          
      s`   dd l } jrtd|  y|j| d|d W n. |jk
rZ } zt|j dS d }~X Y nX dS )Nr   Z	CompilingT)doraiser  F)
py_compilerD  rW  compilePyCompileErrormsg)r   r  r  err)r{   r   r   _compile  s    

z)PyZipFile._get_codename.<locals>._compilez.pyz.pycr~   )optimizationr   r   r   z"invalid value for 'optimize': {!r})r  z%s/%s)r   )	importlibutilcache_from_sourcer  rt   r   r  r   r   rx   rg  r  formatrw   r  )r{   r  r  r  Zfile_pyZfile_pycZpycache_opt0Zpycache_opt1Zpycache_opt2r   r  r  Zarchivenamer   )r{   r   r    s`    


zPyZipFile._get_codename)r~   N)r   r   r   r   r|   r  r  r   r   r   r   r     s   
Rc          	      s  dd l }d}|j|d}|jdd}|jdddd	d
 |jdddddd |jdddddd |jddddd
 || } | jd k	r| j}t|d}| }W d Q R X |rtd	| td n| j
d k	r| j
}t|d}|  W d Q R X n| jd k	r.| j\}}t|d}|| W d Q R X n| jd k	r| jd}	| j}
 fdd t|	d`}xX|
D ]P}tj|}|stjtj|}|dtjtjfkrd} ||| qjW W d Q R X d S ) Nr   z3A simple command-line interface for zipfile module.)descriptionT)requiredz-lz--listz	<zipfile>zShow listing of a zipfile)metavarhelpz-ez	--extractr   )z	<zipfile>z<output_dir>zExtract zipfile into target dir)nargsr  r  z-cz--create+)z<name>z<file>zCreate zipfile from sourcesz-tz--testzTest if a zipfile is validr=  z.The following enclosed file is corrupted: {!r}zDone testingc                st   t j|r| ||t nTt j|rp|r8| || x6tt |D ]$} | t j||t j|| qHW d S )N)	rt   r   r  r   r   r   r  r  r.   )r5  r   zippathZnm)addToZipr   r   r  [  s    zmain.<locals>.addToZipr>  r~   )argparseArgumentParseradd_mutually_exclusive_groupadd_argument
parse_argsZtestr   rp  rW  r  rY   rn  r  r  Zcreatepoprt   r   r  r  r  r  )argsr  r  parsergroupr  r5  Zbadfiler  Zzip_namefilesr   r  r   )r  r   main3  sT    








r  __main__)N)N)r  rt   importlib.utilr  rx   r   r   r  rA   ZbinasciirJ  r   r   ImportErrorr   r   __all__	Exceptionr   r   r   r   r   r  rw  r   r   r   r   rz   r   r   r   rd  rX   rW   r   rV   rG   rH   rI   rJ   rK   rL   rM   r\   rX  rY  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_  ra  rb  Z_CD_DISK_NUMBER_STARTZ_CD_INTERNAL_FILE_ATTRIBUTESZ_CD_EXTERNAL_FILE_ATTRIBUTESrc  r   r   r  r  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_SIZEr  r  rB   rC   r@   rE   rF   rD   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_CENTDIRr8  Structr*   r4   r8   r	   rS   r5   objectr
   r   r   r   r   r   r   r   r   r   r   r   r  r   r-  r   r   r  r   r   r   r   r   <module>   s>  









+= a&
&  ,N       /
<
