
    h%                         S r SSKrSSKrSSKrSSKr " S S\R                  5      r " S S5      r " S S\5      r	 " S	 S
\5      r
SrS rS rSS jrg)z
An incremental approach to unzipping files.  This allows you to unzip a little
bit of a file at a time, which means you can report progress as a file unzips.
    Nc                       \ rS rSrSrS rSrg)ChunkingZipFile   zr
A L{zipfile.ZipFile} object which, with L{readfile}, also gives you access
to a file-like object for each entry.
c                    U R                   S;  a  [        S5      eU R                  (       d  [        S5      eU R                  U5      nU R                  R	                  UR
                  S5        U R                  R                  [        R                  5      nUSS [        R                  :w  a  [        R                  " S5      e[        R                  " [        R                  U5      nU R                  R                  U[        R                     5      nU[        R                     (       a,  U R                  R                  U[        R                     5        UR                   S-  (       a  UR#                  S5      nOUR#                  S	5      nXRR$                  :w  a)  [        R                  " S
UR$                  < SU< S35      eUR&                  [        R(                  :X  a  [+        XR,                  5      $ UR&                  [        R.                  :X  a  [1        XR,                  5      $ [        R                  " SUR&                  U4-  5      e)z#
Return file-like object for name.
)razread() requires mode "r" or "a"z3Attempt to read ZIP archive that was already closedr      z Bad magic number for file headeri   zutf-8cp437zFile name in directory "z" and header "z	" differ.z-Unsupported compression method %d for file %s)modeRuntimeErrorfpgetinfoseekheader_offsetreadzipfilesizeFileHeaderstringFileHeader
BadZipFilestructunpackstructFileHeader_FH_FILENAME_LENGTH_FH_EXTRA_FIELD_LENGTH	flag_bitsdecodeorig_filenamecompress_type
ZIP_STOREDZipFileEntrycompress_sizeZIP_DEFLATEDDeflatedZipFileEntry)selfnamezinfofheaderfname	fname_strs         _/root/1688_scrapy/alibaba-scraper/venv/lib/python3.13/site-packages/twisted/python/zipstream.pyreadfileChunkingZipFile.readfile   s    99J&@AAwwTUUT"U((!,'',,w5561Q<7333$$%GHH-- 8 8'BWW%@%@AB7112GGLL!?!?@A??U"W-IW-I+++$$&&	3 
 '"4"44&9&9::  G$8$88'.A.ABB$$?&&-.      N)__name__
__module____qualname____firstlineno____doc__r+   __static_attributes__r.   r-   r*   r   r      s    
*r-   r   c                   X    \ rS rSrSrS rS rS rS rS r	\	r
S rS	 rS
 rS rS rSrg)
_FileEntryC   a  
Abstract superclass of both compressed and uncompressed variants of
file-like objects within a zip archive.

@ivar chunkingZipFile: a chunking zip file.
@type chunkingZipFile: L{ChunkingZipFile}

@ivar length: The number of bytes within the zip file that represent this
file.  (This is the size on disk, not the number of decompressed bytes
which will result from reading it.)

@ivar fp: the underlying file object (that contains pkzip data).  Do not
touch this, please.  It will quite likely move or go away.

@ivar closed: File-like 'closed' attribute; True before this file has been
closed, False after.
@type closed: L{bool}

@ivar finished: An older, broken synonym for 'closed'.  Do not touch this,
please.
@type finished: L{int}
c                 n    Xl         U R                   R                  U l        X l        SU l        SU l        g)z3
Create a L{_FileEntry} from a L{ChunkingZipFile}.
r   FN)chunkingZipFiler   lengthfinishedclosedr$   r9   r:   s      r*   __init___FileEntry.__init__[   s0      /&&))r-   c                     g)z4
Returns false because zip files should not be ttys
Fr.   r$   s    r*   isatty_FileEntry.isattye   s     r-   c                 $    SU l         SU l        U ?g)z
Close self (file-like object)
T   N)r<   r;   r   rA   s    r*   close_FileEntry.closek   s     Gr-   c                 T   ^  Sn[        U 4S jS5       H  nX-  nUS:X  d  M    U$    U$ )z
Read a line.
r-   c                  &   > T R                  S5      $ )NrE   )r   rA   s   r*   <lambda>%_FileEntry.readline.<locals>.<lambda>x   s    1r-      
)iter)r$   linebytes   `  r*   readline_FileEntry.readlines   s9     -s3DLDu}	 4 r-   c                 H    U R                  5       nU(       a  U$ [        5       e)zQ
Implement next as file does (like readline, except raises StopIteration
at EOF)
)rP   StopIteration)r$   nextlines     r*   __next___FileEntry.__next__~   s    
 ==?Oor-   c                     [        U 5      $ )z!
Returns a list of all the lines
)listrA   s    r*   	readlines_FileEntry.readlines   s     Dzr-   c                     U $ z
Returns an iterator (so self)
r.   rA   s    r*   
xreadlines_FileEntry.xreadlines   	     r-   c                     U $ r\   r.   rA   s    r*   __iter___FileEntry.__iter__   r_   r-   c                     U $ Nr.   rA   s    r*   	__enter___FileEntry.__enter__   s    r-   c                 $    U R                  5         g rd   )rF   )r$   exc_type	exc_value	tracebacks       r*   __exit___FileEntry.__exit__   s    

r-   )r9   r<   r;   r   r:   N)r/   r0   r1   r2   r3   r>   rB   rF   rP   rU   nextrY   r]   ra   re   rk   r4   r.   r-   r*   r6   r6   C   s@    .	 Dr-   r6   c                   .    \ rS rSrSrS rS rSS jrSrg)	r       zB
File-like object used to read an uncompressed entry in a ZipFile
c                 >    [         R                  XU5        SU l        g )Nr   )r6   r>   	readBytesr=   s      r*   r>   ZipFileEntry.__init__   s    D6:r-   c                     U R                   $ rd   )rq   rA   s    r*   tellZipFileEntry.tell   s    ~~r-   Nc                    Uc  U R                   U R                  -
  nUS:X  d  U R                  (       a  gU R                  R                  R                  [        XR                   U R                  -
  5      5      nU =R                  [        U5      -  sl        U R                  U R                   :X  d  [        U5      U:  a  SU l        U$ )Nr   r-   rE   )r:   rq   r;   r9   r   r   minlen)r$   ndatas      r*   r   ZipFileEntry.read   s    9dnn,A6T]]##&&++C;;3O,PQ#d)#>>T[[(CIMDMr-   )r;   rq   rd   	r/   r0   r1   r2   r3   r>   rt   r   r4   r.   r-   r*   r    r       s    	r-   r    c                   .    \ rS rSrSrS rS rSS jrSrg)	r#      z=
File-like object used to read a deflated entry in a ZipFile
c                     [         R                  XU5        SU l        SU l        [        R
                  " S5      U l        SU l        g )Nr   ir-   )r6   r>   returnedBytesrq   zlibdecompressobjdecompbufferr=   s      r*   r>   DeflatedZipFileEntry.__init__   s:    D6:((-r-   c                     U R                   $ rd   )r   rA   s    r*   rt   DeflatedZipFileEntry.tell   s    !!!r-   Nc                 <   U R                   (       a  gUGc  U R                  /nUR                  U R                  R	                  U R
                  R                  R                  U R                  U R                  -
  5      5      5        UR                  U R                  R	                  S5      5        UR                  U R                  R                  5       5        SU l        SU l         SR                  U5      nU =R                  [        U5      -  sl        U$ [        U R                  5      U:  Ga'  U R
                  R                  R                  [        USU R                  U R                  -
  5      5      nU =R                  [        U5      -  sl	        U(       dq  U R                  U R                  R	                  S5      -   U R                  R                  5       -   nSU l         SU l        U =R                  [        U5      -  sl        U$ U =R                  U R                  R	                  U5      -  sl        [        U R                  5      U:  a  GM'  U R                  S U nU R                  US  U l        U =R                  [        U5      -  sl        U$ )Nr-      ZrE   i   )r;   r   appendr   
decompressr9   r   r   r:   rq   flushjoinr   rx   rw   )r$   ry   resultrz   s       r*   r   DeflatedZipFileEntry.read   s   ==9F MM&&((++00t~~1MN
 MM$++0067MM$++++-.DKDMXXf%F#f+-Mdkk"Q&++..334t~~!=> #d)+dkk&<&<T&BBT[[EVEVEXX  %&DM"%DK&&#f+5&!MKK4;;#9#9$#??K dkk"Q& [[!_F++ab/DK#f+-Mr-   )r   r   r;   rq   r   rd   r|   r.   r-   r*   r#   r#      s    "&r-   r#   r   c                 j    Sn[        U 5      nUR                  5        H  nU[        XA5      -  nM     U$ )zf
Predict the number of chunks that will be extracted from the entire
zipfile, given chunksize blocks.
r   )r   infolistcountFileChunks)filename	chunksizetotalchunkszfinfos        r*   countZipFileChunksr      s8    
 K		"Bt77 r-   c                 ^    [        U R                  U5      u  p#US:  a  US-  nU=(       d    S$ )a  
Count the number of chunks that will result from the given C{ZipInfo}.

@param zipinfo: a C{zipfile.ZipInfo} instance describing an entry in a zip
archive to be counted.

@return: the number of chunks present in the zip file.  (Even an empty file
counts as one chunk.)
@rtype: L{int}
r   rE   )divmod	file_size)zipinfor   countextras       r*   r   r     s2     '++Y7LEqy
:Ar-   c              #     #    [        U S5      n[        R                  R                  U5      (       d  [        R                  " U5        [        X5      nUR                  5       nUR                  5       n[        Xg5       GH  u  pU	R                  [        -  n
[        R                  R                  X5      nU
(       aE  [        R                  R                  U5      (       d  [        R                  " U5        US-  nUv   M  [        R                  R                  U5      S   n[        R                  R                  U5      (       d  [        R                  " U5        U(       d$  [        R                  R                  U5      (       d  UR                  U5      nU	R                  S:X  a	  US-  nUv   [        US5       nUR!                  5       U	R                  :  aK  UR#                  U5      nUR%                  U5        US-  nUv   UR!                  5       U	R                  :  a  MK  SSS5        GM  U['        X5      -  nUv   GM     g! , (       d  f       GM  = f7f)z
Return a generator for the zipfile.  This implementation will yield after
every chunksize uncompressed bytes, or at the end of a file, whichever
comes first.

The value it yields is the number of chunks left to unzip.
r   rE   r   wbN)r   ospathexistsmakedirsr   namelistr   zipexternal_attrDIR_BITr   splitr+   r   openrt   r   writer   )r   	directory	overwriter   czf	remainingnamesinfosentryr   isdirffdirr   outfilehunks                   r*   unzipIterChunkyr     s     (C
(C77>>)$$
I"87ILLNELLNE5(""W,GGLL*77>>!$$ANIO 77==#A&D77>>$''D!q 1 1\\%(>>Q&NI#O!T]g'')dnn4!wwy1d+!Q	'	 '')dnn4 #] _T==	9 )( #]s   F>I  A(I*#I 
I		I ).r   i   )r3   os.pathr   r   r   r   ZipFiler   r6   r    r#   r   r   r   r   r.   r-   r*   <module>r      se   

    0goo 0f^ ^B: 05: 5p 	"+ r-   