'Close the file, and for mode "w" and "a" write the ending
records.'

def close(self):

    if (self.fp is None):
        return
    else:
        if ((self.mode in ('w', 'a')) and self._didModify):
            count = 0
            pos1 = self.fp.tell()
            for zinfo in self.filelist:
                count = (count + 1)
                dt = zinfo.date_time
                dosdate = ((((dt[0] - 1980) << 9) | (dt[1] << 5)) | dt[2])
                dostime = (((dt[3] << 11) | (dt[4] << 5)) | (dt[5] // 2))
                extra = []
                if ((zinfo.file_size > ZIP64_LIMIT) or (zinfo.compress_size > ZIP64_LIMIT)):
                    extra.append(zinfo.file_size)
                    extra.append(zinfo.compress_size)
                    file_size = 4294967295
                    compress_size = 4294967295
                else:
                    file_size = zinfo.file_size
                    compress_size = zinfo.compress_size
                if (zinfo.header_offset > ZIP64_LIMIT):
                    extra.append(zinfo.header_offset)
                    header_offset = 4294967295
                else:
                    header_offset = zinfo.header_offset
                extra_data = zinfo.extra
                if extra:
                    extra_data = (struct.pack(('<HH' + ('Q' * len(extra))), 1, (8 * len(extra)), *extra) + extra_data)
                    extract_version = max(45, zinfo.extract_version)
                    create_version = max(45, zinfo.create_version)
                else:
                    extract_version = zinfo.extract_version
                    create_version = zinfo.create_version
                try:
                    (filename, flag_bits) = zinfo._encodeFilenameFlags()
                    centdir = struct.pack(structCentralDir, stringCentralDir, create_version, zinfo.create_system, extract_version, zinfo.reserved, flag_bits, zinfo.compress_type, dostime, dosdate, zinfo.CRC, compress_size, file_size, len(filename), len(extra_data), len(zinfo.comment), 0, zinfo.internal_attr, zinfo.external_attr, header_offset)
                except DeprecationWarning:
                    print >>sys.stderr, (structCentralDir, stringCentralDir, create_version, zinfo.create_system, extract_version, zinfo.reserved, zinfo.flag_bits, zinfo.compress_type, dostime, dosdate, zinfo.CRC, compress_size, file_size, len(zinfo.filename), len(extra_data), len(zinfo.comment), 0, zinfo.internal_attr, zinfo.external_attr, header_offset)
                    raise
                self.fp.write(centdir)
                self.fp.write(filename)
                self.fp.write(extra_data)
                self.fp.write(zinfo.comment)
            pos2 = self.fp.tell()
            centDirCount = count
            centDirSize = (pos2 - pos1)
            centDirOffset = pos1
            if ((centDirCount >= ZIP_FILECOUNT_LIMIT) or (centDirOffset > ZIP64_LIMIT) or (centDirSize > ZIP64_LIMIT)):
                zip64endrec = struct.pack(structEndArchive64, stringEndArchive64, 44, 45, 45, 0, 0, centDirCount, centDirCount, centDirSize, centDirOffset)
                self.fp.write(zip64endrec)
                zip64locrec = struct.pack(structEndArchive64Locator, stringEndArchive64Locator, 0, pos2, 1)
                self.fp.write(zip64locrec)
                centDirCount = min(centDirCount, 65535)
                centDirSize = min(centDirSize, 4294967295)
                centDirOffset = min(centDirOffset, 4294967295)
            if (len(self.comment) >= ZIP_MAX_COMMENT):
                if (self.debug > 0):
                    msg = ('Archive    comment    is    too    long;    truncating    to    %d    bytes' % ZIP_MAX_COMMENT)
                self.comment = self.comment[:ZIP_MAX_COMMENT]
            endrec = struct.pack(structEndArchive, stringEndArchive, 0, 0, centDirCount, centDirCount, centDirSize, centDirOffset, len(self.comment))
            self.fp.write(endrec)
            self.fp.write(self.comment)
            self.fp.flush()
        if (not self._filePassed):
            self.fp.close()
        self.fp = None
        return
