Contao\ZipWriter::close PHP Method

close() public method

Write the central directory and close the file handle
public close ( )
    public function close()
    {
        // Add archive header
        $arrArchive['archive_signature'] = self::CENTRAL_DIR_END;
        $arrArchive['number_of_this_disk'] = "";
        $arrArchive['number_of_disk_with_cd'] = "";
        $arrArchive['total_cd_entries_disk'] = pack('v', $this->intCount);
        $arrArchive['total_cd_entries'] = pack('v', $this->intCount);
        $arrArchive['size_of_cd'] = pack('V', strlen($this->strCentralDir));
        $arrArchive['offset_start_cd'] = pack('V', @ftell($this->resFile));
        $arrArchive['zipfile_comment_length'] = "";
        $arrArchive['zipfile_comment'] = '';
        // Add central directory and archive header (do not change this order)
        fputs($this->resFile, $this->strCentralDir);
        fputs($this->resFile, implode('', $arrArchive));
        // Close the file before renaming it
        fclose($this->resFile);
        // Check if target file exists
        if (!file_exists(TL_ROOT . '/' . $this->strFile)) {
            // Handle open_basedir restrictions
            if (($strFolder = dirname($this->strFile)) == '.') {
                $strFolder = '';
            }
            // Create folder
            if (!is_dir(TL_ROOT . '/' . $strFolder)) {
                new \Folder($strFolder);
            }
        }
        // Rename file
        \Files::getInstance()->rename(self::TEMPORARY_FOLDER . '/' . basename($this->strTemp), $this->strFile);
    }

Usage Example

 protected function exportToDownload()
 {
     $strTmpFile = 'system/tmp/' . $this->strFilename;
     $strTmpFolder = str_replace('.' . $this->compressionType, '', $strTmpFile);
     $arrExportFields = array();
     $arrDca = $GLOBALS['TL_DCA'][$this->linkedTable]['fields'];
     foreach (deserialize($this->tableFieldsForExport, true) as $strField) {
         if (strpos($strField, EXPORTER_RAW_FIELD_SUFFIX) !== false) {
             $arrExportFields[] = str_replace(EXPORTER_RAW_FIELD_SUFFIX, '', $strField) . ' AS ' . $strField;
         } else {
             $arrExportFields[] = $strField;
         }
     }
     $objDbResult = \Database::getInstance()->prepare("SELECT " . implode(',', $arrExportFields) . " FROM " . $this->linkedTable)->execute();
     if (!$objDbResult->numRows > 0) {
         return;
     }
     switch ($this->compressionType) {
         default:
             $objZip = new ZipWriter($strTmpFile);
             break;
     }
     // write files
     while ($objDbResult->next()) {
         $arrRow = $objDbResult->row();
         foreach ($arrRow as $key => $varValue) {
             $objDc = new DC_Table($this->linkedTable);
             $objDc->activeRecord = $objDbResult;
             $varValue = FormSubmission::prepareSpecialValueForPrint($varValue, $arrDca['fields'][$key], $this->linkedTable, $objDc);
             if (!is_array($varValue)) {
                 $varValue = array($varValue);
             }
             foreach ($varValue as $strPath) {
                 if ($strPath && ($objFile = new \File($strPath, true)) !== null && $objFile->exists()) {
                     if (isset($GLOBALS['TL_HOOKS']['exporter_modifyMediaFilename']) && is_array($GLOBALS['TL_HOOKS']['exporter_modifyMediaFilename'])) {
                         foreach ($GLOBALS['TL_HOOKS']['exporter_modifyMediaFilename'] as $callback) {
                             $objCallback = \System::importStatic($callback[0]);
                             $strFixedFilename = $objCallback->{$callback}[1]($objFile, $key, $strPath, $this);
                             if ($strFixedFilename) {
                                 $strTmpFixedFilename = $strTmpFolder . '/' . ltrim($strFixedFilename, '/');
                                 $objFile->copyTo($strTmpFixedFilename);
                                 $objFile->path = $strTmpFixedFilename;
                             }
                         }
                     }
                     switch ($this->compressionType) {
                         default:
                             $objZip->addFile($objFile->path);
                             break;
                     }
                 }
             }
         }
     }
     switch ($this->compressionType) {
         default:
             $objZip->close();
             break;
     }
     $objTmpFolder = new \Folder($strTmpFolder);
     if (is_dir(TL_ROOT . '/' . $objTmpFolder->path)) {
         $objTmpFolder->delete();
     }
     $objFile = new \File($strTmpFile);
     $objFile->sendToBrowser();
 }