U
    pdV                    @   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ddlZddlZddlZddlZzddlZejZW n ek
r   dZejZY nX zddlZW n ek
r   dZY nX z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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!Z0d"Z1d#Z2dZ3d$Z4d%Z5d&Z6e)e5Z7dZ8dZ9dZ:dZ;d Z<d!Z=d"Z>d#Z?dZ@d$ZAd'ZBd(ZCdZDd)ZEdZFd*ZGd+ZHd,ZId-ZJd.ZKd/ZLe)eKZMdZNdZOdZPdZQd ZRd!ZSd"ZTd#ZUdZVd$ZWd'ZXd(ZYd0ZZd1Z[e)eZZ\d2Z]d3Z^e)e]Z_dZ`dZadZbdZcd Zdd!Zed"Zfd#ZgdZhd$Zid4Zjekd5Zld6d7 Zmd8d9 Znd:d
 Zod;d< Zpd=d> ZqG d?d derZsdatd@dA ZudBdC ZvG dDdE dEZwG dFdG dGZxdHdIdJdJdJdJdKdLdMdNdKdOdPdQdRdSdTdUZydVdW ZzdtdXdYZ{dZd[ Z|G d\d] d]Z}G d^d_ d_Z~G d`da daejZG dbdc dcejZG ddd dZG ded deZdfdg Zdhdi ZejZdjdk ZG dldm dmeZG dndo doeZG dpd dZdudqdrZedskr
e  dS )vzP
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LargeZipFilePathc                   @   s   e Zd ZdS )r   N)__name__
__module____qualname__ r   r   /usr/lib/python3.8/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   i              -   .   ?   s   <4s4H2LHs   PK                     	   z<4s4B4HL2L5H2Ls   PK
                     z<4s2B4HL2L2Hs   PKz<4sLQLs   PKz
<4sQ2H2L4Qs   PKiPK<HHc           
      C   s   t j}d}g }d }}|d t| krz|| ||d  \}}|d | }	||krt||krl|| ||  |	}d}|	}q|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     r8   c                 C   s,   zt | rW dS W n tk
r&   Y nX dS )NTF)_EndRecDataOSErrorfpr   r   r   _check_zipfile   s    
r=   c              	   C   sV   d}z8t | drt| d}nt| d}t|}W 5 Q R X W n tk
rP   Y n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;   rb)hasattrr=   openr:   )filenameresultr<   r   r   r   r	      s    
c              
   C   s  z|  |t d W n tk
r,   | Y S X | t}t|tkrH|S tt|\}}}}|tkrh|S |dksx|dkrt	d|  |t t
 d | t
}t|t
kr|S t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>   r0   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   s@    



   rX   c           	      C   sh  |  dd |  }z|  t d W n tk
r<   Y 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rd|||t  }t|tkrdS t
tt	|}|t }||t |t |  }|| |||  t| || | |S d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-   i   )rD   tellsizeEndCentDirr:   r>   r0   stringEndArchiverF   r/   structEndArchivelistr1   rX   maxrfind_ECD_COMMENT_SIZE)	rS   ZfilesizerV   rU   ZmaxCommentStartr5   ZrecDataZcommentSizecommentr   r   r   r9     sD    



r9   c                   @   s\   e Zd 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ddZdd Zd	S )r
   z>Class with attributes describing each file in the ZIP archive.)orig_filenamerB   	date_timecompress_type_compresslevelrb   r3   create_systemrW   extract_versionreserved	flag_bitsvolumeinternal_attrexternal_attrheader_offsetCRCcompress_size	file_size	_raw_timeNoName  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   /ru   z+ZIP does not support timestamps before 1980r-   Zwin32r   )rc   findchrossepreplacerB   rd   
ValueErrorr   re   rf   rb   r3   sysplatformrg   DEFAULT_VERSIONrW   rh   ri   rj   rk   rl   rm   )selfrB   rd   Z	null_byter   r   r   __init__X  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=%r compress_type=%sr)   r   z filemode=%rz external_attr=%#xz file_size=%rz compress_size=%r> )	__class__r   rB   re   r   r1   compressor_namesgetrm   statfilemodeis_dirrq   rp   r2   )r   rC   hiloisdirr   r   r   __repr__  s0    






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 )z-Return the per-file header as a bytes object.r   ru   r$   r   r!   r   r   r&   r    r   Nz<HHQQz'Filesize would require ZIP64 extensions    )rd   rj   ro   rp   rq   r3   ZIP64_LIMITrF   packcalcsizer   ZIP64_VERSIONre   r   r_   BZIP2_VERSIONr   LZMA_VERSIONrh   rW   _encodeFilenameFlagsstructFileHeaderstringFileHeaderri   r0   )r   zip64dtdosdatedostimero   rp   rq   r3   min_versionfmtrB   rj   headerr   r   r   
FileHeader  s^    $$
   
       zZipInfo.FileHeaderc                 C   sD   z| j d| jfW S  tk
r>   | j d| jdB f Y S X d S )Nasciiutf-8   )rB   encoderj   UnicodeEncodeErrorr   r   r   r   r     s    zZipInfo._encodeFilenameFlagsc                 C   s  | j }tj}t|dkr|d|d d \}}|d t|krPtd||f |dkrp|dkrv|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t||krtd|| | _|d7 }| jdkr6t||kr$td|| | _|d7 }| jdkrpt||krXtd| j}|| | _|d7 }||d d  }qd S )Nr    r,   z"Corrupt extra field %04x (size=%d)r      z<QQQ   r)   z<QQr   r   z<Qr   r   r   )l    r   z/Corrupt zip64 extra field. File size not found.r   z3Corrupt zip64 extra field. Compress size not found.z3Corrupt zip64 extra field. Header offset not found.)r3   rF   r/   r0   r   rq   rp   rn   )r   r3   r/   tpZlnZcountsidxoldr   r   r   _decodeExtra  sP    



zZipInfo._decodeExtraTstrict_timestampsc          	      C   s   t |tjrt|}t|}t|j}t|j	}|dd }|sZ|d dk rZd}n|sn|d dkrnd}|dkrz|}tj
tj
|d }|d tjtjfkr|dd }q|r|d	7 }| ||}|jd
@ d> |_|rd|_| jdO  _n|j|_|S )a_  Construct an appropriate ZipInfo for a file on the filesystem.

        filename should be the path to a file or directory on the filesystem.

        arcname is the name which it will have within the archive (by default,
        this will be the same as filename, but without a drive letter and with
        leading path separators removed).
        r   r"   ru   rt   ;  )r   r         ;   r   Nr   rv   r   r)   )
isinstancery   PathLikefspathr   S_ISDIRst_modetime	localtimest_mtimepathnormpath
splitdriverz   altseprm   rq   st_size)	clsrB   arcnamer   str   mtimerd   zinfor   r   r   	from_file  s0    



zZipInfo.from_filec                 C   s   | j d dkS )z2Return True if this archive member is a directory.rv   rB   r   r   r   r   r   %  s    zZipInfo.is_dir)rs   rt   )N)N)r   r   r   r   	__slots__r   r   r   r   r   classmethodr   r   r   r   r   r   r
   >  s   
+
.2%c                 C   s0   t dD ]"}| d@ r"| d? dA } q| dL } q| S )Nr   r   l    q[ )range)crcr7   r   r   r   _gen_crc/  s
    
r   c                    sl   dddt d kr&ttttda t fdd  fdd| D ]}| qLfd	d
}|S )NixV4igE#ixV4   c                    s   |d?  || A d@  A S )z(Compute the CRC32 primitive on one byte.r      r   )Zchr   )crctabler   r   crc32I  s    z_ZipDecrypter.<locals>.crc32c                    s<    | d@  d@ d d d@  d? d S )Nr   r   ir   r   r   )c)r   key0key1key2r   r   update_keysM  s    
z"_ZipDecrypter.<locals>.update_keysc                    sN   t  }|j}| D ]4} dB }|||dA  d? d@ N }| || qt|S )zDecrypt a bytes object.r   r   r   r   )	bytearrayr1   bytes)rV   rC   r1   r   k)r   r   r   r   	decrypterW  s    
z _ZipDecrypter.<locals>.decrypter)	_crctabler^   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)_compr   r   r   r   r   g  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   rF   r   r0   )r   Zpropsr   r   r   _initj  s
    
zLZMACompressor._initc                 C   s*   | j d kr|  | j | S | j |S r   )r   r   compress)r   rV   r   r   r   r   q  s    
zLZMACompressor.compressc                 C   s&   | j d kr|  | j   S | j  S r   )r   r   flushr   r   r   r   r   v  s    
zLZMACompressor.flushN)r   r   r   r   r   r   r   r   r   r   r   r   e  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eofr   r   r   r   r   ~  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   r0   rF   r/   r   r   r   r   r   
decompressr   )r   rV   ZpsizerC   r   r   r   r     s"    


zLZMADecompressor.decompressN)r   r   r   r   r   r   r   r   r   r   |  s   r   Z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 )N)
r   r   ZcompressobjZDEFLATEDZZ_DEFAULT_COMPRESSIONr   r   ZBZ2Compressorr   r   )re   compresslevelr   r   r   _get_compressor  s    
r   c                 C   sv   t |  | tkrd S | tkr&tdS | tkr6t S | tkrDt	 S t
| }|rdtd| |f ntd| f d S )Nr   zcompression type %d (%s)zcompression type %d)r   r   r   r   Zdecompressobjr   r   ZBZ2Decompressorr   r   r   r   r   )re   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 r   )_file_pos_close_lock_writingseekablerZ   )r   fileposcloselockZwritingr   r   r   r     s    z_SharedFile.__init__r   c              
   C   sN   | j > |  rtd| j|| | j | _| jW  5 Q R  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  r|   r  rD   rZ   r  )r   rT   whencer   r   r   rD     s    z_SharedFile.seekr   c              
   C   sX   | j H |  rtd| j| j | j|}| j | _|W  5 Q R  S Q R X d S )NyCan'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  r|   r  rD   r  r>   rZ   r   nrV   r   r   r   r>     s    z_SharedFile.readc                 C   s$   | j d k	r | j }d | _ | | d S r   )r  r  )r   fileobjr   r   r   r	     s    
z_SharedFile.closeN)r   )r   )r   r   r   r   rD   r>   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   )r<   rT   r   r<   r   r   r   r     s    z_Tellable.__init__c                 C   s   | j |}|  j|7  _|S r   )r<   writerT   )r   rV   r  r   r   r   r    s    z_Tellable.writec                 C   s   | j S r   )rT   r   r   r   r   rZ     s    z_Tellable.tellc                 C   s   | j   d S r   )r<   r   r   r   r   r   r     s    z_Tellable.flushc                 C   s   | j   d S r   )r<   r	  r   r   r   r   r	    s    z_Tellable.closeN)r   r   r   r   r  rZ   r   r	  r   r   r   r   r    s
   r  c                       s   e Zd ZdZdZdZdZd(ddZ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 )-
ZipExtFilezZFile-like object for reading an archive member.
       Is returned by ZipFile.open().
    i   @i   i   NFc                 C   s(  || _ || _|| _|j| _|j| _|j| _t	| j| _
d| _d| _d| _d | _|| _|j| _t|drz|j| _td| _nd | _d| _z4| r| | _|j| _|j| _| j| _d| _W n tk
r   Y nX d | _|r$|j d@ r|j!d? d@ }n|jd? d@ }| " }||kr$t#d	|j$ d S )
NFr-   r   ro   Tr   r   r   zBad password for file %r)%_fileobj_pwd_close_fileobjre   _compress_typerp   _compress_leftrq   _leftr   _decompressor_eof_readbuffer_offsetnewlinesmoderB   namer@   ro   _expected_crcr   _running_crc	_seekabler  rZ   _orig_compress_start_orig_compress_size_orig_file_size_orig_start_crcAttributeError
_decrypterrj   rr   _init_decrypterr   rc   )r   r  r   zipinfor   Zclose_fileobjZ
check_bytehr   r   r   r   )  sF    




zZipExtFile.__init__c                 C   s4   t | j| _| jd}|  jd8  _| |d S )Nr   r&   )r   r  r*  r  r>   r  )r   r   r   r   r   r+  \  s    zZipExtFile._init_decrypterc                 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 )N<%s.%sz name=%r mode=%rr   	 [closed]r   r   )r   r   r   closedr1   r!  r   r  r   r   r   r2   r   rC   r   r   r   r   g  s    


zZipExtFile.__repr__r   c                 C   sL   |dk r>| j d| jd }|dkr>| j | j| }|| _|S tj| |S )zrRead and return a line from the stream.

        If limit is specified, at most limit bytes will be read.
        r      
r   )r  rw   r  ioBufferedIOBasereadline)r   limitr6   liner   r   r   r5  u  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 )z6Returns buffered bytes without advancing the position.Nr   i   )r0   r  r  r>   )r   r  chunkr   r   r   peek  s    
zZipExtFile.peekc                 C   s   dS NTr   r   r   r   r   readable  s    zZipExtFile.readablec                 C   s   |dks|dk rH| j | jd }d| _ d| _| jsD|| | j7 }q,|S || j }|t| j k rz| j | j| }|| _|S |t| j  }| j | jd }d| _ d| _|dkr| js| |}|t|k r|| _ || _||d| 7 }q||7 }|t|8 }q|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   r-   r  r  r  _read1MAX_Nr0   )r   r  bufendrV   r   r   r   r>     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 rR| j | jd }d| _ d| _| jsN| | j}|r,||7 }qNq,|S || j }|t| j k r| j | j| }|| _|S |t| j  }| j | jd }d| _ d| _|dkr| js| |}|t|k r|| _ || _||d| 7 }q|r||7 }qq|S )z7Read up to n bytes with at most one read() system call.Nr   r-   r<  )r   r  r?  rV   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_tailr0   _read2r   r  r_   MIN_READ_SIZEr   r   r   r  rB  r  r   r   r   r=    s4    





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  r_   rE  minr  r>   r0   EOFErrorr*  r  r   r   r   rD  	  s    


zZipExtFile._read2c                    s&   z| jr| j  W 5 t    X d S r   )superr	  r  r  r   r   r   r   r	    s    zZipExtFile.closec                 C   s   | j S r   )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}nf|dk r
| j	
| j | j| _| j| _| j| _d| _d| _t| j| _d| _|}| jd k	r
|   |dkr6t| j|}| | ||8 }q
|  S )N!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$  r3  UnsupportedOperationrZ   r'  r|   r  r0   r  r  rD   r%  r(  r#  r&  r  r  r   r  r  r  r*  r+  rF  MAX_SEEK_READr>   )r   rT   r  Zcurr_posZnew_posZread_offsetZbuff_offsetZread_lenr   r   r   rD   #  sH    






zZipExtFile.seekc                 C   s0   | j std| j| j t| j | j }|S )NrJ  )r$  r3  rK  r'  r  r0   r  r  )r   Zfileposr   r   r   rZ   S  s    
zZipExtFile.tell)NF)r   )r   )r   )r   )r   r   r   r   r>  rE  rL  r   r+  r   r5  r9  r;  r>   rB  rC  r=  rD  r	  r  rD   rZ   __classcell__r   r   rI  r   r    s*     
3


!
%$
0r  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 r  )
_zinfo_zip64_zipfiler   re   rf   _compressor
_file_size_compress_size_crc)r   zfr   r   r   r   r   r   [  s    z_ZipWriteFile.__init__c                 C   s   | j jS r   )rQ  r<   r   r   r   r   r  e  s    z_ZipWriteFile._fileobjc                 C   s   dS r:  r   r   r   r   r   writablei  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.)r0  r|   r0   rS  r   rU  rR  r   rT  r  r  )r   rV   nbytesr   r   r   r  l  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 5 d| j_X d S )NFr   z<LLQQz<LLLLz+File size unexpectedly exceeded ZIP64 limitz1Compressed size unexpectedly exceeded ZIP64 limit)!r0  rQ  r  rH  r	  rR  r   rT  r0   r  r  rO  rp   rS  rU  ro   rq   rj   rP  rF   r   _DD_SIGNATURErZ   	start_dirr   r   rD   rn   r   filelistr1   
NameToInforB   )r   r?  r   rI  r   r   r	  x  sF    




 

z_ZipWriteFile.close)
r   r   r   r   propertyr  rW  r  r	  rM  r   r   rI  r   rN  Z  s   

rN  c                   @   s$  e Zd ZdZdZdZdeddfdd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dAd'd(ZdBd)d*Zed+d, Zd-d. Zd/d0 ZdCd1d2ZdDd3d4Zd5d6 Z d7d8 Z!d9d: Z"d;d< Z#dS )Er   ai   Class with methods to open, read, write, close, list zip files.

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

    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', exclusive create 'x',
          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.
    compresslevel: None (default for the given compression type) or an integer
                   specifying the level to pass to the compressor.
                   When using ZIP_STORED or ZIP_LZMA this keyword has no effect.
                   When using ZIP_DEFLATED integers 0 through 9 are accepted.
                   When using ZIP_BZIP2 integers 1 through 9 are accepted.

    NrTr   c          
   	   C   sP  |dkrt dt| || _d| _d| _i | _g | _|| _|| _|| _	d| _
d| _|| _t|tjrpt|}t|trd| _|| _ddd	d
dddd}|| }zt||| _W q tk
r   ||kr|| }Y q Y qX qqnd| _|| _t|dd| _d| _t | _d| _d| _z|dkr4|   n|dkrd| _z| j  | _!W n2 t"tfk
r   t#| j| _d| _!d| _Y n6X z| j$| j! W n  t"tfk
r   d| _Y nX nf|dkrz|   | 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 )z]Open the ZIP file with mode read 'r', write 'w', exclusive create 'x',
        or append 'a'.)r^  wxaz+ZipFile requires mode 'r', 'w', 'x', or 'a'Fr   Nr-   r?   w+bx+br+bwbZxb)r^  r_  r`  ra  rd  rb  rc  r   r!  Tr^  )r_  r`  ra  r   z"Mode must be 'r', 'w', 'x', or 'a')'r|   r   _allowZip64
_didModifydebugr\  r[  r   r   r   r   _comment_strict_timestampsr   ry   r   r   str_filePassedrB   r3  rA   r<   r:   getattr_fileRefCnt	threadingRLockr  r$  r  _RealGetContentsrZ   rZ  r)  r  rD   r   _fpclose)
r   r  r   r   
allowZip64r   r   ZmodeDictr   r<   r   r   r   r     s    

  





zZipFile.__init__c                 C   s   | S r   r   r   r   r   r   	__enter__  s    zZipFile.__enter__c                 C   s   |    d S r   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 )Nr.  z file=%rz filename=%rz mode=%rr/  r   r   )	r   r   r   r<   rl  r1   rB   r   r2   r1  r   r   r   r   "  s    



zZipFile.__repr__c                 C   s  | j }z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}||k r|t}	t|	tkr
tdtt|	}	|	t tkr,td| jdkr@t|	 ||	t }
|	d }|d	@ rl|
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| qd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!   r   r   cp437r   zzip file version %.1fr%   r(   r+   r$   ru   r   r&   r   totalN)9r<   r9   r:   r   rh  printrQ   rR   _ECD_COMMENTri  _ECD_LOCATIONrL   rK   rI   rE   rZ  rD   r>   r3  BytesIOsizeCentralDirr0   rF   r/   structCentralDir_CD_SIGNATUREstringCentralDir_CD_FILENAME_LENGTHdecoder
   _CD_EXTRA_FIELD_LENGTHr3   _CD_COMMENT_LENGTHrb   _CD_LOCAL_HEADER_OFFSETrn   rW   rg   rh   ri   rj   re   ro   rp   rq   MAX_EXTRACT_VERSIONr   rk   rl   rm   rr   rd   r   r[  r1   r\  rB   )r   r<   rU   Zsize_cdZ	offset_cdconcatZinferredrV   r{  centdirrB   flagsr`  tdr   r   r   rq  0  s    











      
 

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S r   r   ).0rV   r   r   r   
<listcomp>  s     z$ZipFile.namelist.<locals>.<listcomp>r[  r   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  r   r   r   r   infolist  s    zZipFile.infolistc                 C   sL   t dd |d | jD ]0}d|jdd  }t d|j||jf |d qdS )z+Print a table of contents for the zip file.z%-46s %19s %12s)z	File NamezModified    ZSizer  z%d-%02d-%02d %02d:%02d:%02dNr"   z%-46s %s %12d)r|  r[  rd   rB   rq   )r   r  r   dater   r   r   printdir  s    
zZipFile.printdirc              
   C   s^   d}| j D ]N}z*| |jd}||r,q W 5 Q R X W q
 tk
