src/snac/util/EADParser.php
/**
     * Unzip a zip file
     *
     * Unzips the file given as a parameter to a temporary directory and
     * returns the location of the zip contents.
     *
     * @param $zipcontents string the contents of the zip file (not filename)
     * @return string The path to the folder containing the zip contents
     */
Unzip a zip file
Unzips the file given as a parameter to a temporary directory and
returns the location of the zip contents.

@param $zipcontents string the contents of the zip file (not filename)
@return string The path to the folder containing the zip contents
Unzip a zip file Unzips the file given as a parameter to a temporary directory and returns the location of the zip contents . @param $zipcontents string the contents of the zip file ( not filename ) @return string The path to the folder containing the zip contents
private function unzip($zipcontents) {
        $tmpdir = \snac\Config::$EAD_PARSETMP_DIR . "/". microtime(true);
        $this->logger->addDebug("creating tmp directory");
        mkdir($tmpdir);
        $infile = $tmpdir."/upload.zip";
        $eaddir = $tmpdir."/ead/";
        mkdir($eaddir);
        $errors = [];
        try {
            $this->logger->addDebug("Writing zip contents");
            file_put_contents($infile, $zipcontents);
            $this->logger->addDebug("Unzipping");
            $zip = new \ZipArchive();
            $result = $zip->open($infile, \ZipArchive::CHECKCONS);
            if ($result !== true) {
                switch($result) {
                case \ZipArchive::ER_NOZIP:
                    throw new \Exception('Uploaded file is not a zip archive.');
                case \ZipArchive::ER_INCONS :
                    $result = $zip->open($infile);
                    if ($result === true)
                        break;
                    throw new \Exception('Uploaded file failed consistency check.');
                case \ZipArchive::ER_CRC :
                    throw new \Exception('Uploaded file failed checksum.');
                default:
                    throw new \Exception('An error occurred: ' . $result);
                }
            }
            for($i = 0; $i < $zip->numFiles; $i++) {
                $innerPath = $zip->getNameIndex($i);
                $fileinfo = pathinfo($innerPath);
                if (strpos($innerPath, 'MACOSX') === false && isset($fileinfo['extension']) &&
                        ($fileinfo['extension'] == 'xml' || $fileinfo['extension'] == 'XML'))
                    file_put_contents($eaddir.$fileinfo['basename'], $zip->getFromIndex($i));
            }
            $zip->close();
            $this->logger->addDebug("Unzipped");
        } catch (\Exception $e) {
            $this->cleanup($tmpdir);
            throw new \snac\exceptions\SNACEADParserException($e);
        }
        return $tmpdir;
    }
private function unzip ( $ zipcontents ) { $ tmpdir = \ snac \ Config :: $ EAD_PARSETMP_DIR . " / " . microtime ( true ) ; $ this -> logger -> addDebug ( " creating tmp directory " ) ; mkdir ( $ tmpdir ) ; $ infile = $ tmpdir . " /upload.zip " ; $ eaddir = $ tmpdir . " /ead/ " ; mkdir ( $ eaddir ) ; $ errors = [ ] ; try { $ this -> logger -> addDebug ( " Writing zip contents " ) ; file_put_contents ( $ infile , $ zipcontents ) ; $ this -> logger -> addDebug ( " Unzipping " ) ; $ zip = new \ ZipArchive ( ) ; $ result = $ zip -> open ( $ infile , \ ZipArchive :: CHECKCONS ) ; if ( $ result !== true ) { switch ( $ result ) { case \ ZipArchive :: ER_NOZIP : throw new \ Exception ( 'Uploaded file is not a zip archive.' ) ; case \ ZipArchive :: ER_INCONS : $ result = $ zip -> open ( $ infile ) ; if ( $ result === true ) break ; throw new \ Exception ( 'Uploaded file failed consistency check.' ) ; case \ ZipArchive :: ER_CRC : throw new \ Exception ( 'Uploaded file failed checksum.' ) ; default : throw new \ Exception ( 'An error occurred: ' . $ result ) ; } } for ( $ i = 0 ; $ i < $ zip -> numFiles ; $ i ++ ) { $ innerPath = $ zip -> getNameIndex ( $ i ) ; $ fileinfo = pathinfo ( $ innerPath ) ; if ( strpos ( $ innerPath , 'MACOSX' ) === false && isset ( $ fileinfo [ 'extension' ] ) && ( $ fileinfo [ 'extension' ] == 'xml' || $ fileinfo [ 'extension' ] == 'XML' ) ) file_put_contents ( $ eaddir . $ fileinfo [ 'basename' ] , $ zip -> getFromIndex ( $ i ) ) ; } $ zip -> close ( ) ; $ this -> logger -> addDebug ( " Unzipped " ) ; } catch ( \ Exception $ e ) { $ this -> cleanup ( $ tmpdir ) ; throw new \ snac \ exceptions \ SNACEADParserException ( $ e ) ; } return $ tmpdir ; }
Unzip a zip file
 Unzips the file given as a parameter to a temporary directory and
 returns the location of the zip contents.
Unzip a zip file Unzips the file given as a parameter to a temporary directory and returns the location of the zip contents .
// NOTE: Mac OS Zip files seem to fail consistency checks // It works to ignore this check, but we should NOT let everyone upload // files -- only trusted sources! // Workaround for Mac zip files -- if they are inconsistent, that's okay // Loop through all files in the zip and find those that have xml/XML extension // check for MACOSX folder, too.