def is_openxml_good(filename: str) -> bool:
    """
    Determines whether an OpenXML file appears to be good (not corrupted).
    """
    try:
        log.debug("Trying: {}", filename)
        with ZipFile(filename, 'r') as zip_ref:
            namelist = zip_ref.namelist()  # type: List[str]
            # log.critical("\n{}", pformat(namelist))
            # -----------------------------------------------------------------
            # Contains key files?
            # -----------------------------------------------------------------
            for mandatory_filename in MANDATORY_FILENAMES:
                if mandatory_filename not in namelist:
                    log.debug("Bad [missing {!r}]: {}",
                              mandatory_filename, filename)
                    return False

            infolist = zip_ref.infolist()  # type: List[ZipInfo]
            contains_docx = False
            contains_pptx = False
            contains_xlsx = False
            for info in infolist:
                # -------------------------------------------------------------
                # Sensible date check?
                # ... NO: lots of perfectly good files have this date/time.
                # -------------------------------------------------------------
                # if info.date_time == NULL_DATE_TIME:
                #     log.debug("{!r}: {!r}", info.filename, info.date_time)

                # -------------------------------------------------------------
                # Only one kind of contents?
                # ... YES, I think so. This has 100% reliability on my
                # stash of 34 PPTX, 223 DOCX, 85 XLSX, and labelled none as bad
                # from an HFC collection of 1866 such files. There are lots of
                # files emerging from Scalpel (plus my find_recovered_openxml
                # zip-fixing tool) that fail this test, though.
                # -------------------------------------------------------------
                if (not contains_docx and
                        DOCX_CONTENTS_REGEX.search(info.filename)):
                    contains_docx = True
                if (not contains_pptx and
                        PPTX_CONTENTS_REGEX.search(info.filename)):
                    contains_pptx = True
                if (not contains_xlsx and
                        XLSX_CONTENTS_REGEX.search(info.filename)):
                    contains_xlsx = True
                if sum([contains_docx, contains_pptx, contains_xlsx]) > 1:
                    log.debug("Bad [>1 of DOCX, PPTX, XLSX content]: {}",
                              filename)
                    return False

            return True
    except (BadZipFile, OSError) as e:
        # ---------------------------------------------------------------------
        # Duff file. Easy!
        # ---------------------------------------------------------------------
        log.debug("Bad [BadZipFile or OSError]: {!r}; error was {!r}",
                  filename, e)
        return False
def is_openxml_good ( filename : str ) -> bool : try : log . debug ( "Trying: {}" , filename ) with ZipFile ( filename , 'r' ) as zip_ref : namelist = zip_ref . namelist ( ) for mandatory_filename in MANDATORY_FILENAMES : if mandatory_filename not in namelist : log . debug ( "Bad [missing {!r}]: {}" , mandatory_filename , filename ) return False infolist = zip_ref . infolist ( ) contains_docx = False contains_pptx = False contains_xlsx = False for info in infolist : if ( not contains_docx and DOCX_CONTENTS_REGEX . search ( info . filename ) ) : contains_docx = True if ( not contains_pptx and PPTX_CONTENTS_REGEX . search ( info . filename ) ) : contains_pptx = True if ( not contains_xlsx and XLSX_CONTENTS_REGEX . search ( info . filename ) ) : contains_xlsx = True if sum ( [ contains_docx , contains_pptx , contains_xlsx ] ) > 1 : log . debug ( "Bad [>1 of DOCX, PPTX, XLSX content]: {}" , filename ) return False return True except ( BadZipFile , OSError ) as e : log . debug ( "Bad [BadZipFile or OSError]: {!r}; error was {!r}" , filename , e ) return False
Determines whether an OpenXML file appears to be good (not corrupted).