rV   |j Y   S X q
dS )z%Read all the files and check the CRC.i   r^  N)r[  rA   rB   r>   r   )r   Z
chunk_sizer   fr   r   r   testzip  s    

zZipFile.testzipc                 C   s$   | j |}|dkr td| |S )z,Return the instance of ZipInfo given 'name'.Nz(There is no item named %r in the archive)r\  r   KeyError)r   r!  infor   r   r   getinfo  s    zZipFile.getinfoc                 C   s6   |r t |ts tdt|j |r,|| _nd| _dS )z)Set default password for encrypted files.pwd: expected bytes, got %sN)r   r   	TypeErrorrv  r   r   )r   r   r   r   r   setpassword  s
    zZipFile.setpasswordc                 C   s   | j S )z.The comment text associated with the ZIP file.)ri  r   r   r   r   rb     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   r  rv  r   r0   ZIP_MAX_COMMENTwarningswarnri  rg  )r   rb   r  r   r   r   rb     s    
c              
   C   s.   |  |d|}| W  5 Q R  S Q R X dS )zReturn file bytes for name.r^  NrA   r>   )r   r!  r   r<   r   r   r   r>     s    zZipFile.readFforce_zip64c                   s  |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}z|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|t! d@ r|"d}	n
|"d}	|	|j#krtd|j#|f |jd	@ }
|
r|s҈ j$}|st%d| nd}t&||||dW S    |'   Y nX dS )au  Return file-like object for 'name'.

        name is a string for the file name within the ZIP file, or a ZipInfo
        object.

        mode should be 'r' to read a file already in the ZIP file, or 'w' to
        write to a file newly added to the archive.

        pwd is the password to decrypt files (only used for reading).

        When writing, if the file size is not known in advance but may exceed
        2 GiB, pass force_zip64 to use the ZIP64 format, which can handle large
        files.  If the size is known in advance, it is best to pass a ZipInfo
        instance for name, with zinfo.file_size set.
        >   r_  r^  zopen() requires mode "r" or "w"r  r_  z'pwd is only supported for reading filesz2Attempt to use ZIP archive that was already closedr  r  r   c                      s    j S r   )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)r   r   rz  z/File name in directory %r and header %r differ.z6File %r is encrypted, password required for extractionNT)(r|   r   r   r  rv  r   r<   r
   r   re   r   rf   r  _open_to_writer  rn  r   rn   rr  r  r>   sizeFileHeaderr0   r   rF   r/   r   _FH_SIGNATUREr   _FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTHrj   r   _FH_GENERAL_PURPOSE_FLAG_BITSr  rc   r   r   r  r	  )r   r!  r   r   r  r   Zzef_fileZfheaderfnameZ	fname_strZis_encryptedr   r   r   rA     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.rq   r   r   r     g?T)rf  r|   r  r@   rq   rp   ro   rj   re   r   r$  rm   r   r<   rD   rZ  rZ   rn   _writecheckrg  r  r   rN  )r   r   r  r   r   r   r   r  (  s8    



zZipFile._open_to_writec                 C   s*   |dkrt  }n
t |}| |||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)ry   getcwdr   _extract_member)r   memberr   r   r   r   r   extractS  s    

