protected function extract_versionphp_file($zipfilepath, $targetdir) {
        global $CFG;
        require_once($CFG->libdir.'/filelib.php');

        $fp = get_file_packer('application/zip');
        $files = $fp->list_files($zipfilepath);

        if (empty($files)) {
            return false;
        }

        $rootdirname = null;
        $found = null;

        foreach ($files as $file) {
            // Valid plugin ZIP package has just one root directory with all
            // files in it.
            $pathnameitems = explode('/', $file->pathname);

            if (empty($pathnameitems)) {
                return false;
            }

            // Set the expected name of the root directory in the first
            // iteration of the loop.
            if ($rootdirname === null) {
                $rootdirname = $pathnameitems[0];
            }

            // Require the same root directory for all files in the ZIP
            // package.
            if ($rootdirname !== $pathnameitems[0]) {
                return false;
            }

            // If we reached the valid version.php file, remember it.
            if ($pathnameitems[1] === 'version.php' and !$file->is_directory and $file->size > 0) {
                $found = $file->pathname;
            }
        }

        if (empty($found)) {
            return false;
        }

        $extracted = $fp->extract_to_pathname($zipfilepath, $targetdir, array($found));

        if (empty($extracted)) {
            return false;
        }

        // The following syntax uses function array dereferencing, added in PHP 5.4.0.
        return array_keys($extracted)[0];
    }