(self, name, mode="r", pwd=None)
return ZipExtFile(zef_file, mode, zinfo, zd,
                          close_fileobj=not self._filePassed)
Return file-like object for 'name'.
Return file-like object for 'name'.
Return file - like object for name .
def open(self, name, mode="r", pwd=None):
        """Return file-like object for 'name'."""
        if mode not in ("r", "U", "rU"):
            raise RuntimeError('open() requires mode "r", "U", or "rU"')
        if pwd and not isinstance(pwd, bytes):
            raise TypeError("pwd: expected bytes, got %s" % type(pwd))
        if not self.fp:
            raise RuntimeError(
                  "Attempt to read ZIP archive that was already closed")

        # Only open a new file for instances where we were not
        # given a file object in the constructor
        if self._filePassed:
            zef_file = self.fp
        else:
            zef_file = io.open(self.filename, 'rb')

        # Make sure we have an info object
        if isinstance(name, ZipInfo):
            # 'name' is already an info object
            zinfo = name
        else:
            # Get info object for name
            try:
                zinfo = self.getinfo(name)
            except KeyError:
                if not self._filePassed:
                    zef_file.close()
                raise
        zef_file.seek(zinfo.header_offset, 0)

        # Skip the file header:
        fheader = zef_file.read(sizeFileHeader)
        if fheader[0:4] != stringFileHeader:
            raise BadZipFile("Bad magic number for file header")

        fheader = struct.unpack(structFileHeader, fheader)
        fname = zef_file.read(fheader[_FH_FILENAME_LENGTH])
        if fheader[_FH_EXTRA_FIELD_LENGTH]:
            zef_file.read(fheader[_FH_EXTRA_FIELD_LENGTH])

        if zinfo.flag_bits & 0x800:
            # UTF-8 filename
            fname_str = fname.decode("utf-8")
        else:
            fname_str = fname.decode("cp437")

        if fname_str != zinfo.orig_filename:
            if not self._filePassed:
                zef_file.close()
            raise BadZipFile(
                  'File name in directory %r and header %r differ.'
                  % (zinfo.orig_filename, fname))

        # check for encrypted flag & handle password
        is_encrypted = zinfo.flag_bits & 0x1
        zd = None
        if is_encrypted:
            if not pwd:
                pwd = self.pwd
            if not pwd:
                if not self._filePassed:
                    zef_file.close()
                raise RuntimeError("File %s is encrypted, "
                                   "password required for extraction" % name)

            zd = _ZipDecrypter(pwd)
            # The first 12 bytes in the cypher stream is an encryption header
            #  used to strengthen the algorithm. The first 11 bytes are
            #  completely random, while the 12th contains the MSB of the CRC,
            #  or the MSB of the file time depending on the header type
            #  and is used to check the correctness of the password.
            header = zef_file.read(12)
            h = list(map(zd, header[0:12]))
            if zinfo.flag_bits & 0x8:
                # compare against the file type from extended local headers
                check_byte = (zinfo._raw_time >> 8) & 0xff
            else:
                # compare against the CRC otherwise
                check_byte = (zinfo.CRC >> 24) & 0xff
            if h[11] != check_byte:
                if not self._filePassed:
                    zef_file.close()
                raise RuntimeError("Bad password for file", name)

        return ZipExtFile(zef_file, mode, zinfo, zd,
                          close_fileobj=not self._filePassed)
def open ( self , name , mode = "r" , pwd = None ) : if mode not in ( "r" , "U" , "rU" ) : raise RuntimeError ( 'open() requires mode "r", "U", or "rU"' ) if pwd and not isinstance ( pwd , bytes ) : raise TypeError ( "pwd: expected bytes, got %s" % type ( pwd ) ) if not self . fp : raise RuntimeError ( "Attempt to read ZIP archive that was already closed" ) # Only open a new file for instances where we were not # given a file object in the constructor if self . _filePassed : zef_file = self . fp else : zef_file = io . open ( self . filename , 'rb' ) # Make sure we have an info object if isinstance ( name , ZipInfo ) : # 'name' is already an info object zinfo = name else : # Get info object for name try : zinfo = self . getinfo ( name ) except KeyError : if not self . _filePassed : zef_file . close ( ) raise zef_file . seek ( zinfo . header_offset , 0 ) # Skip the file header: fheader = zef_file . read ( sizeFileHeader ) if fheader [ 0 : 4 ] != stringFileHeader : raise BadZipFile ( "Bad magic number for file header" ) fheader = struct . unpack ( structFileHeader , fheader ) fname = zef_file . read ( fheader [ _FH_FILENAME_LENGTH ] ) if fheader [ _FH_EXTRA_FIELD_LENGTH ] : zef_file . read ( fheader [ _FH_EXTRA_FIELD_LENGTH ] ) if zinfo . flag_bits & 0x800 : # UTF-8 filename fname_str = fname . decode ( "utf-8" ) else : fname_str = fname . decode ( "cp437" ) if fname_str != zinfo . orig_filename : if not self . _filePassed : zef_file . close ( ) raise BadZipFile ( 'File name in directory %r and header %r differ.' % ( zinfo . orig_filename , fname ) ) # check for encrypted flag & handle password is_encrypted = zinfo . flag_bits & 0x1 zd = None if is_encrypted : if not pwd : pwd = self . pwd if not pwd : if not self . _filePassed : zef_file . close ( ) raise RuntimeError ( "File %s is encrypted, " "password required for extraction" % name ) zd = _ZipDecrypter ( pwd ) # The first 12 bytes in the cypher stream is an encryption header #  used to strengthen the algorithm. The first 11 bytes are #  completely random, while the 12th contains the MSB of the CRC, #  or the MSB of the file time depending on the header type #  and is used to check the correctness of the password. header = zef_file . read ( 12 ) h = list ( map ( zd , header [ 0 : 12 ] ) ) if zinfo . flag_bits & 0x8 : # compare against the file type from extended local headers check_byte = ( zinfo . _raw_time >> 8 ) & 0xff else : # compare against the CRC otherwise check_byte = ( zinfo . CRC >> 24 ) & 0xff if h [ 11 ] != check_byte : if not self . _filePassed : zef_file . close ( ) raise RuntimeError ( "Bad password for file" , name ) return ZipExtFile ( zef_file , mode , zinfo , zd , close_fileobj = not self . _filePassed )