zZipFile.extractc                 C   sH   |dkr|   }|dkr"t }n
t|}|D ]}| ||| q0d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)r  ry   r  r   r  )r   r   membersr   r,  r   r   r   
extractall`  s    

zZipFile.extractallc                 C   s^   | j }|s(d}t|dt| }|| _ ||}dd ||D }|dd |D }|S )z;Replace bad characters and remove trailing dots from parts.z:<>|"?*_c                 s   s   | ]}| 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 r   r   r  r   r   r   r  }  s      )!_windows_illegal_name_trans_tablerk  	maketransr0   	translatesplitr2   )r   r   pathseptableZillegalr   r   r   _sanitize_windows_nameq  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 5 Q R X W 5 Q R X |S )	zbExtract the ZipInfo object 'member' to a physical
           file on the path targetpath.
        rv   r   r   c                 3   s   | ]}| kr|V  qd S r   r   r  Zinvalid_path_partsr   r   r    s    z*ZipFile._extract_member.<locals>.<genexpr>\)r   re  )r   r
   r  rB   r{   ry   r   rz   r   r   curdirpardirr2   r  r  r   dirnameexistsmakedirsr   r   mkdirrA   shutilcopyfileobj)r   r  Z
targetpathr   r   Z	upperdirssourcetargetr   r  r   r    s2    

&


 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 )z6Check for errors before writing a file to the archive.r   NzDuplicate name: %rr   r  r_  r`  ra  z&write() requires mode 'w', 'x', or 'a'z4Attempt to write ZIP archive that was already closedFiles countZFilesizezZipfile size would require ZIP64 extensions)rB   r\  r  r  r   r|   r<   r   re   rf  r0   r[  ZIP_FILECOUNT_LIMITrq   r   rn   r   )r   r   r  requires_zip64r   r   r   r    s,    



