PclZip::privWriteCentralFileHeader PHP Method

privWriteCentralFileHeader() public method

--------------------------------------------------------------------------------
public privWriteCentralFileHeader ( &$p_header )
    public function privWriteCentralFileHeader(&$p_header)
    {
        $v_result = 1;
        // TBC
        //for(reset($p_header); $key = key($p_header); next($p_header)) {
        //}
        // ----- Transform UNIX mtime to DOS format mdate/mtime
        $v_date = getdate($p_header['mtime']);
        $v_mtime = ($v_date['hours'] << 11) + ($v_date['minutes'] << 5) + $v_date['seconds'] / 2;
        $v_mdate = ($v_date['year'] - 1980 << 9) + ($v_date['mon'] << 5) + $v_date['mday'];
        // ----- Packed data
        $v_binary_data = pack('VvvvvvvVVVvvvvvVV', 0x2014b50, $p_header['version'], $p_header['version_extracted'], $p_header['flag'], $p_header['compression'], $v_mtime, $v_mdate, $p_header['crc'], $p_header['compressed_size'], $p_header['size'], strlen($p_header['stored_filename']), $p_header['extra_len'], $p_header['comment_len'], $p_header['disk'], $p_header['internal'], $p_header['external'], $p_header['offset']);
        // ----- Write the 42 bytes of the header in the zip file
        fwrite($this->zip_fd, $v_binary_data, 46);
        // ----- Write the variable fields
        if (strlen($p_header['stored_filename']) != 0) {
            fwrite($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
        }
        if ($p_header['extra_len'] != 0) {
            fwrite($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
        }
        if ($p_header['comment_len'] != 0) {
            fwrite($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
        }
        // ----- Return
        return $v_result;
    }

Usage Example

Example #1
0
 function privDeleteByRule(&$p_result_list, &$p_options)
 {
     //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privDeleteByRule", "");
     $v_result = 1;
     $v_list_detail = array();
     // ----- Open the zip file
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
     if (($v_result = $this->privOpenFd('rb')) != 1) {
         // ----- Return
         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
         return $v_result;
     }
     // ----- Read the central directory informations
     $v_central_dir = array();
     if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1) {
         $this->privCloseFd();
         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
         return $v_result;
     }
     // ----- Go to beginning of File
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
     @rewind($this->zip_fd);
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
     // ----- Scan all the files
     // ----- Start at beginning of Central Dir
     $v_pos_entry = $v_central_dir['offset'];
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
     @rewind($this->zip_fd);
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
     if (@fseek($this->zip_fd, $v_pos_entry)) {
         // ----- Close the zip file
         $this->privCloseFd();
         // ----- Error log
         PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
         // ----- Return
         //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
         return PclZip::errorCode();
     }
     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
     // ----- Read each entry
     $v_header_list = array();
     $j_start = 0;
     for ($i = 0, $v_nb_extracted = 0; $i < $v_central_dir['entries']; $i++) {
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry (index '$i')");
         // ----- Read the file header
         $v_header_list[$v_nb_extracted] = array();
         if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1) {
             // ----- Close the zip file
             $this->privCloseFd();
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
             return $v_result;
         }
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename (index '$i') : '".$v_header_list[$v_nb_extracted]['stored_filename']."'");
         // ----- Store the index
         $v_header_list[$v_nb_extracted]['index'] = $i;
         // ----- Look for the specific extract rules
         $v_found = false;
         // ----- Look for extract by name rule
         if (isset($p_options[PCLZIP_OPT_BY_NAME]) && $p_options[PCLZIP_OPT_BY_NAME] != 0) {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
             // ----- Look if the filename is in the list
             for ($j = 0; $j < sizeof($p_options[PCLZIP_OPT_BY_NAME]) && !$v_found; $j++) {
                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
                 // ----- Look for a directory
                 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
                     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
                     // ----- Look if the directory is in the filename path
                     if (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]) && substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
                         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");
                         $v_found = true;
                     } elseif (($v_header_list[$v_nb_extracted]['external'] & 0x10) == 0x10 && $v_header_list[$v_nb_extracted]['stored_filename'] . '/' == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
                         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The entry is the searched directory");
                         $v_found = true;
                     }
                 } elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
                     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The file is the right one.");
                     $v_found = true;
                 }
             }
         } else {
             if (isset($p_options[PCLZIP_OPT_BY_EREG]) && $p_options[PCLZIP_OPT_BY_EREG] != "") {
                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract by ereg '".$p_options[PCLZIP_OPT_BY_EREG]."'");
                 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
                     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
                     $v_found = true;
                 }
             } else {
                 if (isset($p_options[PCLZIP_OPT_BY_PREG]) && $p_options[PCLZIP_OPT_BY_PREG] != "") {
                     //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByEreg'");
                     if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
                         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filename match the regular expression");
                         $v_found = true;
                     }
                 } else {
                     if (isset($p_options[PCLZIP_OPT_BY_INDEX]) && $p_options[PCLZIP_OPT_BY_INDEX] != 0) {
                         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByIndex'");
                         // ----- Look if the index is in the list
                         for ($j = $j_start; $j < sizeof($p_options[PCLZIP_OPT_BY_INDEX]) && !$v_found; $j++) {
                             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Look if index '$i' is in [".$p_options[PCLZIP_OPT_BY_INDEX][$j]['start'].",".$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']."]");
                             if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] && $i <= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
                                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Found as part of an index range");
                                 $v_found = true;
                             }
                             if ($i >= $p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
                                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Do not look this index range for next loop");
                                 $j_start = $j + 1;
                             }
                             if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start'] > $i) {
                                 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Index range is greater than index, stop loop");
                                 break;
                             }
                         }
                     } else {
                         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "No argument mean remove all file");
                         $v_found = true;
                     }
                 }
             }
         }
         // ----- Look for deletion
         if ($v_found) {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' need to be deleted");
             unset($v_header_list[$v_nb_extracted]);
         } else {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_header_list[$v_nb_extracted]['stored_filename']."', index '$i' will not be deleted");
             $v_nb_extracted++;
         }
     }
     // ----- Look if something need to be deleted
     if ($v_nb_extracted > 0) {
         // ----- Creates a temporay file
         $v_zip_temp_name = PCLZIP_TEMPORARY_DIR . uniqid('pclzip-') . '.tmp';
         // ----- Creates a temporary zip archive
         $v_temp_zip = new PclZip($v_zip_temp_name);
         // ----- Open the temporary zip file in write mode
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary write mode");
         if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
             $this->privCloseFd();
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
             return $v_result;
         }
         // ----- Look which file need to be kept
         for ($i = 0; $i < sizeof($v_header_list); $i++) {
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Keep entry index '$i' : '".$v_header_list[$i]['filename']."'");
             // ----- Calculate the position of the header
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset='". $v_header_list[$i]['offset']."'");
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position before rewind : ".ftell($this->zip_fd)."'");
             @rewind($this->zip_fd);
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after rewind : ".ftell($this->zip_fd)."'");
             if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
                 // ----- Close the zip file
                 $this->privCloseFd();
                 $v_temp_zip->privCloseFd();
                 @unlink($v_zip_temp_name);
                 // ----- Error log
                 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
                 // ----- Return
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
                 return PclZip::errorCode();
             }
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position after fseek : ".ftell($this->zip_fd)."'");
             // ----- Read the file header
             $v_local_header = array();
             if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
                 // ----- Close the zip file
                 $this->privCloseFd();
                 $v_temp_zip->privCloseFd();
                 @unlink($v_zip_temp_name);
                 // ----- Return
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
             // ----- Check that local file header is same as central file header
             if ($this->privCheckFileHeaders($v_local_header, $v_header_list[$i]) != 1) {
                 // TBC
             }
             unset($v_local_header);
             // ----- Write the file header
             if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
                 // ----- Close the zip file
                 $this->privCloseFd();
                 $v_temp_zip->privCloseFd();
                 @unlink($v_zip_temp_name);
                 // ----- Return
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset for this file is '".$v_header_list[$i]['offset']."'");
             // ----- Read/write the data block
             if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
                 // ----- Close the zip file
                 $this->privCloseFd();
                 $v_temp_zip->privCloseFd();
                 @unlink($v_zip_temp_name);
                 // ----- Return
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
         }
         // ----- Store the offset of the central dir
         $v_offset = @ftell($v_temp_zip->zip_fd);
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "New offset of central dir : $v_offset");
         // ----- Re-Create the Central Dir files header
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the new central directory");
         for ($i = 0; $i < sizeof($v_header_list); $i++) {
             // ----- Create the file header
             //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Offset of file : ".$v_header_list[$i]['offset']);
             if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
                 $v_temp_zip->privCloseFd();
                 $this->privCloseFd();
                 @unlink($v_zip_temp_name);
                 // ----- Return
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
             // ----- Transform the header to a 'usable' info
             $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
         }
         //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Creates the central directory footer");
         // ----- Zip file comment
         $v_comment = '';
         if (isset($p_options[PCLZIP_OPT_COMMENT])) {
             $v_comment = $p_options[PCLZIP_OPT_COMMENT];
         }
         // ----- Calculate the size of the central header
         $v_size = @ftell($v_temp_zip->zip_fd) - $v_offset;
         // ----- Create the central dir footer
         if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
             // ----- Reset the file list
             unset($v_header_list);
             $v_temp_zip->privCloseFd();
             $this->privCloseFd();
             @unlink($v_zip_temp_name);
             // ----- Return
             //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
             return $v_result;
         }
         // ----- Close
         $v_temp_zip->privCloseFd();
         $this->privCloseFd();
         // ----- Delete the zip file
         // TBC : I should test the result ...
         @unlink($this->zipname);
         // ----- Rename the temporary file
         // TBC : I should test the result ...
         //@rename($v_zip_temp_name, $this->zipname);
         PclZipUtilRename($v_zip_temp_name, $this->zipname);
         // ----- Destroy the temporary archive
         unset($v_temp_zip);
     } else {
         if ($v_central_dir['entries'] != 0) {
             $this->privCloseFd();
             if (($v_result = $this->privOpenFd('wb')) != 1) {
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
             if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
                 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
                 return $v_result;
             }
             $this->privCloseFd();
         }
     }
     // ----- Return
     //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
     return $v_result;
 }
All Usage Examples Of PclZip::privWriteCentralFileHeader