PhpConcept

Developers Tools

  • Increase font size
  • Default font size
  • Decrease font size
Home PclZip User Guide

PclZip User Guide - PCLZIP_CB_PRE_EXTRACT

Print PDF

PCLZIP_CB_PRE_EXTRACT

This optional arguments gives you the ability to add a specific processing while extrating an archive by calling a "call-back" function before the extraction of each archived file. The call-back function PCLZIP_CB_PRE_EXTRACT can modify the extraction of the concerned file in two ways :
- by modifying the path or the name of the extracted file,
- by skipping the extraction of the file and go to the next one.

To be precise, the call-back function is applied after the optional arguments PCLZIP_OPT_PATH, PCLZIP_OPT_ADD_PATH, PCLZIP_OPT_REMOVE_PATH or PCLZIP_OPT_REMOVE_ALL_PATH, but before the coherence check (file does not exist, file is not an existing folder, ...).

The 'call-back' function, given as a value of the argument, must respect the following synopsis :

function myCallBack($p_event, &$p_header)
{
[... Your specific code ...]
return $result;
}

When the method call the call-back function, it gives the following arguments :
- $p_event : the identity of the call-back argument (here PCLZIP_CB_PRE_EXTRACT). This is usefull when you want to use the same function for different call-back actions.
- $p_header : the description of the file that will be extracted. This is an array which contains informations in several fields. The most interesting are the archived file name and the destination file name of the extracted file. The array fields are described in the chapter 'Returned Values'.
The function can only modify the field 'filename' of $p_header array. This field is the destination filename of the extracted file. This gives the ability to change the destination of the extracted file. The other fields are read-only.
The function must return 2, 1 or 0 ($result). Other values are reserved for futur use. If the function returns 1, then the extraction is resumed (with the potentially modified destination filename). If the result is 0 the file extraction is skipped, and the method process will go for the next file to extract. If the result is 2, the file extraction is skipped and the extraction normally stop before extracting the end of the archive.

  function myPreExtractCallBack($p_event, &$p_header)
  {
    
$info pathinfo($p_header['filename']);
    
// ----- gif files are skipped
    
if ($info['extension'] == 'gif') {
      return 
0;
    }
    
// ----- jpg files are extracted in images folder
    
else if ($info['extension'] == 'jpg') {
      
$p_header['filename'] = 'images/'.$info['basename'];
      return 
1;
    }
    
// ----- all other files are simply extracted
    
else {
      return 
1;
    }
  }

  
$list $archive->extract(PCLZIP_OPT_PATH'folder',);
                            
PCLZIP_CB_PRE_EXTRACT'myPreExtractCallBack'); 

In this sample the call-back function will skip the extraction of 'gif' files, and will extract the 'jpg' files in the 'images' folder (from the current path). All other files are normally extracted in the 'folder' folder.

 

[Optional Arguments List]

 

Last Updated on Friday, 18 December 2009 13:48