zZipFile._writecheckc              
   C   sP  | j std| jrtdtj||| jd}| rDd|_d|_n0|dk	rT||_	n| j
|_	|dk	rl||_n| j|_| r| j | jr| j | j | j  |_|j	tkr| jdO  _| | d| _| j| || j|j< | j |d | j  | _W 5 Q R X n<t|d	,}| |d
}t||d W 5 Q R X W 5 Q R X dS )zLPut the bytes from filename into the archive under the name
        arcname.7Attempt to write to ZIP archive that was already closedz>Can't write to ZIP archive while an open writing handle existsr   r   Nr   TFr?   r_  i    ) r<   r|   r  r
   r   rj  r   rp   ro   re   r   rf   r   r  r$  rD   rZ  rZ   rn   r   rj   r  rg  r[  r1   r\  rB   r  r   rA   r  r  )r   rB   r   re   r   r   srcdestr   r   r   r    sF    


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 5 Q R X W 5 Q R X 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.r   Nr"   )rB   rd   r   rv   i  Ar)   r  r  z?Can't write to ZIP archive while an open writing handle exists.r_  )r   )r   rk  r   r
   r   r   r   re   r   rf   rB   rm   r<   r|   r  r0   rq   r  rA   r  )r   Zzinfo_or_arcnamerV   re   r   r   r  r   r   r   writestr  s:    



