(self, file, mode="r", compression=ZIP_STORED, allowZip64=False)
Open the ZIP file with mode read "r", write "w" or append "a".
Open the ZIP file with mode read "r", write "w" or append "a".
Open the ZIP file with mode read r write w or append a .
def __init__(self, file, mode="r", compression=ZIP_STORED, allowZip64=False):
        """Open the ZIP file with mode read "r", write "w" or append "a"."""
        if mode not in ("r", "w", "a"):
            raise RuntimeError('ZipFile() requires mode "r", "w", or "a"')

        if compression == ZIP_STORED:
            pass
        elif compression == ZIP_DEFLATED:
            if not zlib:
                raise RuntimeError(
                      "Compression requires the (missing) zlib module")
        else:
            raise RuntimeError("That compression method is not supported")

        self._allowZip64 = allowZip64
        self._didModify = False
        self.debug = 0  # Level of printing: 0 through 3
        self.NameToInfo = {}    # Find file info given name
        self.filelist = []      # List of ZipInfo instances for archive
        self.compression = compression  # Method of compression
        self.mode = key = mode.replace('b', '')[0]
        self.pwd = None
        self.comment = b''

        # Check if we were passed a file-like object
        if isinstance(file, str):
            # No, it's a filename
            self._filePassed = 0
            self.filename = file
            modeDict = {'r' : 'rb', 'w': 'wb', 'a' : 'r+b'}
            try:
                self.fp = io.open(file, modeDict[mode])
            except IOError:
                if mode == 'a':
                    mode = key = 'w'
                    self.fp = io.open(file, modeDict[mode])
                else:
                    raise
        else:
            self._filePassed = 1
            self.fp = file
            self.filename = getattr(file, 'name', None)

        if key == 'r':
            self._GetContents()
        elif key == 'w':
            # set the modified flag so central directory gets written
            # even if no files are added to the archive
            self._didModify = True
        elif key == 'a':
            try:
                # See if file is a zip file
                self._RealGetContents()
                # seek to start of directory and overwrite
                self.fp.seek(self.start_dir, 0)
            except BadZipFile:
                # file is not a zip file, just append
                self.fp.seek(0, 2)

                # set the modified flag so central directory gets written
                # even if no files are added to the archive
                self._didModify = True
        else:
            if not self._filePassed:
                self.fp.close()
                self.fp = None
            raise RuntimeError('Mode must be "r", "w" or "a"')
def __init__ ( self , file , mode = "r" , compression = ZIP_STORED , allowZip64 = False ) : if mode not in ( "r" , "w" , "a" ) : raise RuntimeError ( 'ZipFile() requires mode "r", "w", or "a"' ) if compression == ZIP_STORED : pass elif compression == ZIP_DEFLATED : if not zlib : raise RuntimeError ( "Compression requires the (missing) zlib module" ) else : raise RuntimeError ( "That compression method is not supported" ) self . _allowZip64 = allowZip64 self . _didModify = False self . debug = 0 # Level of printing: 0 through 3 self . NameToInfo = { } # Find file info given name self . filelist = [ ] # List of ZipInfo instances for archive self . compression = compression # Method of compression self . mode = key = mode . replace ( 'b' , '' ) [ 0 ] self . pwd = None self . comment = b'' # Check if we were passed a file-like object if isinstance ( file , str ) : # No, it's a filename self . _filePassed = 0 self . filename = file modeDict = { 'r' : 'rb' , 'w' : 'wb' , 'a' : 'r+b' } try : self . fp = io . open ( file , modeDict [ mode ] ) except IOError : if mode == 'a' : mode = key = 'w' self . fp = io . open ( file , modeDict [ mode ] ) else : raise else : self . _filePassed = 1 self . fp = file self . filename = getattr ( file , 'name' , None ) if key == 'r' : self . _GetContents ( ) elif key == 'w' : # set the modified flag so central directory gets written # even if no files are added to the archive self . _didModify = True elif key == 'a' : try : # See if file is a zip file self . _RealGetContents ( ) # seek to start of directory and overwrite self . fp . seek ( self . start_dir , 0 ) except BadZipFile : # file is not a zip file, just append self . fp . seek ( 0 , 2 ) # set the modified flag so central directory gets written # even if no files are added to the archive self . _didModify = True else : if not self . _filePassed : self . fp . close ( ) self . fp = None raise RuntimeError ( 'Mode must be "r", "w" or "a"' )