3 \)2@sdZddlZddlZddlZddlZddlZddlZddlZddl Z ddl Z ddl Z y ddl Z Wne k r|ddlZ YnXyddlZejZWne k rdZe jZYnXy ddlZWne k rdZYnXy ddlZWne k rdZYnXddddddd d d d d dg ZGdddeZGdddeZeZZdfZdhZdjZdZdZdZdZdZ dZ!dZ"dZ#dZ$dZ%dZ&e j'e%Z(dZ)dZ*dZ+dZ,dZ-d Z.d!Z/d"Z0dZ1d#Z2d$Z3d%Z4e j'e3Z5dZ6dZ7dZ8dZ9dZ:d Z;d!Zd#Z?d&Z@d'ZAdZBd(ZCdZDd)ZEdZFd*ZGd+ZHd,ZId-ZJe j'eIZKdZLdZMdZNdZOdZPd ZQd!ZRd"ZSdZTd#ZUd&ZVd'ZWd.ZXd/ZYe j'eXZZd0Z[d1Z\e j'e[Z]dZ^dZ_dZ`dZadZbd Zcd!Zdd"ZedZfd#Zgd2Zhe jid3Zjd4d5Zkd6d7Zld8d Zmd9d:Znd;d<ZoGd=d d epZqGd>d?d?ZrGd@dAdAZsGdBdCdCZtdDdEdFdFdFdFdGdHdIdJdGdKdLdMdNdOdPdQZudRdSZvdTdUZwdVdWZxGdXdYdYZyGdZd[d[ZzGd\d]d]ej{Z|Gd^d_d_ej{Z}Gd`d d Z~Gdad d e~ZdkdbdcZeddkredS)lzP Read and write ZIP files. XXX references to utf-8 need further investigation. N BadZipFile BadZipfileerror ZIP_STORED ZIP_DEFLATED ZIP_BZIP2ZIP_LZMA is_zipfileZipInfoZipFile PyZipFile LargeZipFilec@s eZdZdS)rN)__name__ __module__ __qualname__rr/usr/lib64/python3.6/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 /s -.?s<4s4H2LHsPK z<4s4B4HL2L5H2LsPK z <4s2B4HL2L2HsPKz<4sLQLsPKz <4sQ2H2L4QsPKiPKz        r7c@sVeZdZdZd(Zd*ddZddZd+ddZd d!Zd"d#Z e d,d$d%Z d&d'Z dS)-r z>Class with attributes describing each file in the ZIP archive. orig_filenamer? date_time compress_typer_r1 create_systemrTextract_versionreserved flag_bitsvolume internal_attr external_attr header_offsetCRC compress_size file_size _raw_timeNoNamerrcCs||_|jtd}|dkr(|d|}tjdkrJtj|krJ|jtjd}||_||_|ddkrjtdt |_ d|_ d|_ t jdkrd|_nd|_t|_t|_d|_d|_d|_d|_d|_dS)Nr/iz+ZIP does not support timestamps before 1980r+Zwin32r)r`findchrossepreplacer?ra ValueErrorrrbr_r1sysplatformrcDEFAULT_VERSIONrTrdrerfrgrhri)selfr?raZ null_byterrr__init__Ws.   zZipInfo.__init__cCsd|jj|jfg}|jtkr8|jdtj|j|j|jd?}|jd@}|rd|jdt j ||rv|jd||j }| s|j r|jd|j | s|j r|jtks|j |j kr|jd|j |jd d j|S) Nz<%s filename=%rz compress_type=%sriz filemode=%rz external_attr=%#xz file_size=%rz compress_size=%r>) __class__rr?rbrr/compressor_namesgetristatfilemodeis_dirrmrlr0)r{r@hiloisdirrrr__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}|rd } |tj| dtj | d ||}|tks|tkr|st d d}d}t } |j t krtt| } n|j tkrtt| } t| |j|_t| |j|_|j\} } tjtt|j|j| |j |||||t| t| } | | |S)z-Return the per-file header as a bytes object.rir$rr!rrr&r rNzy|jjd|jfStk r8|jjd|jdBfSXdS)Nasciizutf-8i)r?encoderfUnicodeEncodeError)r{rrrrszZipInfo._encodeFilenameFlagscCs(|j}tj}xt|dkr"|d|dd\}}|dkr|dkrZ|d|dd}nV|dkrv|d|dd }n:|d kr|d |dd }n|d krf}ntd||fd }|jdkr|||_|d7}|jdkr|||_|d7}|jdkr|j}|||_|d7}||dd}qWdS)Nr z|_|rd|_|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"Nrrqir) isinstancertPathLikefspathrS_ISDIRst_modetime localtimest_mtimepathnormpath splitdriverualtseprirmst_size)clsr?arcnamestrmtimerazinforrr from_files(       zZipInfo.from_filecCs|jddkS)z2Return True if this archive member is a directory.rrq)r?)r{rrrrszZipInfo.is_dir)r`r?rarbr_r1rcrTrdrerfrgrhrirjrkrlrmrnrprrrrr)ror)N)N) rrrr __slots__r|rrrr classmethodrrrrrrr >s8 * .$ !c@s<eZdZdZddZdZddZddZd d Zd d Z dS) _ZipDecrypteraClass to handle decryption of files stored within a ZIP archive. ZIP supports a password-based form of encryption. Even though known plaintext attacks have been found against it, it is still useful to be able to get data out of such a file. Usage: zd = _ZipDecrypter(mypwd) plain_char = zd(cypher_char) plain_text = map(zd, cypher_text) cCshd}dgd}xTtdD]H}|}x6tdD]*}|d@rH|d?d@|A}q*|d?d@}q*W|||<qW|S)zGenerate a CRC-32 table. ZIP encryption uses the CRC32 one-byte primitive for scrambling some internal keys. We noticed that a direct implementation is faster than relying on binascii.crc32(). l q[rrri)range)Zpolytabler4crcr5rrr_GenerateCRCTable#s  z_ZipDecrypter._GenerateCRCTableNcCs|d?d@|j||Ad@AS)z(Compute the CRC32 primitive on one byte.ri)crctable)r{Zchrrrr_crc327sz_ZipDecrypter._crc32cCsBtjdkrtjt_d|_d|_d|_x|D]}|j|q,WdS)NixV4igE#ixV4)rrrkey0key1key2 _UpdateKeys)r{pwdprrrr|;s   z_ZipDecrypter.__init__cCsX|j||j|_|j|jd@d@|_|jddd@|_|j|jd?d@|j|_dS)Nrlirr)rrrr)r{crrrrDsz_ZipDecrypter._UpdateKeyscCs0|jdB}|||dAd?d@A}|j||S)zDecrypt a single character.rrrr)rr)r{rkrrr__call__Js  z_ZipDecrypter.__call__) rrrrrrrr|rrrrrrrs  rc@s,eZdZddZddZddZddZd S) LZMACompressorcCs d|_dS)N)_comp)r{rrrr|UszLZMACompressor.__init__cCsFtjdtji}tjtjtjtj|gd|_tjdddt ||S)Nid)filtersz|jjd|jd}|dkr>|j|j|}||_|Stjj||S)zrRead and return a line from the stream. If limit is specified, at most limit bytes will be read. r r)rrrrioBufferedIOBasereadline)r{limitr4linerrrr1szZipExtFile.readlinecCsr|t|j|jkr\|j|}t||jkrJ||j|jd|_d|_n|jt|8_|j|j|jdS)z6Returns buffered bytes without advancing the position.Nri)r.rrr;)r{rchunkrrrpeekAs zZipExtFile.peekcCsdS)NTr)r{rrrreadableNszZipExtFile.readablecCs |dks|dkrL|j|jd}d|_d|_x|jsF||j|j7}q.W|S||j}|t|jkr~|j|j|}||_|S|t|j}|j|jd}d|_d|_x^|dko|j r|j|}|t|kr||_||_||d|7}P||7}|t|8}qW|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.. Nrr+)rrr_read1MAX_Nr.)r{rbufendrSrrrr;Qs4   zZipExtFile.readcCs@|jdkrdSt||j|_|jr<|j|jkr      zZipExtFile.read1cCs"|js|dkrdS|jtkrH|jj}|t|krR||j|t|7}n |j|}|jtkrj|jdk|_nx|jtkrt ||j }|jj ||}|jj p|jdko|jj |_|jr||jj 7}n |jj |}|jj p|jdk|_|d|j}|jt|8_|jdkrd|_|j||S)Nrr+T)rrrrZunconsumed_tailr._read2rrr\ MIN_READ_SIZErrrrr)r{rrSrrrrs2           zZipExtFile._read1cCsj|jdkrdSt||j}t||j}|jj|}|jt|8_|sLt|jdk rft t |j|}|S)Nrr+) rr\rminrr;r.EOFErrorrbytesmap)r{rrSrrrrs     zZipExtFile._read2c s&z|jr|jjWdtjXdS)N)rrrsuper)r{)rrrrszZipExtFile.closei@)NFr)r)rr)r)rrrrrrr|rrrrr;rrrrr __classcell__rr)rrrs    ! %$rcs@eZdZddZeddZddZddZfd d ZZ S) _ZipWriteFilecCs4||_||_||_t|j|_d|_d|_d|_dS)Nr) _zinfo_zip64_zipfilerrb _compressor _file_size_compress_size_crc)r{zfrrrrrr|s z_ZipWriteFile.__init__cCs|jjS)N)r)r9)r{rrrrsz_ZipWriteFile._fileobjcCsdS)NTr)r{rrrwritablesz_ZipWriteFile.writablecCsf|jrtdt|}|j|7_t||j|_|jrV|jj|}|jt|7_|j j ||S)NzI/O operation on closed file.) r rwr.r+r r-r*rr,rr)r{rSnbytesrrrrs  z_ZipWriteFile.writecsX|jr dStj|jrN|jj}|jt|7_|jj||j|j _ n |j |j _ |j |j _ |j |j _|j jd@r|jrdnd}|jjtj|t|j j |j j |j j|jj|j_nn|js|j tkrtd|jtkrtd|jj|j_|jj|j j|jj|j j|j|jj|jjd|j_|jjj|j |j |jj|j j <dS)Nrzd d!d"d#Zd?d$d%Zd@d&d'ZdAd(d)Zed*d+Zd,d-Zd.d/ZdBd0d1ZdCd2d3Zd4d5Z d6d7Z!d8d9Z"d:d;Z#dS)Dr a Class with methods to open, read, write, close, list zip files. z = ZipFile(file, mode="r", compression=ZIP_STORED, allowZip64=True) file: Either the path to the file, or a file-like object. If it is a path, the file will be opened and closed by ZipFile. mode: The mode can be either read 'r', write 'w', 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. NrTcCsF|dkrtdt|||_d|_d|_i|_g|_||_||_d|_ d |_ t |t j rdt j|}t |trd|_||_d d d d d ddd}||}x^ytj|||_Wn(tk r||kr||}wYnXPqWnd|_||_t|dd|_d|_tj|_d|_d|_y|dkr*|jn|dkrd|_y|jj|_Wn2t tfk r|t!|j|_d|_d|_Yn6Xy|jj"|jWn t tfk rd|_YnXnf|dkry|j|jj"|jWn6t#k r|jj"ddd|_|jj|_YnXntdWn$|j}d|_|j$|YnXdS)z]Open the ZIP file with mode read 'r', write 'w', exclusive create 'x', or append 'a'.r6wxaz+ZipFile requires mode 'r', 'w', 'x', or 'a'FrNr+r<zw+bzx+bzr+bwbZxb)r6r7r8r9zr+bzw+bzx+brrTrz"Mode must be 'r', 'w', 'x', or 'a')r6r7r8r9)r7r8)%rwr _allowZip64 _didModifydebugr4r3rrr_commentrrtrrstr _filePassedr?rr>r9r8getattr _fileRefCnt threadingRLockr _seekabler_RealGetContentsrWr2AttributeErrorrrAr_fpclose)r{rrr allowZip64ZmodeDictrr9rrrr|;s             zZipFile.__init__cCs|S)Nr)r{rrr __enter__szZipFile.__enter__cCs |jdS)N)r)r{typevalue tracebackrrr__exit__szZipFile.__exit__cCsd|jj|jjfg}|jdk rd|jr8|jd|jn|jdk rR|jd|j|jd|jn |jd|jddj|S)Nz<%s.%sz file=%rz filename=%rz mode=%rz [closed]r}r~) rrrr9r@r/r?rr0)r{r@rrrrs    zZipFile.__repr__cCs|j}y t|}Wntk r.tdYnX|s _ECD_LOCATIONrIrHrFrBr2rAr;rBytesIOsizeCentralDirr.rCr-structCentralDir _CD_SIGNATUREstringCentralDir_CD_FILENAME_LENGTHdecoder _CD_EXTRA_FIELD_LENGTHr1_CD_COMMENT_LENGTHr__CD_LOCAL_HEADER_OFFSETrjrTrcrdrerfrbrkrlrmMAX_EXTRACT_VERSIONrrgrhrirnrarr3r/r4r?)r{r9rRZsize_cdZ offset_cdconcatZinferredrSrPcentdirr?flagsr8tdrrrrFsp                4       zZipFile._RealGetContentscCsdd|jDS)z+Return a list of file names in the archive.cSsg|] }|jqSr)r?).0rSrrr sz$ZipFile.namelist..)r3)r{rrrnamelistszZipFile.namelistcCs|jS)zJReturn a list of class ZipInfo instances for files in the archive.)r3)r{rrrinfolistszZipFile.infolistcCsPtdd |dx:|jD]0}d|jdd}td |j||jf|dqWdS) z+Print a table of contents for the zip file.z%-46s %19s %12s File Name Modified Size)rz%d-%02d-%02d %02d:%02d:%02dNr"z %-46s %s %12d)rhrirj)rQr3rar?rm)r{rrZdaterrrprintdirs  zZipFile.printdircCs^d}xT|jD]J}y.|j|jd}x|j|r0q$WWdQRXWq tk rT|jSXq WdS)z%Read all the files and check the CRC.rrr6Ni)r3r>r?r;r)r{Z chunk_sizerfrrrtestzips  zZipFile.testzipcCs$|jj|}|dkr td||S)z,Return the instance of ZipInfo given 'name'.Nz(There is no item named %r in the archive)r4rKeyError)r{rinforrrgetinfos   zZipFile.getinfocCs8|r"t|t r"tdt|j|r.||_nd|_dS)z)Set default password for encrypted files.zpwd: expected bytes, got %sN)rr! TypeErrorrKrr)r{rrrr setpasswords zZipFile.setpasswordcCs|jS)z.The comment text associated with the ZIP file.)r>)r{rrrr_%szZipFile.commentcCs^t|tstdt|jt|tkrNddl}|jdtdd|dt}||_ d|_ dS)Nzcomment: expected bytes, got %srz3Archive comment is too long; truncating to %d bytesr) stacklevelT) rr!rqrKrr.ZIP_MAX_COMMENTwarningswarnr>r<)r{r_rurrrr_*s    c Cs"|j|d| }|jSQRXdS)zReturn file bytes for name.r6N)r>r;)r{rrr9rrrr;7sz ZipFile.readF) force_zip64c sv|d krtd|r2t|t r2tdt|j|rF|dkrFtdjsTtdt|trd|}n$|dkr~t|}j|_ n j |}|dkrj ||dSj rtdj d 7_ tj|jjjfd d }y~|jt}t|tkrtd tjt|}|ttkr td |j|t}|trF|j|t|jd@rZtd|jd@rntd|jd@r|jd} n |jd} | |j krtd|j |f|jd @} d} | rL|sΈj!}|st"d|t#|} |jd} t$t%| | dd} |jd@r$|j&d?d@}n|j'd?d@}| d|krLt"d|t(|||| dS|j)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. r6r7zopen() requires mode "r" or "w"zpwd: expected bytes, got %sz'pwd is only supported for reading filesz2Attempt to use ZIP archive that was already closed)rwzyCan't read from the ZIP file while there is an open writing handle on it. Close the writing handle before trying to read.rcsjS)N)rr)r{rrlszZipFile.open..zTruncated file headerz Bad magic number for file header z$compressed patched data (flag bit 5)@zstrong encryption (flag bit 6)izutf-8rOz/File name in directory %r and header %r differ.Nz6File %r is encrypted, password required for extractionrrrrrr&zBad password for file %rT>r6r7)*rwrr!rqrKrr9r rrbrp_open_to_writerrBrrjrHrr;sizeFileHeaderr.rrCr-r _FH_SIGNATUREr_FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTHrfrrZr`rrrr[r"rnrkrr)r{rrrrwrZzef_fileZfheaderfnameZ fname_strZ is_encryptedZzdrhZ check_byter)r{rr><s~                  z ZipFile.opencCs|r|j rtd|jr"tdt|ds2d|_d|_d|_d|_|jt kr\|jdO_|j sp|jdO_|j s|d |_ |jo|p|jd t k}|j r|j j|j|j j|_|j|d |_|j j|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.rmrrrirg?Ti)r;rwrr=rmrlrkrfrbrrErirr9rAr2rWrj _writecheckr<rrr&)r{rrwrrrrr{s4     zZipFile._open_to_writecCs*|dkrtj}n tj|}|j|||S)a#Extract a member from the archive to the current working directory, using its full name. Its file information is extracted as accurately as possible. `member' may be a filename or a ZipInfo object. You can specify a different directory using `path'. N)rtgetcwdr_extract_member)r{memberrrrrrextracts  zZipFile.extractcCsL|dkr|j}|dkr"tj}n tj|}x|D]}|j|||q2WdS)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)rfrtrrr)r{rmembersrr rrr extractalls   zZipFile.extractallcCs^|j}|s(d}tj|dt|}||_|j|}dd|j|D}|jdd|D}|S)z;Replace bad characters and remove trailing dots from parts.z:<>|"?*_css|]}|jdVqdS).N)rstrip)rdr8rrr sz1ZipFile._sanitize_windows_name..css|]}|r|VqdS)Nr)rdr8rrrrs)!_windows_illegal_name_trans_tabler? maketransr. translatesplitr0)rrpathseprZillegalrrr_sanitize_windows_names zZipFile._sanitize_windows_namecsNt|ts|j|}|jjdtjj}tjjrB|jtjjtjj}tjj |d}dtjj tjj ftjjj fdd|j tjjD}tjjdkr|j|tjj}tjj ||}tjj|}tjj|}|rtjj| rtj||jrtjj|s tj||S|j||d(}t|d}tj||Wd QRXWd QRX|S) zbExtract the ZipInfo object 'member' to a physical file on the path targetpath. rqrr~c3s|]}|kr|VqdS)Nr)rdr8)invalid_path_partsrrrsz*ZipFile._extract_member..\)rr:N)rr rpr?rvrtrrurrcurdirpardirr0rrrdirnameexistsmakedirsrrmkdirr>shutil copyfileobj)r{rZ targetpathrrZ upperdirssourcetargetr)rrrs.  &        zZipFile._extract_membercCs|j|jkr(ddl}|jd|jdd|jdkr:td |jsHtd t|j|j sd}t |j t krpd }n|j tkrd }n|jtkrd }|rt|ddS)z6Check for errors before writing a file to the archive.rNzDuplicate name: %rr)rsr7r8r9z&write() requires mode 'w', 'x', or 'a'z4Attempt to write ZIP archive that was already closedz Files countZFilesizez Zipfile sizez would require ZIP64 extensions)r7r8r9)r?r4rurvrrwr9rrbr;r.r3ZIP_FILECOUNT_LIMITrmrrjr )r{rrurequires_zip64rrrr/s(     zZipFile._writecheckcCs0|jstd|jrtdtj||}|jr>d|_d|_n|dk rN||_n|j |_|jr|j |j rz|jj |j |jj|_|jtkr|jdO_|j|d|_|jj|||j|j<|jj|jd|jj|_ WdQRXnCan't write to ZIP archive while an open writing handle existsrNrTFr<r7iri )r9rwrr rrrlrkrbrrrErAr2rWrjrrfrr<r3r/r4r?rrr>rr)r{r?rrbrsrcdestrrrrFs8      z ZipFile.writecCst|tr|jd}t|tspt|tjtjddd}|j|_|jddkrhd|_ |j dO_ qtd|_ n|}|j st d |j rt d |dk r||_t ||_|j*|j|d d }|j|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?rarrqiAriz7Attempt to write to ZIP archive that was already closedz?Can't write to ZIP archive while an open writing handle exists.r7)rriAi)rr?rr rrrrbr?rir9rwrr.rmrr>r)r{Zzinfo_or_arcnamerSrbrrrrrwritestrps.    zZipFile.writestrcCs |jdS)z2Call the "close()" method in case the user forgot.N)r)r{rrr__del__szZipFile.__del__cCs||jdkrdS|jrtdzB|jdkr\|jr\|j"|jrJ|jj|j|j WdQRXWd|j}d|_|j |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.r7r8r9)r7r8r9) r9rrwrr<rrErAr2_write_end_recordrH)r{r9rrrrs z ZipFile.closecCsrxP|jD]D}|j}|ddd>|dd>B|dB}|dd>|d d>B|ddB}g}|jtksv|jtkr|j|j|j|jd }d }n |j}|j}|jtkr|j|jd }n|j}|j} d} |rt| d} t j d d t |dd t |f|| } t } |j tkr(tt| } n|j tkr>tt| } t| |j} t| |j} yZ|j\} }t j tt| |j| |j||j |||j||t | t | t |jd|j|j|}Wnltk rttt| |j| |j|j |j |||j||t |j!t | t |jd|j|j|ft"j#dYnX|j$j%||j$j%| |j$j%| |j$j%|jq W|j$j&}t |j}||j'}|j'}d}|t(krd}n|tkrd}n|tkrd}|r*|j)st*|dt j t+t,ddddd|||| }|j$j%|t j t-t.d|d}|j$j%|t/|d}t/|d }t/|d }t j t0t1dd||||t |j2 }|j$j%||j$j%|j2|j$j3dS)Nrir$rr!rrr&r lzr)r{rrrrr1rmrlrjZ extra_datarrdrTr?rfr`Zpos2Z centDirCountZ centDirSizeZ centDirOffsetrZ zip64endrecZ zip64locrecrRrrrrs$$                                     zZipFile._write_end_recordcCs*|jd8_|j r&|j r&|jdS)Nr)rBr@r)r{r9rrrrHszZipFile._fpclose)N)N)r6N)F)NN)NNN)NN)N)$rrrrr9rrr|rJrNrrFrfrgrkrmrprrr5r_setterr;r>r{rrrrrrrrrrrrHrrrrr 's<WN     q +  * * $fc@s4eZdZdZdedd fddZdd d Zd d ZdS)r zDClass to create ZIP archives with Python library files and packages.r6TrcCstj|||||d||_dS)N)rrrI)r r| _optimize)r{rrrrIoptimizerrrr|s zPyZipFile.__init__r~NcCstj|}|rF|| rF|jrBtjj|r.dnd}td||fdStjj|\}}tjj|rntjj|d}tjj|r|rd||f}n|}|jrtd|d||j |d d|\}} |jrtd | |j || tj |} | j dx| D]} tjj|| } tjj | \} }tjj| rRtjjtjj| dr|j| ||d q|d kr|r||  r|jrtd| q|j | d d|\}} |jrtd | |j || qWn|jrtd|xtj |D]} tjj|| } tjj | \} }|d kr|r0||  r0|jrtd| q|j | d d|\}} |jrZtd | |j || qWnP|ddd krtd|j |d d|\}} |jrtd| |j || dS)aAdd 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. rrz%s %r skipped by filterfuncNz __init__.pyz%s/%szAdding package inasrrZAdding) filterfuncz.pyzfile %r skipped by filterfunczAdding files from directoryz.Files added with writepy() must end with ".py"z Adding filerrrr)rtrr=rrrQrr0isfile _get_codenamerlistdirremovesplitextwritepyr)r{pathnamebasenamerZlabeldirrZinitnamerrZdirlistr?rrootZextrrrr$sx                zPyZipFile.writepyc sdfdd }|d}|d}tjj|dd}tjj|dd}tjj|dd}jdkr\tjj|rtj|jtj|jkr|} } ntjj|rtj|jtj|jkr|} |} ntjj|rtj|jtj|jkr|} |} nvtjj|rtj|jtj|jkr|} |} nD||rRt j j d kr4|} nt j j dkrH|} n|} |} n|} } njd krr|} |} n<|} jdkr|} n&jdkr|} nd j j} t | tjj| otj| jtj|jks||jd s|} } tjj| 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). rcs^ddl}jrtd|y|j|d|dWn,|jk rX}zt|jdSd}~XnXdS)NrZ CompilingT)doraiserF) py_compiler=rQcompilePyCompileErrormsg)rrrerr)r{rr_compile}s  z)PyZipFile._get_codename.._compilez.pyz.pycr~) optimizationrrz"invalid value for 'optimize': {!r})rz%s/%sr)rr) importlibutilcache_from_sourcerrtrrrrrxrarformatrwr) r{rrrZfile_pyZfile_pycZ pycache_opt0Z pycache_opt1Z pycache_opt2rrrZ archivenamer)r{rrvs`             zPyZipFile._get_codenamer)r~N)rrrrrr|rrrrrrr s  Rc sddl}|jd}|dkr(tjdd}| s:|ddkrLt|tjd|ddkrt|dkrvt|tjdt|dd }|jWdQRXn||ddkr t|dkrt|tjdt|dd }|j }WdQRX|rtd j |td n|ddkrbt|d kr8t|tjdt|dd }|j |dWdQRXn|ddkrt|d krt|tjdfd dt|ddh}x`|ddD]P}t j j|}|st j jt j j|}|dt jt jfkrd}|||qWWdQRXdS)NraE Usage: zipfile.py -l zipfile.zip # Show listing of a zipfile zipfile.py -t zipfile.zip # Test if a zipfile is valid zipfile.py -e zipfile.zip target # Extract zipfile into target dir zipfile.py -c zipfile.zip src ... # Create zipfile from sources r-l-c-e-trr6z.The following enclosed file is corrupted: {!r}z Done testingrcsptjj|r|j||tnPtjj|rl|r8|j||x2tj|D]$}|tjj||tjj||qDWdS)N)rtrrrrrrr0)r.rzippathZnm)addToZiprrrs   zmain..addToZipr7r~)rrrr)textwrapdedentrxargvrQexitr.r rkrmrrrtrrrrr)argsrZUSAGEr.Zbadfilerrr)rrmainsT           r__main__liiiii)N)rrrtreimportlib.utilrrxrrrrCZbinasciirC ImportErrorZdummy_threadingrr rr__all__ Exceptionrr rrrrrtrrrrrzrrrr^rZrYrrXrIrJrKrLrMrNrOr^rRrSrVrXrUrWZ_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_SIZErYr[r\Z_CD_DISK_NUMBER_STARTZ_CD_INTERNAL_FILE_ATTRIBUTESZ_CD_EXTERNAL_FILE_ATTRIBUTESr]rrr|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~rrDrErBrGrHrFZ_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_CENTDIRr1Structr,r6r:r rUr7objectr rrrrrrrrrrrr&r r rrrrrrs@              +=Y= fJy/ B