zZipFile.writestrc                 C   s   |    dS )z2Call the "close()" method in case the user forgot.Nru  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 5 Q R X W 5 | j }d| _ | | X dS )zOClose the file, and for mode 'w', 'x' and 'a' write the ending
        records.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  r|   rr  r   rg  r  r$  rD   rZ  _write_end_recordr  r   r   r   r	    s    
zZipFile.closec                 C   s  | j D ]D}|j}|d d d> |d d> B |d B }|d d> |d	 d> B |d d B }g }|jtksr|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}zZ| \}}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| 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 | j3dkrt| j$4  | j$5  d S )Nr   ru   r$   r   r!   r   r   r&   r    r   )r   r,   Qr   r  r  zCentral directory offsetzCentral directory sizer  ,   r   r   ra  )6r[  rd   rq   r   rp   r1   rn   r3   r8   rF   r   r0   r   re   r   r_   r   r   r   rh   rW   r   r  r  rg   ri   ro   rb   rl   rm   DeprecationWarningr|  rj   rB   r}   stderrr<   r  rZ   rZ  r  rf  r   rJ   rK   rG   rH   rF  r]   r\   ri  r   truncater   )r   r   r   r   r   r3   rq   rp   rn   Z
extra_datar   rh   rW   rB   rj   r  Zpos2ZcentDirCountZcentDirSizeZcentDirOffsetr  Zzip64endrecZzip64locrecrU   r   r   r   r  5  s   $$


 

                       





           


     
zZipFile._write_end_recordc                 C   s4   | j dkst|  j d8  _ | j s0| js0|  d S )Nr   r   )rn  AssertionErrorrl  r	  r  r   r   r   rr    s    zZipFile._fpclose)N)N)r^  N)F)NN)NNN)NNN)NN)$r   r   r   r   r<   r  r   r   rt  ry  r   rq  r  r  r  r  r  r  r]  rb   setterr>   rA   r  r  r  r   r  r  r  r  r  r  r	  r  rr  r   r   r   r   r     sT   ZN
			


