B N��j�A�@s�dZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z yddl Z e j Z Wnek r�dZ e j Z YnXy ddlZWnek r�dZYnXy ddlZWnek r�dZYnXddddddd d d d d dg ZGdd�de�ZGdd�de�ZeZZdZdZdZdZdZdZdZdZdZdZ dZ!dZ"dZ#dZ$e �%e#�Z&dZ'dZ(dZ)dZ*dZ+d Z,d!Z-d"Z.dZ/d#Z0d$Z1d%Z2e �%e1�Z3dZ4dZ5dZ6dZ7dZ8d Z9d!Z:d"Z;dZ<d#Z=d&Z>d'Z?dZ@d(ZAdZBd)ZCd*ZDd+ZEd,ZFd-ZGd.ZHe �%eG�ZIdZJdZKdZLdZMdZNd ZOd!ZPd"ZQdZRd#ZSd&ZTd'ZUd/ZVd0ZWe �%eV�ZXd1ZYd2ZZe �%eY�Z[dZ\dZ]dZ^dZ_dZ`d Zad!Zbd"ZcdZdd#Zed3Zfe �gd4�Zhd5d6�Zid7d8�Zjd9d �Zkd:d;�Zld<d=�ZmGd>d �d en�Zodapd?d@�ZqdAdB�ZrGdCdD�dD�ZsGdEdF�dF�ZtdGdHdIdIdIdIdJdKdLdMdJdNdOdPdQdRdSdT�ZudUdV�ZvdhdWdX�ZwdYdZ�ZxGd[d\�d\�ZyGd]d^�d^�ZzGd_d`�d`ej{�Z|Gdadb�dbej{�Z}Gdcd �d �Z~Gddd �d e~�Zdidedf�Z�e�dgk�r�e��dS)jzP Read and write ZIP files. XXX references to utf-8 need further investigation. �N� BadZipFile� BadZipfile�error� ZIP_STORED� ZIP_DEFLATED� ZIP_BZIP2�ZIP_LZMA� is_zipfile�ZipInfo�ZipFile� PyZipFile� LargeZipFilec@s eZdZdS)rN)�__name__� __module__� __qualname__�rr�,/opt/alt/python37/lib64/python3.7/zipfile.pyr&sc@seZdZdZdS)r zu Raised when writing a zipfile, the zipfile requires ZIP64 extensions and those extensions are disabled. N)rrr�__doc__rrrrr *si���i���� ���-�.�?s<4s4H2LHsPK�������� z<4s4B4HL2L5H2LsPK� � � ����z <4s2B4HL2L2HsPKz<4sLQLsPKz <4sQ2H2L4QsPKiPKz<HHc Cs�tj}d}g}d}}xf|dt|�kr|||||d��\}}|d|} ||krv||krn|�|||��| }d}| }qW|s�|Sd�|�S)NFrrT�)�_EXTRA_FIELD_STRUCT�unpack�len�append�join) �extraZxidsr,Zmodified�buffer�start�iZxidZxlen�jrrr� _strip_extra�s  r5cCs*yt|�rdSWntk r$YnXdS)NTF)� _EndRecData�OSError)�fprrr�_check_zipfile�s r9c CsZd}y8t|d�rt|d�}nt|d��}t|�}WdQRXWnttfk rTYnX|S)z�Quickly 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�read)r8�rbN)�hasattrr9�openr7r)�filename�resultr8rrrr �s   c Cs�|t8}|dkr|S|�|�|�t�}t|�tkr<td��t�t|�\}}}}|tkr\|S|dksl|dkrtt d��|t 8}||kr�t d��|�|�||}|�t �}t|�t kr�td��|� t �s�||kr�|�|�d}|�t �}t|�t kr�td��|� t ��st d��t�t |�\ }} } } } } }}}}|||k�sN| dt |k�rVt d��||t<| |t<| |t<||t<||t<||t<||t<|||t<|S) zM Read the ZIP64 end-of-archive records and use that to update endrec rzUnknown I/O errorrz3zipfiles that span multiple disks are not supportedz.Corrupt zip64 end of central directory locatorz/Zip64 end of central directory record not foundrz-Corrupt zip64 end of central directory record)�sizeEndCentDir64Locator�seekr:r-r7�structr,�structEndArchive64Locator�stringEndArchive64Locatorr�sizeEndCentDir64� startswith�stringEndArchive64�structEndArchive64�_ECD_SIGNATURE�_ECD_DISK_NUMBER�_ECD_DISK_START�_ECD_ENTRIES_THIS_DISK�_ECD_ENTRIES_TOTAL� _ECD_SIZE� _ECD_OFFSET� _ECD_LOCATION)�fpin�offset�endrec�dataZsigZdisknoZreloffZdisksZextraszZsz�create_versionZ read_versionZdisk_numZdisk_dirZdircountZ dircount2ZdirsizeZ diroffsetrrr� _EndRecData64�sR            rVc Csd|�dd�|��}y|�t d�Wntk r:dSX|��}t|�tkr�|dd�tkr�|dd�dkr�t�t |�}t |�}|� d�|� |t�t ||t|�St |dtd�}|�|d�|��}|�t�}|dk�r`|||t�}t|�tk�rdSt t�t |��}|t}||t|t|�}|� |�|� ||�t ||||�SdS) z�Return 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.rrNr���sr*i)rA�tell�sizeEndCentDirr7r:r-�stringEndArchiverBr,�structEndArchive�listr.rV�max�rfind�_ECD_COMMENT_SIZE) rQZfilesizerTrSZmaxCommentStartr2ZrecDataZ commentSize�commentrrrr6 s<        r6c@sVeZdZdZdZddd�Zdd�Zdd d �Zd d �Zdd�Z e ddd��Z dd�Z d S)r z>Class with attributes describing each file in the ZIP archive.)� orig_filenamer>� date_time� compress_type�_compresslevelr`r0� create_systemrU�extract_version�reserved� flag_bits�volume� internal_attr� external_attr� header_offset�CRC� compress_size� file_size� _raw_time� _end_offset�NoName�i�rrrrrcCs�||_|�td��}|dkr(|d|�}tjdkrJtj|krJ|�tjd�}||_||_|ddkrjtd��t |_ d|_ d|_ d|_ tjdkr�d|_nd|_t|_t|_d|_d|_d|_d|_d|_d|_dS)Nr�/i�z+ZIP does not support timestamps before 1980r*Zwin32r)ra�find�chr�os�sep�replacer>rb� ValueErrorrrcrdr`r0�sys�platformre�DEFAULT_VERSIONrUrfrgrhrirjrkrq)�selfr>rbZ null_byterrr�__init__ds2   zZipInfo.__init__cCs�d|jj|jfg}|jtkr8|�dt�|j|j��|jd?}|jd@}|rd|�dt � |��|rv|�d|�|� �}|r�|j r�|�d|j �|r�|j r�|jtks�|j |j kr�|�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__rr>rcrr.�compressor_names�getrk�stat�filemode�is_dirrornr/)r~r?�hi�lo�isdirrrr�__repr__�s(         zZipInfo.__repr__NcCs||j}|ddd>|dd>B|dB}|dd>|d d>B|ddB}|jd @rfd}}}n|j}|j}|j}|j}d} |d kr�|tkp�|tk}|r�d } |t�| dt� | �d ||�}|tks�|tkr�|s�t d ��d}d}t } |j t k�rtt| �} n|j tk�rtt| �} t| |j�|_t| |j�|_|��\} } t�tt|j|j| |j |||||t| �t|�� } | | |S)z-Return the per-file header as a bytes object.ri�r"rrrrr$rrNz<HHQQz'Filesize would require ZIP64 extensionsl��)rbrhrmrnror0� ZIP64_LIMITrB�pack�calcsizer � ZIP64_VERSIONrcrr]� BZIP2_VERSIONr� LZMA_VERSIONrfrU�_encodeFilenameFlags�structFileHeader�stringFileHeaderrgr-)r~�zip64�dt�dosdate�dostimermrnror0� min_version�fmtr>rh�headerrrr� FileHeader�sF$$        zZipInfo.FileHeadercCs>y|j�d�|jfStk r8|j�d�|jdBfSXdS)N�asciizutf-8i)r>�encoderh�UnicodeEncodeError)r~rrrr��szZipInfo._encodeFilenameFlagscCs�|j}tj}�xxt|�dk�r�|d|dd��\}}|dt|�krTtd||f��|dk�rt|dkrz|d|dd��}nV|dkr�|d |dd ��}n:|d kr�|d |dd ��}n|dkr�d}ntd||f��d}|jdk�rt|�|kr�td��|||_|d7}|jdk�r:t|�|k�r(td��|||_|d7}|jdk�rtt|�|k�r\td��|j}|||_|d7}||dd�}qWdS)Nrz<HHz"Corrupt extra field %04x (size=%d)r�z<QQQ�r'z<QQrrz<Qrrr)l����l��z/Corrupt zip64 extra field. File size not found.l��z3Corrupt zip64 extra field. Compress size not found.z3Corrupt zip64 extra field. Header offset not found.)r0rBr,r-rrornrl)r~r0r,�tpZlnZcounts�idx�oldrrr� _decodeExtra�sJ        zZipInfo._decodeExtracCs�t|tj�rt�|�}t�|�}t�|j�}t�|j �}|dd�}|dkrP|}tj � tj � |�d�}x$|dtj tjfkr�|dd�}qjW|r�|d7}|||�}|jd@d>|_|r�d|_|jdO_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). rr Nrrti��r')� isinstancerw�PathLike�fspathr��S_ISDIR�st_mode�time� localtime�st_mtime�path�normpath� splitdriverx�altseprkro�st_size)�clsr>�arcname�str��mtimerb�zinforrr� from_file s(       zZipInfo.from_filecCs|jddkS)z2Return True if this archive member is a directory.���rt)r>)r~rrrr�.szZipInfo.is_dir)rrrs)N)N) rrrr� __slots__rr�r�r�r�� classmethodr�r�rrrrr Is , .2 !cCs4x.td�D]"}|d@r$|d?dA}q |dL}q W|S)Nrrl q[)�range)�crcr4rrr�_gen_crc8s  r�cspd�d�d�tdkr&ttttd���at��fdd������fdd��x|D] }�|�qNW��fd d �}|S) NixV4i�gE#i�xV4�cs|d?�||Ad@AS)z(Compute the CRC32 primitive on one byte.r�r)Zchr�)�crctablerr�crc32Rsz_ZipDecrypter.<locals>.crc32cs<�|�����d@d@��ddd@���d?���dS)Nr�l��i�rr�r)�c)r��key0�key1�key2rr� update_keysVs z"_ZipDecrypter.<locals>.update_keyscsRt�}|j}x<|D]4}�dB}|||dAd?d@N}�|�||�qWt|�S)zDecrypt a bytes object.rrrr�)� bytearrayr.�bytes)rTr?r.r��k)r�r�rr� decrypter`s  z _ZipDecrypter.<locals>.decrypter)� _crctabler\�mapr�r�)�pwd�pr�r)r�r�r�r�r�r�r� _ZipDecrypterHs    r�c@s,eZdZdd�Zdd�Zdd�Zdd�Zd S) �LZMACompressorcCs d|_dS)N)�_comp)r~rrrrpszLZMACompressor.__init__cCsFt�dtji�}tjtjt�tj|�gd�|_t�dddt |��|S)N�id)�filtersz<BBHr"r) �lzmaZ_encode_filter_properties� FILTER_LZMA1r�� FORMAT_RAW�_decode_filter_propertiesr�rBr�r-)r~Zpropsrrr�_initsszLZMACompressor._initcCs*|jdkr|��|j�|�S|j�|�S)N)r�r��compress)r~rTrrrr�zs zLZMACompressor.compresscCs&|jdkr|��|j��S|j��S)N)r�r��flush)r~rrrr�s zLZMACompressor.flushN)rrrrr�r�r�rrrrr�nsr�c@seZdZdd�Zdd�ZdS)�LZMADecompressorcCsd|_d|_d|_dS)Nr*F)�_decomp� _unconsumed�eof)r~rrrr�szLZMADecompressor.__init__c Cs�|jdkr�|j|7_t|j�dkr*dSt�d|jdd��\}t|j�d|krXdStjtjt�tj |jdd|��gd�|_|jd|d�}|`|j� |�}|jj |_ |S)Nrr*z<Hr)r�) r�r�r-rBr,r�r�r�r�r�� decompressr�)r~rTZpsizer?rrrr��s   zLZMADecompressor.decompressN)rrrrr�rrrrr��sr�ZstoreZshrink�reduceZimplode�tokenizeZdeflateZ deflate64Zbzip2r�ZterseZlz77ZwavpackZppmd)rrrrrrr r!rr"r#rrr)��a�bcCsX|tkr nJ|tkr tsTtd��n4|tkr6tsTtd��n|tkrLtsTtd��ntd��dS)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) rr�zlib� RuntimeErrorr�bz2rr��NotImplementedError)� compressionrrr�_check_compression�sr�cCsj|tkr2|dk r t�|tjd�St�tjtjd�S|tkrT|dk rLt�|�St��S|tkrbt �SdSdS)Ni�) rr�Z compressobjZDEFLATEDZZ_DEFAULT_COMPRESSIONrr�Z BZ2Compressorrr�)rc� compresslevelrrr�_get_compressor�s r�cCsn|tkr dS|tkrt�d�S|tkr.t��S|tkr<t�St � |�}|r\t d||f��nt d|f��dS)Ni�zcompression type %d (%s)zcompression type %d) rrr�Z decompressobjrr�ZBZ2Decompressorrr�r�r�r�)rcZdescrrrr�_get_decompressor�s  r�c@s0eZdZdd�Zd dd�Zd dd�Zd d �Zd S)� _SharedFilecCs2||_||_||_||_||_|j|_|j|_dS)N)�_file�_pos�_close�_lock�_writing�seekablerX)r~�file�pos�close�lockZwritingrrrr�sz_SharedFile.__init__rc CsB|j�2|��rtd��|j�||�|j��|_|jSQRXdS)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�rzr�rArXr�)r~rR�whencerrrrA�s  z_SharedFile.seekr�c CsL|j�<|��rtd��|j�|j�|j�|�}|j��|_|SQRXdS)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�rzr�rAr�r:rX)r~�nrTrrrr:�s  z_SharedFile.readcCs$|jdk r |j}d|_|�|�dS)N)r�r�)r~�fileobjrrrr�s z_SharedFile.closeN)r)r�)rrrrrAr:r�rrrrr��s r�c@s4eZdZdd�Zdd�Zdd�Zdd�Zd d �Zd S) � _TellablecCs||_d|_dS)Nr)r8rR)r~r8rrrrsz_Tellable.__init__cCs|j�|�}|j|7_|S)N)r8�writerR)r~rTr�rrrr�s z_Tellable.writecCs|jS)N)rR)r~rrrrXsz_Tellable.tellcCs|j��dS)N)r8r�)r~rrrr�sz_Tellable.flushcCs|j��dS)N)r8r�)r~rrrr�sz_Tellable.closeN)rrrrr�rXr�r�rrrrr�s r�cs�eZdZdZdZdZdZd(dd�Zd d �Zd d �Z d)dd�Z d*dd�Z dd�Z d+dd�Z dd�Zdd�Zdd�Zdd�Z�fdd �Zd!d"�Zd,d$d%�Zd&d'�Z�ZS)-� ZipExtFilezZFile-like object for reading an archive member. Is returned by ZipFile.open(). i@iiNFcCs(||_||_||_|j|_|j|_|j|_t |j�|_ d|_ d|_ d|_ d|_||_|j|_t|d�rz|j|_td�|_nd|_d|_y4|��r�|��|_|j|_|j|_|j|_d|_Wntk r�YnXd|_|�r$|j d@r�|j!d?d@}n|jd?d@}|�"�}||k�r$t#d |j$��dS) NFr*rrmTrr�r�zBad password for file %r)%�_fileobj�_pwd�_close_fileobjrc�_compress_typern�_compress_leftro�_leftr�� _decompressor�_eof� _readbuffer�_offset�newlines�moder>�namer<rm� _expected_crcr�� _running_crc� _seekabler�rX�_orig_compress_start�_orig_compress_size�_orig_file_size�_orig_start_crc�AttributeError� _decrypterrhrp�_init_decrypterr�ra)r~r�r �zipinfor�Z close_fileobjZ check_byte�hrrrr1sF       zZipExtFile.__init__cCs4t|j�|_|j�d�}|jd8_|�|�dS)Nrr$)r�rrrr:r)r~r�rrrrds  zZipExtFile._init_decryptercCsvd|jj|jjfg}|jsX|�d|j|jf�|jtkrb|�dt � |j|j��n |�d�|�d�d� |�S)Nz<%s.%sz name=%r mode=%rz compress_type=%sz [closed]r�r�) r�rr�closedr.r r rrr�r�r/)r~r?rrrr�os   zZipExtFile.__repr__r�cCsL|dkr>|j�d|j�d}|dkr>|j|j|�}||_|Stj�||�S)zrRead and return a line from the stream. If limit is specified, at most limit bytes will be read. r� r)rrur �io�BufferedIOBase�readline)r~�limitr3�linerrrr}szZipExtFile.readlinercCsr|t|j�|jkr\|�|�}t|�|jkrJ||j|jd�|_d|_n|jt|�8_|j|j|jd�S)z6Returns buffered bytes without advancing the position.Nri)r-rr r:)r~r��chunkrrr�peek�s zZipExtFile.peekcCsdS)NTr)r~rrr�readable�szZipExtFile.readablecCs |dks|dkrL|j|jd�}d|_d|_x|jsF||�|j�7}q.W|S||j}|t|j�kr~|j|j|�}||_|S|t|j�}|j|jd�}d|_d|_x^|dk�r|j�s|�|�}|t|�kr�||_||_||d|�7}P||7}|t|�8}q�W|S)z�Read and return up to n bytes. If the argument is omitted, None, or negative, data is read and returned until EOF is reached. Nrr*)rr r�_read1�MAX_Nr-)r~r��buf�endrTrrrr:�s4   zZipExtFile.readcCs@|jdkrdSt||j�|_|jr<|j|jkr<td|j��dS)NzBad CRC-32 for file %r)r r�rrrr )r~�newdatarrr� _update_crc�s  zZipExtFile._update_crccCs|dks|dkrV|j|jd�}d|_d|_x$|jsP|�|j�}|r.||7}Pq.W|S||j}|t|j�kr�|j|j|�}||_|S|t|j�}|j|jd�}d|_d|_|dk�r xN|j�s |�|�}|t|�kr�||_||_||d|�7}P|r�||7}Pq�W|S)z7Read up to n bytes with at most one read() system call.Nrr*)rr rr#r$r-)r~r�r%rTr&rrr�read1�s>      zZipExtFile.read1cCs"|js|dkrdS|jtkrH|jj}|t|�krR||�|t|��7}n |�|�}|jtkrj|jdk|_nx|jtkr�t ||j �}|j� ||�}|jj p�|jdko�|jj |_|jr�||j� �7}n |j� |�}|jj p�|jdk|_|d|j�}|jt|�8_|jdk�rd|_|�|�|S)Nrr*T)rrrrZunconsumed_tailr-�_read2rrr]� MIN_READ_SIZEr�r�r�rr()r~r�rTrrrr#�s2           zZipExtFile._read1cCsd|jdkrdSt||j�}t||j�}|j�|�}|jt|�8_|sLt�|jdk r`|�|�}|S)Nrr*) rr]r+�minrr:r-�EOFErrorr)r~r�rTrrrr*s      zZipExtFile._read2cs&z|jr|j��Wdt���XdS)N)rrr��super)r~)r�rrr�!szZipExtFile.closecCs|jS)N)r)r~rrrr�(szZipExtFile.seekablercCsB|jst�d��|��}|dkr&|}n.|dkr8||}n|dkrL|j|}ntd��||jkrd|j}|dkrpd}||}||j}|dkr�|t|j�kr�||_d}nf|dk�r |j � |j �|j |_ |j|_|j|_d|_d|_t|j�|_d|_|}|jdk �r |��x.|dk�r8t|j|�}|�|�||8}�q W|��S)Nz!underlying stream is not seekablerrrzCwhence must be os.SEEK_SET (0), os.SEEK_CUR (1), or os.SEEK_END (2)r*F)rr�UnsupportedOperationrXrrzr r-rrrArrrrrrr�rrrrrr,� MAX_SEEK_READr:)r~rRr�Zcurr_posZnew_posZ read_offsetZ buff_offsetZread_lenrrrrA+sH           zZipExtFile.seekcCs0|jst�d��|j|jt|j�|j}|S)Nz!underlying stream is not seekable)rrr/rrr-rr )r~ZfileposrrrrX[s zZipExtFile.tell)NF)r�)r)r�)r)rrrrr$r+r0rrr�rr!r"r:r(r)r#r*r�r�rArX� __classcell__rr)r�rr�#s( 2    ! %$  0r�cs@eZdZdd�Zedd��Zdd�Zdd�Z�fd d �Z�Z S) � _ZipWriteFilecCs8||_||_||_t|j|j�|_d|_d|_d|_ dS)Nr) �_zinfo�_zip64�_zipfiler�rcrd� _compressor� _file_size�_compress_size�_crc)r~�zfr�r�rrrrcs z_ZipWriteFile.__init__cCs|jjS)N)r5r8)r~rrrrmsz_ZipWriteFile._fileobjcCsdS)NTr)r~rrr�writableqsz_ZipWriteFile.writablecCsf|jrtd��t|�}|j|7_t||j�|_|jrV|j�|�}|jt|�7_|j � |�|S)NzI/O operation on closed file.) rrzr-r7r�r9r6r�r8rr�)r~rT�nbytesrrrr�ts  z_ZipWriteFile.writec sb|jr dS�zFt���|jrR|j��}|jt|�7_|j�|�|j|j _ n |j |j _ |j |j _ |j |j _|j jd@r�|jr�dnd}|j�t�|t|j j |j j |j j��|j��|j_nn|js�|j tkr�td��|jtkr�td��|j��|j_|j�|j j�|j�|j �|j��|j�|jj�|jj�|j �|j |jj|j j<Wdd|j_ XdS)Nrz<LLQQz<LLLLz+File size unexpectedly exceeded ZIP64 limitz1Compressed size unexpectedly exceeded ZIP64 limitF)!rr.r�r6r�r8r-rr�r3rnr7r9rmrorhr4rBr�� _DD_SIGNATURErXr5� start_dirr�r�rArlr��filelistr.� NameToInfor>r�)r~r%r�)r�rrr��s>          z_ZipWriteFile.close) rrrr�propertyrr;r�r�r1rr)r�rr2bs   r2c@seZdZdZdZdZdeddfdd�Zdd�Zd d �Z d d �Z d d�Z dd�Z dd�Z d<dd�Zdd�Zdd�Zdd�Zedd��Zejdd��Zd=dd�Zd>d d!�d"d#�Zd?d$d%�Zd@d&d'�ZdAd(d)�Zed*d+��Zd,d-�Zd.d/�ZdBd0d1�ZdCd2d3�Zd4d5�Z d6d7�Z!d8d9�Z"d:d;�Z#dS)Dr 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�rTc CsL|dkrtd��t|�||_d|_d|_i|_g|_||_||_||_ d|_ d|_ t |t j�rjt �|�}t |t�r�d|_||_ddd d dd d d �}||}x^yt�||�|_Wn(tk r�||kr�||}w��YnXPq�Wnd|_||_t|dd�|_d|_t��|_d|_d|_�y|dk�r0|��n�|dk�r�d|_y|j��|_ Wn2t!tfk �r�t"|j�|_d|_ d|_Yn6Xy|j�#|j �Wn t!tfk �r�d|_YnXnf|dk�ry|��|j�#|j �Wn6t$k �r|j�#dd�d|_|j��|_ YnXntd��Wn$|j}d|_|�%|��YnXdS)z]Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', or append 'a'.)rB�w�x�az+ZipFile requires mode 'r', 'w', 'x', or 'a'FrNr*r;zw+bzx+bzr+b�wbZxb)rBrCrDrEzr+bzw+bzx+brr TrB)rCrDrErz"Mode must be 'r', 'w', 'x', or 'a')&rzr�� _allowZip64� _didModify�debugr@r?r�r�r r��_commentr�rwr�r��str� _filePassedr>rr=r8r7�getattr� _fileRefCnt� threading�RLockr�rr��_RealGetContentsrXr>rr�rAr�_fpclose) r~r�r r�� allowZip64r�ZmodeDictr�r8rrrr�s�             zZipFile.__init__cCs|S)Nr)r~rrr� __enter__#szZipFile.__enter__cCs |��dS)N)r�)r~�type�value� tracebackrrr�__exit__&szZipFile.__exit__cCs�d|jj|jjfg}|jdk rd|jr8|�d|j�n|jdk rR|�d|j�|�d|j�n |�d�|�d�d�|�S)Nz<%s.%sz file=%rz filename=%rz mode=%rz [closed]r�r�) r�rrr8rLr.r>r r/)r~r?rrrr�)s    zZipFile.__repr__c Cs�|j}y t|�}Wntk r.td��YnX|s<td��|jdkrNt|�|t}|t}|t|_ |t ||}|jdkr�||}td|||�|||_ |� |j d�|� |�}t�|�}d}�x�||k�r�|� t�} t| �tkr�td��t�t| �} | ttk�rtd��|jdk�r*t| �|� | t�} | d} | d @�rV| �d �} n | �d �} t| �} |� | t�| _|� | t�| _| t| _| dd �\ | _ | _!| _"| _#| _$| _%} }| _&| _'| _(| j"t)k�r�t*d | j"d��| dd�\| _+| _,| _-| | _.|d?d|d?d@|d@| d?| d?d@| d@df| _/| �0�| j|| _|j1�2| �| |j3| j4<|t| t| t| t}|jdkr�td|�q�W|j }x*t5|j1dd�dd�D]}||_6|j}�q�WdS)z/Read in the table of contents for the ZIP file.zFile is not a zip filerrzgiven, inferred, offsetrzTruncated central directoryz&Bad magic number for central directoryrizutf-8�cp437rzzip file version %.1fr#r&r)r"i��r$r�totalcSs|jS)N)rl)r�rrr�<lambda>�r*z*ZipFile._RealGetContents.<locals>.<lambda>T)�key�reverseN)7r8r6r7rrI�printrNrO� _ECD_COMMENTrJrPr>rAr:r�BytesIO�sizeCentralDirr-rBr,�structCentralDir� _CD_SIGNATURE�stringCentralDir�_CD_FILENAME_LENGTH�decoder �_CD_EXTRA_FIELD_LENGTHr0�_CD_COMMENT_LENGTHr`�_CD_LOCAL_HEADER_OFFSETrlrUrerfrgrhrcrmrnro�MAX_EXTRACT_VERSIONr�rirjrkrprbr�r?r.r@r>�sortedrq)r~r8rSZsize_cdZ offset_cd�concatZinferredrTr[�centdirr>�flagsrD�t�dZ end_offsetr�rrrrQ7sx               4        zZipFile._RealGetContentscCsdd�|jD�S)z+Return a list of file names in the archive.cSsg|] }|j�qSr)r>)�.0rTrrr� <listcomp>�sz$ZipFile.namelist.<locals>.<listcomp>)r?)r~rrr�namelist�szZipFile.namelistcCs|jS)zJReturn a list of class ZipInfo instances for files in the archive.)r?)r~rrr�infolist�szZipFile.infolistcCsPtdd|d�x:|jD]0}d|jdd�}td|j||jf|d�qWdS)z+Print a table of contents for the zip file.z%-46s %19s %12s)z File Namez Modified ZSize)r�z%d-%02d-%02d %02d:%02d:%02dNr z %-46s %s %12d)r_r?rbr>ro)r~r�r�Zdaterrr�printdir�s  zZipFile.printdirc Cs^d}xT|jD]J}y.|�|jd��}x|�|�r0q$WWdQRXWq tk rT|jSXq WdS)z%Read all the files and check the CRC.irBN)r?r=r>r:r)r~Z chunk_sizer��frrr�testzip�s  zZipFile.testzipcCs$|j�|�}|dkr td|��|S)z,Return the instance of ZipInfo given 'name'.Nz(There is no item named %r in the archive)r@r��KeyError)r~r �inforrr�getinfo�s   zZipFile.getinfocCs6|r t|t�s tdt|�j��|r,||_nd|_dS)z)Set default password for encrypted files.zpwd: expected bytes, got %sN)r�r�� TypeErrorrUrr�)r~r�rrr� setpassword�s zZipFile.setpasswordcCs|jS)z.The comment text associated with the ZIP file.)rJ)r~rrrr`�szZipFile.commentcCs^t|t�stdt|�j��t|�tkrNddl}|jdtdd�|dt�}||_ d|_ dS)Nzcomment: expected bytes, got %srz3Archive comment is too long; truncating to %d bytesr)� stacklevelT) r�r�r|rUrr-�ZIP_MAX_COMMENT�warnings�warnrJrH)r~r`r�rrrr`�s    c Cs"|�|d|�� }|��SQRXdS)zReturn file bytes for name.rBN)r=r:)r~r r�r8rrrr:�sz ZipFile.readF)� force_zip64c sF|dkrtd��|r0t|t�s0tdt|�j��|rD|dkrDtd���jsRtd��t|t�rb|}n,|dkr�t|�}�j|_ �j |_ n �� |�}|dkr��j ||d�S�jr�td���jd 7_t�j|j�j�j�fd d ��}�yH|�t�}t|�tk�rtd ��t�t|�}|ttk�r&td ��|�|t�}|t�rL|�|t�|jd@�r`t d��|jd@�rtt d��|jd@�r�|�!d�} n |�!d�} | |j"k�r�td|j"|f��|j#dk �r�|�$�|j%|j#k�r�td|j"�d���|jd @} | �r|�s�j&}|�st'd|��nd}t(||||d�S|�)��YnXdS)auReturn 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. >rBrCzopen() requires mode "r" or "w"zpwd: expected bytes, got %srCz'pwd is only supported for reading filesz2Attempt to use ZIP archive that was already closed)r�zyCan't read from the ZIP file while there is an open writing handle on it. Close the writing handle before trying to read.rcs�jS)N)r�r)r~rrr\r*zZipFile.open.<locals>.<lambda>zTruncated file headerz Bad magic number for file header� z$compressed patched data (flag bit 5)�@zstrong encryption (flag bit 6)izutf-8rYz/File name in directory %r and header %r differ.NzOverlapped entries: z (possible zip bomb)z6File %r is encrypted, password required for extractionT)*rzr�r�r|rUrr8r r�rcr�rdr{�_open_to_writer�rNr�rlrRr�r:�sizeFileHeaderr-rrBr,r�� _FH_SIGNATUREr��_FH_FILENAME_LENGTH�_FH_EXTRA_FIELD_LENGTHrhr�rgrarqrXrnr�r�r�r�) r~r r r�r�r�Zzef_fileZfheader�fnameZ fname_strZ is_encryptedr)r~rr=�sv                 z ZipFile.opencCs�|r|jstd��|jr td��t|d�s0d|_d|_d|_d|_|jt krZ|jdO_|j sn|jdO_|j szd|_ |jo�|p�|jdt 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.rorrri�g�������?T)rGrzr�r<rornrmrhrcrrrkr�r8rAr>rXrl� _writecheckrHr�r�r2)r~r�r�r�rrrr�6s4     zZipFile._open_to_writecCs*|dkrt��}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)rw�getcwdr��_extract_member)r~�memberr�r�rrr�extractas  zZipFile.extractcCsL|dkr|��}|dkr"t��}n t�|�}x|D]}|�|||�q2WdS)z�Extract 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)rtrwr�r�r�)r~r��membersr�rrrr� extractallns   zZipFile.extractallcCs^|j}|s(d}t�|dt|��}||_|�|�}dd�|�|�D�}|�dd�|D��}|S)z;Replace bad characters and remove trailing dots from parts.z:<>|"?*�_css|]}|�d�VqdS)�.N)�rstrip)rrrDrrr� <genexpr>�sz1ZipFile._sanitize_windows_name.<locals>.<genexpr>css|]}|r|VqdS)Nr)rrrDrrrr��s)�!_windows_illegal_name_trans_tablerK� maketransr-� translate�splitr/)r�r��pathsep�tableZillegalrrr�_sanitize_windows_names zZipFile._sanitize_windows_namec sLt|t�s|�|�}|j�dtjj�}tjjrB|�tjjtjj�}tj� |�d}dtjj tjj f�tjj� �fdd�|� tjj�D��}tjjdkr�|�|tjj�}tj� ||�}tj�|�}tj�|�}|r�tj�|�s�t�|�|���r tj�|��st�|�|S|j||d��(}t|d��}t�||�Wd QRXWd QRX|S) zbExtract the ZipInfo object 'member' to a physical file on the path targetpath. rtrr�c3s|]}|�kr|VqdS)Nr)rrrD)�invalid_path_partsrrr��sz*ZipFile._extract_member.<locals>.<genexpr>�\)r�rFN)r�r r{r>ryrwr�rxr�r��curdir�pardirr/r�r�r��dirname�exists�makedirsr�r��mkdirr=�shutil� copyfileobj)r~r�Z targetpathr�r�Z upperdirs�source�targetr)r�rr��s.  &        zZipFile._extract_membercCs�|j|jkr(ddl}|jd|jdd�|jdkr:td��|jsHtd��t|j�|j s�d}t |j �t krpd }n|j tkr�d }n|jtkr�d }|r�t|d ��dS) z6Check for errors before writing a file to the archive.rNzDuplicate name: %rr)r~)rCrDrEz&write() requires mode 'w', 'x', or 'a'z4Attempt to write ZIP archive that was already closedz Files countZFilesizez Zipfile sizez would require ZIP64 extensions)r>r@r�r�r rzr8r�rcrGr-r?�ZIP_FILECOUNT_LIMITror�rlr )r~r�r��requires_zip64rrrr��s(     zZipFile._writecheckc CsJ|jstd��|jrtd��t�||�}|��r>d|_d|_n0|dk rN||_n|j |_|dk rf||_ n|j |_ |���r |j ��|j r�|j�|j�|j��|_|jtkr�|jdO_|�|�d|_|j�|�||j|j<|j�|�d��|j��|_WdQRXn<t|d��,}|�|d ��}t�||d �WdQRXWdQRXdS) zLPut the bytes from filename into the archive under the name arcname.z7Attempt to write to ZIP archive that was already closedz>Can't write to ZIP archive while an open writing handle existsrNrTFr;rCi )r8rzr�r r�r�rnrmrcr�rdr�r�rrAr>rXrlrrhr�rHr?r.r@r>r�r�r=r�r�)r~r>r�rcr�r��src�destrrrr��s>       z ZipFile.writec Cs�t|t�r|�d�}t|t�sxt|t�t���dd�d�}|j|_|j|_ |j ddkrpd|_ |j dO_ q|d |_ n|}|j s�t d ��|jr�t d ��|dk r�||_|dk r�||_ t|�|_|j�*|j|d d ��}|�|�WdQRXWdQRXdS)aWrite 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.zutf-8Nr )r>rbr�rti�Ar'i�z7Attempt to write to ZIP archive that was already closedz?Can't write to ZIP archive while an open writing handle exists.rC)r )r�rKr�r r�r�r�rcr�rdr>rkr8rzr�r-ror�r=r�)r~Zzinfo_or_arcnamerTrcr�r�r�rrr�writestr�s4    zZipFile.writestrcCs |��dS)z2Call the "close()" method in case the user forgot.N)r�)r~rrr�__del__(szZipFile.__del__c Cs||jdkrdS|jrtd��zB|jdkr\|jr\|j�"|jrJ|j�|j�|� �WdQRXWd|j}d|_|� |�XdS)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.)rCrDrE) r8r�rzr rHr�rrAr>�_write_end_recordrR)r~r8rrrr�,s z ZipFile.closecCsv�xP|jD�]D}|j}|ddd>|dd>B|dB}|dd>|d d>B|ddB}g}|jtksv|jtkr�|�|j�|�|j�d }d }n |j}|j}|jtkr�|�|j�d }n|j}|j} d} |�rt| d �} t j d d t |�ddt |�f|��| } t } |j tk�r(tt| �} n|j tk�r>tt| �} t| |j�} t| |j�} yZ|��\} }t � tt| |j| |j||j |||j||t | �t | �t |j�d|j|j|�}Wnltk �rttt| |j| |j|j |j |||j||t |j!�t | �t |j�d|j|j|ft"j#d��YnX|j$�%|�|j$�%| �|j$�%| �|j$�%|j�q W|j$�&�}t |j�}||j'}|j'}d}|t(k�r�d}n|tk�r�d}n|tk�r�d}|�r.|j)�s�t*|d��t � t+t,t-ddddd||||� }|j$�%|�t � t.t/d|d�}|j$�%|�t0|d�}t0|d �}t0|d �}t � t1t2dd||||t |j3�� }|j$�%|�|j$�%|j3�|j$�4�dS)Nri�r"rrrrr$rl��)rz<HH�Qr)r�z Files countzCentral directory offsetzCentral directory sizez would require ZIP64 extensionsrri��)5r?rbror�rnr.rlr0r5rBr�r-r�rcrr]r�rr�rfrUr�rcrerergrmr`rjrk�DeprecationWarningr_rhr>r{�stderrr8r�rXr>r�rGr rHrGrErCrDr,r[rZrJr�)r~r�r�r�r�r0rornrlZ extra_datar�rfrUr>rhrnZpos2Z centDirCountZ centDirSizeZ centDirOffsetr�Z zip64endrecZ zip64locrecrSrrrr�Bs�$$                                     zZipFile._write_end_recordcCs4|jdkst�|jd8_|js0|js0|��dS)Nrr)rN�AssertionErrorrLr�)r~r8rrrrR�s zZipFile._fpclose)N)N)rBN)F)NN)NNN)NNN)NN)$rrrrr8r�rrrTrXr�rQrtrurvrxr{r}rAr`�setterr:r=r�r�r�r�r�r�r�r�r�r�r�r�rRrrrrr �s@ XQ     f +  * 0 (fc@s4eZdZdZdeddfdd�Zd d d �Zd d �ZdS)r zDClass to create ZIP archives with Python library files and packages.rBTr�cCstj|||||d�||_dS)N)r r�rS)r r� _optimize)r~r�r r�rS�optimizerrrr�s zPyZipFile.__init__r�NcCs�t�|�}|rD||�sD|jr@tj�|�r,dnd}td||f�dStj�|�\}}tj�|��rptj�|d�}tj�|��r�|r�d||f}n|}|jr�td|d|�|� |d d �|�\}} |jr�td | �|� || �t t� |��} | � d�x�| D]�} tj�|| �} tj�| �\} }tj�| ��rTtj�tj�| d���r�|j| ||d �q�|d kr�|�r�|| ��s�|jr�td| �q�|� | d d �|�\}} |j�r�td | �|� || �q�Wn�|j�r�td|�x�t t� |��D]�} tj�|| �} tj�| �\} }|d k�r�|�r2|| ��s2|j�r�td| ��q�|� | d d �|�\}} |j�r\td | �|� || ��q�WnP|d d�d k�r�td��|� |d d �|�\}} |j�r�td| �|� || �dS)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__.pyz%s/%szAdding package in�asr���ZAdding)� filterfuncz.pyzfile %r skipped by filterfunczAdding files from directoryz.Files added with writepy() must end with ".py"z Adding file)rwr�rIr�r�r_r�r/�isfile� _get_codenamer�rl�listdir�remove�splitext�writepyr�)r~�pathname�basenamer�Zlabel�dirr Zinitnamer�r�Zdirlistr>r��rootZextrrrr��sx                zPyZipFile.writepyc sd�fdd� }|d}|d}tjj|dd�}tjj|dd�}tjj|d d�}�jdk�r\tj�|�r�t�|�jt�|�jkr�|} } n�tj�|�r�t�|�jt�|�jkr�|} |} n�tj�|�r�t�|�jt�|�jkr�|} |} nvtj�|��rt�|�jt�|�jk�r|} |} nD||��rRt j j d k�r4|} nt j j dk�rH|} n|} |} n|} } n��jd k�rr|} |} n<|} �jdk�r�|} n&�jd k�r�|} nd � �j�} t | ��tj�| ��r�t�| �jt�|�jk�s�||�jd ��s�|} } tj�| �d} |�rd || f} | | fS)aReturn (filename, archivename) for the path. Given a module name path, return the correct file path and archive name, compiling if necessary. For example, given /python/lib/string, return (/python/lib/string.pyc, string). r�c s`ddl}�jrtd|�y|j|d|d�Wn.|jk rZ}zt|j�dSd}~XYnXdS)NrZ CompilingT)�doraiser�F)� py_compilerIr_�compile�PyCompileError�msg)r�r�r��err)r~rr�_compiles  z)PyZipFile._get_codename.<locals>._compilez.pyz.pycr�)� optimizationrrrz"invalid value for 'optimize': {!r})r�z%s/%s)r�)� importlib�util�cache_from_sourcer�rwr�r�r�r�r{ror��formatrzr�) r~r�r�r�Zfile_pyZfile_pycZ pycache_opt0Z pycache_opt1Z pycache_opt2r�r�r�Z archivenamer)r~rr� s`             zPyZipFile._get_codename)r�N)rrrrrrr�r�rrrrr �s  Rc s�ddl}d}|j|d�}|jdd�}|jdddd d �|jd d d ddd�|jdddddd�|jddddd �|�|�}|jdk r�|j}t|d��}|��}WdQRX|r�td� |��td��n|j dk r�|j }t|d��}|� �WdQRXn�|j dk �r.|j \}}t|d��}|� |�WdQRXn�|jdk �r�|j�d�} |j} �fdd��t| d��`}xX| D]P} tj�| �} | �s�tj�tj�| ��} | dtjtjfk�r�d} �|| | ��qjWWdQRXdS) Nrz3A simple command-line interface for zipfile module.)� descriptionT)Zrequiredz-lz--listz <zipfile>zShow listing of a zipfile)�metavar�helpz-ez --extractr)z <zipfile>z <output_dir>zExtract zipfile into target dir)�nargsr�r�z-cz--create�+)z<name>z<file>zCreate zipfile from sourcesz-tz--testzTest if a zipfile is validrBz.The following enclosed file is corrupted: {!r}z Done testingcsttj�|�r|�||t�nTtj�|�rp|r8|�||�x6tt�|��D]$}�|tj�||�tj�||��qHWdS)N) rwr�r�r�rr�rlr�r/)r:r��zippathZnm)�addToZiprrr��s   zmain.<locals>.addToZiprCr�)�argparse�ArgumentParserZadd_mutually_exclusive_group� add_argument� parse_argsZtestr rxr_r�r\rvr�r�Zcreate�poprwr�r�r�r�r�) �argsr�r��parser�groupr�r:Zbadfiler�Zzip_name�filesr�r�r)r�r�main]sT                    r��__main__)N)N)�rrrw�importlib.utilr�r{r�r�r�rBZbinasciirOr�r�� ImportErrorr�r��__all__� Exceptionrr rrr�r�rrrrrr}r�r�r�rkr[rZr�rYrIrJrKrLrMrNrOr_r`rPrcrerbrdZ_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_SIZErfrhriZ_CD_DISK_NUMBER_STARTZ_CD_INTERNAL_FILE_ATTRIBUTESZ_CD_EXTERNAL_FILE_ATTRIBUTESrjr�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�rCrDr@rHrGrEZ_CD64_SIGNATUREZ_CD64_DIRECTORY_RECSIZEZ_CD64_CREATE_VERSIONZ_CD64_EXTRACT_VERSIONZ_CD64_DISK_NUMBERZ_CD64_DISK_NUMBER_STARTZ_CD64_NUMBER_ENTRIES_THIS_DISKZ_CD64_NUMBER_ENTRIES_TOTALZ_CD64_DIRECTORY_SIZEZ_CD64_OFFSET_START_CENTDIRr=ZStructr+r5r9r rVr6�objectr r�r�r�r�r�r�r�r�r�r�r�rr�r2r r r�rrrrr�<module>s@             <<o& &AN/ <