b
+


*    
2   
)hc                   @   s4   e Zd ZdZdeddfddZ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 S )N)r   r   rs  )r   r   	_optimize)r   r  r   r   rs  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|rht j|d}t j|r|rd||f }n|}| jrtd|d| | 	|d	d
 |\}}	| jrtd|	 | 
||	 tt |}
|
d |
D ]}t j||}t j|\}}t j|rRt jt j|dr| j|||d q|dkr|r~||s~| jrtd|  q| 	|d	d
 |\}}	| jrtd|	 | 
||	 qn| jrtd| tt |D ]}t j||}t j|\}}|dkr|r,||s,| jrtd|  q| 	|d	d
 |\}}	| jrVtd|	 | 
||	 qnP|d
d dkrtd| 	|d	d
 |\}}	| jrtd|	 | 
||	 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.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.
        r   r  z%s %r skipped by filterfuncNz__init__.py%s/%szAdding package inasr   ZAdding)
filterfunc.pyzfile %r skipped by filterfunczAdding files from directoryz.Files added with writepy() must end with ".py"zAdding file)ry   r   rh  r   r   r|  r  r2   isfile_get_codenamer  sortedlistdirremovesplitextwritepyr   )r   pathnamebasenamer  Zlabeldirr!  Zinitnamer  r   ZdirlistrB   r   rootZextr   r   r   r    s    







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 )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              
      sf   dd l } jrtd|  z|j| d|d W n4 |jk
r` } zt|j W Y dS d }~X Y nX dS )Nr   Z	CompilingT)doraiser  F)
py_compilerh  r|  compilePyCompileErrormsg)r  r  r  errr   r   r   _compile  s    

z)PyZipFile._get_codename.<locals>._compiler  z.pycr   )optimizationr   r   r   z"invalid value for 'optimize': {!r})r  r  )r   )	importlibutilcache_from_sourcer  ry   r   r  r   r   r}   r  r  formatr|   r  )r   r  r  r  Zfile_pyZfile_pycZpycache_opt0Zpycache_opt1Zpycache_opt2r   r  r  Zarchivenamer   r   r   r    sj    


zPyZipFile._get_codename)r   N)r   r   r   r   r   r   r  r  r   r   r   r   r     s    

Rc                 C   s   t t| ddS )a2  
    Given a path with elements separated by
    posixpath.sep, generate all parents of that path.

    >>> list(_parents('b/d'))
    ['b']
    >>> list(_parents('/b/d/'))
    ['/b']
    >>> list(_parents('b/d/f/'))
    ['b/d', 'b']
    >>> list(_parents('b'))
    []
    >>> list(_parents(''))
    []
    r   N)	itertoolsislice	_ancestry)r   r   r   r   _parentsR  s    r  c                 c   s4   |  tj} | r0| tjkr0| V  t| \} }qdS )aR  
    Given a path with elements separated by
    posixpath.sep, generate all elements of that path

    >>> list(_ancestry('b/d'))
    ['b/d', 'b']
    >>> list(_ancestry('/b/d/'))
    ['/b/d', '/b']
    >>> list(_ancestry('b/d/f/'))
    ['b/d/f', 'b/d', 'b']
    >>> list(_ancestry('b'))
    ['b']
    >>> list(_ancestry(''))
    []
    N)r  	posixpathrz   r  )r   tailr   r   r   r  e  s    r  c                 C   s   t t|j| S )zZ
    Return items in minuend not in subtrahend, retaining order
    with O(1) lookup.
    )r  filterfalseset__contains__)ZminuendZ
subtrahendr   r   r   _difference  s    r
  c                       sH   e Zd ZdZedd Z fddZdd Zdd	 Ze	d
d Z
  ZS )CompleteDirszk
    A ZipFile subclass that ensures that implied directories
    are always included in the namelist.
    c                 C   s.   t jtt| }dd |D }tt|| S )Nc                 s   s   | ]}|t j V  qd S r   )r  rz   )r  r   r   r   r   r    s     z-CompleteDirs._implied_dirs.<locals>.<genexpr>)r  chainfrom_iterabler   r  _deduper
  )namesparentsZas_dirsr   r   r   _implied_dirs  s    zCompleteDirs._implied_dirsc                    s    t t|  }|t| | S r   )rH  r  r  r^   r  )r   r  rI  r   r   r    s    zCompleteDirs.namelistc                 C   s   t |  S r   )r  r  r   r   r   r   	_name_set  s    zCompleteDirs._name_setc                 C   s,   |   }|d }||ko||k}|r(|S |S )zx
        If the name represents a directory, return that name
        as a directory (with the trailing slash).
        rv   )r  )r   r!  r  r  Z	dir_matchr   r   r   resolve_dir  s    zCompleteDirs.resolve_dirc                 C   sN   t |tr|S t |ts | |S d|jkr.t} | | }t|t| |S )zl
        Given a source (filename or zipfile), return an
        appropriate CompleteDirs subclass.
        r^  )r   r  r   r   __new__varsupdate)r   r  resr   r   r   make  s    



zCompleteDirs.make)r   r   r   r   staticmethodr  r  r  r  r   r  rM  r   r   rI  r   r    s   

r  c                       s,   e Zd ZdZ fddZ fddZ  ZS )
FastLookupzV
    ZipFile subclass to ensure implicit
    dirs exist and are resolved rapidly.
    c              
      s:   t t | jW  5 Q R  S Q R X tt|  | _| jS r   )
contextlibsuppressr)  Z_FastLookup__namesrH  r  r  r   rI  r   r   r    s    zFastLookup.namelistc              
      s:   t t | jW  5 Q R  S Q R X tt|  | _| jS r   )r  r  r)  Z_FastLookup__lookuprH  r  r  r   rI  r   r   r    s    zFastLookup._name_set)r   r   r   r   r  r  rM  r   r   rI  r   r    s   r  c                   @   s   e Zd ZdZdZd#ddZedd Zedd	 Zd
d Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd ZeZed d! Zd"S )$r   u  
    A pathlib-compatible interface for zip files.

    Consider a zip file with this structure::

        .
        ├── a.txt
        └── b
            ├── c.txt
            └── d
                └── e.txt

    >>> data = io.BytesIO()
    >>> zf = ZipFile(data, 'w')
    >>> zf.writestr('a.txt', 'content of a')
    >>> zf.writestr('b/c.txt', 'content of c')
    >>> zf.writestr('b/d/e.txt', 'content of e')
    >>> zf.filename = 'abcde.zip'

    Path accepts the zipfile object itself or a filename

    >>> root = Path(zf)

    From there, several path operations are available.

    Directory iteration (including the zip file itself):

    >>> a, b = root.iterdir()
    >>> a
    Path('abcde.zip', 'a.txt')
    >>> b
    Path('abcde.zip', 'b/')

    name property:

    >>> b.name
    'b'

    join with divide operator:

    >>> c = b / 'c.txt'
    >>> c
    Path('abcde.zip', 'b/c.txt')
    >>> c.name
    'c.txt'

    Read text:

    >>> c.read_text()
    'content of c'

    existence:

    >>> c.exists()
    True
    >>> (b / 'missing.txt').exists()
    False

    Coercion to string:

    >>> str(c)
    'abcde.zip/b/c.txt'
    z>{self.__class__.__name__}({self.root.filename!r}, {self.at!r})r   c                 C   s   t || _|| _d S r   )r  r  r  at)r   r  r  r   r   r   r   	  s    zPath.__init__c                 C   s   t | jj| jS r   )	functoolspartialr  rA   r  r   r   r   r   rA   	  s    z	Path.openc                 C   s   t | jdS Nrv   )r  r  r  r  r   r   r   r   r!  	  s    z	Path.namec              
   O   s6   |   $}tj|f|| W  5 Q R  S Q R X d S r   )rA   r3  TextIOWrapperr>   )r   argskwargsstrmr   r   r   	read_text	  s    
zPath.read_textc              
   C   s(   |   }| W  5 Q R  S Q R X d S r   r  )r   r$  r   r   r   
read_bytes	  s    
zPath.read_bytesc                 C   s   t |jd| jdkS r   )r  r  r  r  )r   r   r   r   r   	_is_child"	  s    zPath._is_childc                 C   s   t | j|S r   )r   r  )r   r  r   r   r   _next%	  s    z
Path._nextc                 C   s   | j  p| j dS r   )r  endswithr   r   r   r   r   (	  s    zPath.is_dirc                 C   s
   |    S r   )r   r   r   r   r   is_file+	  s    zPath.is_filec                 C   s   | j | j kS r   )r  r  r  r   r   r   r   r  .	  s    zPath.existsc                 C   s.   |   stdt| j| j }t| j|S )NzCan't listdir a file)r   r|   r   r(  r  r  filterr'  )r   Zsubsr   r   r   iterdir1	  s    zPath.iterdirc                 C   s   t | jj| jS r   )r  r2   r  rB   r  r   r   r   r   __str__7	  s    zPath.__str__c                 C   s   | j j| dS )Nr   )_Path__reprr   r   r   r   r   r   :	  s    zPath.__repr__c                 C   s    t | j|}| | j|S r   )r  r2   r  r(  r  r  )r   addnextr   r   r   joinpath=	  s    zPath.joinpathc                 C   s(   t | jd}|r|d7 }| |S r   )r  r  r  r  r(  )r   Z	parent_atr   r   r   parentC	  s    zPath.parentN)r   )r   r   r   r   r.  r   r]  rA   r!  r%  r&  r'  r(  r   r*  r  r,  r-  r   r1  __truediv__r2  r   r   r   r   r     s*   @


c              	      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 5 Q R X |rtd	| td n| j
d k	r| j
}t|d}|  W 5 Q R X n| jd k	r,| j\}}t|d}|| W 5 Q R X n| jd k	r| jd}	| j}
 fdd t|	d\}|
D ]P}tj|}|stjtj|}|dtjtjfkrd} ||| qfW 5 Q R X d S ) Nr   z3A simple command-line interface for zipfile module.)descriptionT)Zrequiredz-lz--list	<zipfile>zShow listing of a zipfile)metavarhelpz-ez	--extractr   )r5  z<output_dir>zExtract zipfile into target dir)nargsr6  r7  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                    sp   t j|r| ||t nPt j|rl|r8| || tt |D ]$} | t j||t j|| qFd S r   )	ry   r   r  r  r   r   r  r  r2   )rV  r   zippathZnmaddToZipr   r   r<  s	  s     zmain.<locals>.addToZipr_  r   )argparseArgumentParserZadd_mutually_exclusive_groupadd_argument
parse_argsZtestr   r  r|  r   r^   r  r  r  Zcreatepopry   r   r  r  r  r  )r"  r=  r4  parsergroupr  rV  Zbadfiler  Zzip_namefilesr   r:  r   r;  r   mainK	  s\    








rE  __main__)N)N)r   Zbinasciir  importlib.utilr  r3  r  ry   r  r  r   rF   r}   ro  r   r  r   r   ImportErrorr   r   __all__	Exceptionr   r   r   r   r   r  r  r   r   r   r   r   r   r   r   r  r]   r\   r   r[   rL   rM   rN   rO   rP   rQ   rR   ra   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   r  r  Z_FH_EXTRACT_VERSIONZ_FH_EXTRACT_SYSTEMr  Z_FH_COMPRESSION_METHODZ_FH_LAST_MOD_TIMEZ_FH_LAST_MOD_DATEZ_FH_CRCZ_FH_COMPRESSED_SIZEZ_FH_UNCOMPRESSED_SIZEr  r  rG   rH   rE   rJ   rK   rI   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_CENTDIRrY  ZStructr.   r8   r=   r	   rX   r9   objectr
   r   r   r   r   r   r   r   r   r   r   r  r4  r  rN  r   r   r  r  dictfromkeysr  r
  r  r  r   rE  r   r   r   r   r   <module>   sl  


       






+= q&
&  AN       /2 
=
