2 // --------------------------------------------------------------------------------
3 // PhpConcept Library - Zip Module 2.8.2
4 // --------------------------------------------------------------------------------
5 // License GNU/LGPL - Vincent Blavet - August 2009
6 // http://www.phpconcept.net
7 // --------------------------------------------------------------------------------
10 // PclZip is a PHP library that manage ZIP archives.
11 // So far tests show that archives generated by PclZip are readable by
12 // WinZip application and other tools.
15 // See readme.txt and http://www.phpconcept.net
18 // This library and the associated files are non commercial, non professional
20 // It should not have unexpected results. However if any damage is caused by
21 // this software the author can not be responsible.
22 // The use of this software is at the risk of the user.
25 // personnalisations spip
26 if (!defined('PCLZIP_TEMPORARY_DIR')) {
27 define('PCLZIP_TEMPORARY_DIR', _DIR_TMP
);
29 if (!function_exists('gzopen') && function_exists('gzopen64')) {
30 function gzopen($filename, $mode, $use_include_path = 0) {
31 return gzopen64($filename, $mode, $use_include_path);
34 // fin personnalisations spip
37 if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
38 define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
41 // ----- File list separator
42 // In version 1.x of PclZip, the separator for file list is a space
43 // (which is not a very smart choice, specifically for windows paths !).
44 // A better separator should be a comma (,). This constant gives you the
45 // abilty to change that.
46 // However notice that changing this value, may have impact on existing
47 // scripts, using space separated filenames.
48 // Recommanded values for compatibility with older versions :
49 //define( 'PCLZIP_SEPARATOR', ' ' );
50 // Recommanded values for smart separation of filenames.
51 if (!defined('PCLZIP_SEPARATOR')) {
52 define( 'PCLZIP_SEPARATOR', ',' );
55 // ----- Error configuration
56 // 0 : PclZip Class integrated error handling
57 // 1 : PclError external library error handling. By enabling this
58 // you must ensure that you have included PclError library.
59 // [2,...] : reserved for futur use
60 if (!defined('PCLZIP_ERROR_EXTERNAL')) {
61 define( 'PCLZIP_ERROR_EXTERNAL', 0 );
64 // ----- Optional static temporary directory
65 // By default temporary files are generated in the script current
68 // - MUST BE terminated by a '/'.
69 // - MUST be a valid, already created directory
71 // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
72 // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
73 if (!defined('PCLZIP_TEMPORARY_DIR')) {
74 define( 'PCLZIP_TEMPORARY_DIR', '' );
77 // ----- Optional threshold ratio for use of temporary files
78 // Pclzip sense the size of the file to add/extract and decide to
79 // use or not temporary file. The algorythm is looking for
80 // memory_limit of PHP and apply a ratio.
81 // threshold = memory_limit * ratio.
82 // Recommended values are under 0.5. Default 0.47.
84 // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
85 if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
86 define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.47 );
89 // --------------------------------------------------------------------------------
90 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
91 // --------------------------------------------------------------------------------
93 // ----- Global variables
94 $g_pclzip_version = "2.8.2";
97 // -1 : Unable to open file in binary write mode
98 // -2 : Unable to open file in binary read mode
99 // -3 : Invalid parameters
100 // -4 : File does not exist
101 // -5 : Filename is too long (max. 255)
102 // -6 : Not a valid zip file
103 // -7 : Invalid extracted file size
104 // -8 : Unable to create directory
105 // -9 : Invalid archive extension
106 // -10 : Invalid archive format
107 // -11 : Unable to delete file (unlink)
108 // -12 : Unable to rename file (rename)
109 // -13 : Invalid header checksum
110 // -14 : Invalid archive size
111 define( 'PCLZIP_ERR_USER_ABORTED', 2 );
112 define( 'PCLZIP_ERR_NO_ERROR', 0 );
113 define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
114 define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
115 define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
116 define( 'PCLZIP_ERR_MISSING_FILE', -4 );
117 define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
118 define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
119 define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
120 define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
121 define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
122 define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
123 define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
124 define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
125 define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
126 define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
127 define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
128 define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
129 define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
130 define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
131 define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
132 define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
133 define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
135 // ----- Options values
136 define( 'PCLZIP_OPT_PATH', 77001 );
137 define( 'PCLZIP_OPT_ADD_PATH', 77002 );
138 define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
139 define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
140 define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
141 define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
142 define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
143 define( 'PCLZIP_OPT_BY_NAME', 77008 );
144 define( 'PCLZIP_OPT_BY_INDEX', 77009 );
145 define( 'PCLZIP_OPT_BY_EREG', 77010 );
146 define( 'PCLZIP_OPT_BY_PREG', 77011 );
147 define( 'PCLZIP_OPT_COMMENT', 77012 );
148 define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
149 define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
150 define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
151 define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
152 define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
153 // Having big trouble with crypt. Need to multiply 2 long int
154 // which is not correctly supported by PHP ...
155 //define( 'PCLZIP_OPT_CRYPT', 77018 );
156 define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
157 define( 'PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020 );
158 define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
159 define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
160 define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
161 define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
162 define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
164 // ----- File description attributes
165 define( 'PCLZIP_ATT_FILE_NAME', 79001 );
166 define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
167 define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
168 define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
169 define( 'PCLZIP_ATT_FILE_CONTENT', 79005 );
170 define( 'PCLZIP_ATT_FILE_COMMENT', 79006 );
172 // ----- Call backs values
173 define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
174 define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
175 define( 'PCLZIP_CB_PRE_ADD', 78003 );
176 define( 'PCLZIP_CB_POST_ADD', 78004 );
178 define( 'PCLZIP_CB_PRE_LIST', 78005 );
179 define( 'PCLZIP_CB_POST_LIST', 78006 );
180 define( 'PCLZIP_CB_PRE_DELETE', 78007 );
181 define( 'PCLZIP_CB_POST_DELETE', 78008 );
184 // --------------------------------------------------------------------------------
187 // PclZip is the class that represent a Zip archive.
188 // The public methods allow the manipulation of the archive.
190 // Attributes must not be accessed directly.
192 // PclZip() : Object creator
193 // create() : Creates the Zip archive
194 // listContent() : List the content of the Zip archive
195 // extract() : Extract the content of the archive
196 // properties() : List the properties of the archive
197 // --------------------------------------------------------------------------------
200 // ----- Filename of the zip file
203 // ----- File descriptor of the zip file
206 // ----- Internal error handling
208 var $error_string = '';
210 // ----- Current status of the magic_quotes_runtime
211 // This value store the php configuration for magic_quotes
212 // The class can then disable the magic_quotes and reset it after
213 var $magic_quotes_status;
215 // --------------------------------------------------------------------------------
216 // Function : PclZip()
218 // Creates a PclZip object and set the name of the associated Zip archive
220 // Note that no real action is taken, if the archive does not exist it is not
221 // created. Use create() for that.
222 // --------------------------------------------------------------------------------
223 function PclZip($p_zipname)
226 // ----- Tests the zlib
227 if (!function_exists('gzopen'))
229 die('Abort '.basename(__FILE__
).' : Missing zlib extensions');
232 // ----- Set the attributes
233 $this->zipname
= $p_zipname;
235 $this->magic_quotes_status
= -1;
240 // --------------------------------------------------------------------------------
242 // --------------------------------------------------------------------------------
244 // create($p_filelist, $p_add_dir="", $p_remove_dir="")
245 // create($p_filelist, $p_option, $p_option_value, ...)
247 // This method supports two different synopsis. The first one is historical.
248 // This method creates a Zip Archive. The Zip file is created in the
249 // filesystem. The files and directories indicated in $p_filelist
250 // are added in the archive. See the parameters description for the
251 // supported format of $p_filelist.
252 // When a directory is in the list, the directory and its content is added
254 // In this synopsis, the function takes an optional variable list of
255 // options. See bellow the supported options.
257 // $p_filelist : An array containing file or directory names, or
258 // a string containing one filename or one directory name, or
259 // a string containing a list of filenames and/or directory
260 // names separated by spaces.
261 // $p_add_dir : A path to add before the real path of the archived file,
262 // in order to have it memorized in the archive.
263 // $p_remove_dir : A path to remove from the real path of the file to archive,
264 // in order to have a shorter path memorized in the archive.
265 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
266 // is removed first, before $p_add_dir is added.
268 // PCLZIP_OPT_ADD_PATH :
269 // PCLZIP_OPT_REMOVE_PATH :
270 // PCLZIP_OPT_REMOVE_ALL_PATH :
271 // PCLZIP_OPT_COMMENT :
272 // PCLZIP_CB_PRE_ADD :
273 // PCLZIP_CB_POST_ADD :
276 // The list of the added files, with a status of the add action.
277 // (see PclZip::listContent() for list entry format)
278 // --------------------------------------------------------------------------------
279 function create($p_filelist)
283 // ----- Reset the error handler
284 $this->privErrorReset();
286 // ----- Set default values
287 $v_options = array();
288 $v_options[PCLZIP_OPT_NO_COMPRESSION
] = FALSE;
290 // ----- Look for variable options arguments
291 $v_size = func_num_args();
293 // ----- Look for arguments
295 // ----- Get the arguments
296 $v_arg_list = func_get_args();
298 // ----- Remove from the options list the first argument
299 array_shift($v_arg_list);
302 // ----- Look for first arg
303 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
305 // ----- Parse the options
306 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
307 array (PCLZIP_OPT_REMOVE_PATH
=> 'optional',
308 PCLZIP_OPT_REMOVE_ALL_PATH
=> 'optional',
309 PCLZIP_OPT_ADD_PATH
=> 'optional',
310 PCLZIP_CB_PRE_ADD
=> 'optional',
311 PCLZIP_CB_POST_ADD
=> 'optional',
312 PCLZIP_OPT_NO_COMPRESSION
=> 'optional',
313 PCLZIP_OPT_COMMENT
=> 'optional',
314 PCLZIP_OPT_TEMP_FILE_THRESHOLD
=> 'optional',
315 PCLZIP_OPT_TEMP_FILE_ON
=> 'optional',
316 PCLZIP_OPT_TEMP_FILE_OFF
=> 'optional'
317 //, PCLZIP_OPT_CRYPT => 'optional'
319 if ($v_result != 1) {
324 // ----- Look for 2 args
325 // Here we need to support the first historic synopsis of the
329 // ----- Get the first argument
330 $v_options[PCLZIP_OPT_ADD_PATH
] = $v_arg_list[0];
332 // ----- Look for the optional second argument
334 $v_options[PCLZIP_OPT_REMOVE_PATH
] = $v_arg_list[1];
336 else if ($v_size > 2) {
337 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
,
338 "Invalid number / type of arguments");
344 // ----- Look for default option values
345 $this->privOptionDefaultThreshold($v_options);
348 $v_string_list = array();
349 $v_att_list = array();
350 $v_filedescr_list = array();
351 $p_result_list = array();
353 // ----- Look if the $p_filelist is really an array
354 if (is_array($p_filelist)) {
356 // ----- Look if the first element is also an array
357 // This will mean that this is a file description entry
358 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
359 $v_att_list = $p_filelist;
362 // ----- The list is a list of string names
364 $v_string_list = $p_filelist;
368 // ----- Look if the $p_filelist is a string
369 else if (is_string($p_filelist)) {
370 // ----- Create a list from the string
371 $v_string_list = explode(PCLZIP_SEPARATOR
, $p_filelist);
374 // ----- Invalid variable type for $p_filelist
376 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid variable type p_filelist");
380 // ----- Reformat the string list
381 if (sizeof($v_string_list) != 0) {
382 foreach ($v_string_list as $v_string) {
383 if ($v_string != '') {
384 $v_att_list[][PCLZIP_ATT_FILE_NAME
] = $v_string;
391 // ----- For each file in the list check the attributes
392 $v_supported_attributes
393 = array ( PCLZIP_ATT_FILE_NAME
=> 'mandatory'
394 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME
=> 'optional'
395 ,PCLZIP_ATT_FILE_NEW_FULL_NAME
=> 'optional'
396 ,PCLZIP_ATT_FILE_MTIME
=> 'optional'
397 ,PCLZIP_ATT_FILE_CONTENT
=> 'optional'
398 ,PCLZIP_ATT_FILE_COMMENT
=> 'optional'
400 foreach ($v_att_list as $v_entry) {
401 $v_result = $this->privFileDescrParseAtt($v_entry,
404 $v_supported_attributes);
405 if ($v_result != 1) {
410 // ----- Expand the filelist (expand directories)
411 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
412 if ($v_result != 1) {
416 // ----- Call the create fct
417 $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
418 if ($v_result != 1) {
423 return $p_result_list;
425 // --------------------------------------------------------------------------------
427 // --------------------------------------------------------------------------------
429 // add($p_filelist, $p_add_dir="", $p_remove_dir="")
430 // add($p_filelist, $p_option, $p_option_value, ...)
432 // This method supports two synopsis. The first one is historical.
433 // This methods add the list of files in an existing archive.
434 // If a file with the same name already exists, it is added at the end of the
435 // archive, the first one is still present.
436 // If the archive does not exist, it is created.
438 // $p_filelist : An array containing file or directory names, or
439 // a string containing one filename or one directory name, or
440 // a string containing a list of filenames and/or directory
441 // names separated by spaces.
442 // $p_add_dir : A path to add before the real path of the archived file,
443 // in order to have it memorized in the archive.
444 // $p_remove_dir : A path to remove from the real path of the file to archive,
445 // in order to have a shorter path memorized in the archive.
446 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
447 // is removed first, before $p_add_dir is added.
449 // PCLZIP_OPT_ADD_PATH :
450 // PCLZIP_OPT_REMOVE_PATH :
451 // PCLZIP_OPT_REMOVE_ALL_PATH :
452 // PCLZIP_OPT_COMMENT :
453 // PCLZIP_OPT_ADD_COMMENT :
454 // PCLZIP_OPT_PREPEND_COMMENT :
455 // PCLZIP_CB_PRE_ADD :
456 // PCLZIP_CB_POST_ADD :
459 // The list of the added files, with a status of the add action.
460 // (see PclZip::listContent() for list entry format)
461 // --------------------------------------------------------------------------------
462 function add($p_filelist)
466 // ----- Reset the error handler
467 $this->privErrorReset();
469 // ----- Set default values
470 $v_options = array();
471 $v_options[PCLZIP_OPT_NO_COMPRESSION
] = FALSE;
473 // ----- Look for variable options arguments
474 $v_size = func_num_args();
476 // ----- Look for arguments
478 // ----- Get the arguments
479 $v_arg_list = func_get_args();
481 // ----- Remove form the options list the first argument
482 array_shift($v_arg_list);
485 // ----- Look for first arg
486 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
488 // ----- Parse the options
489 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
490 array (PCLZIP_OPT_REMOVE_PATH
=> 'optional',
491 PCLZIP_OPT_REMOVE_ALL_PATH
=> 'optional',
492 PCLZIP_OPT_ADD_PATH
=> 'optional',
493 PCLZIP_CB_PRE_ADD
=> 'optional',
494 PCLZIP_CB_POST_ADD
=> 'optional',
495 PCLZIP_OPT_NO_COMPRESSION
=> 'optional',
496 PCLZIP_OPT_COMMENT
=> 'optional',
497 PCLZIP_OPT_ADD_COMMENT
=> 'optional',
498 PCLZIP_OPT_PREPEND_COMMENT
=> 'optional',
499 PCLZIP_OPT_TEMP_FILE_THRESHOLD
=> 'optional',
500 PCLZIP_OPT_TEMP_FILE_ON
=> 'optional',
501 PCLZIP_OPT_TEMP_FILE_OFF
=> 'optional'
502 //, PCLZIP_OPT_CRYPT => 'optional'
504 if ($v_result != 1) {
509 // ----- Look for 2 args
510 // Here we need to support the first historic synopsis of the
514 // ----- Get the first argument
515 $v_options[PCLZIP_OPT_ADD_PATH
] = $v_add_path = $v_arg_list[0];
517 // ----- Look for the optional second argument
519 $v_options[PCLZIP_OPT_REMOVE_PATH
] = $v_arg_list[1];
521 else if ($v_size > 2) {
523 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid number / type of arguments");
531 // ----- Look for default option values
532 $this->privOptionDefaultThreshold($v_options);
535 $v_string_list = array();
536 $v_att_list = array();
537 $v_filedescr_list = array();
538 $p_result_list = array();
540 // ----- Look if the $p_filelist is really an array
541 if (is_array($p_filelist)) {
543 // ----- Look if the first element is also an array
544 // This will mean that this is a file description entry
545 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
546 $v_att_list = $p_filelist;
549 // ----- The list is a list of string names
551 $v_string_list = $p_filelist;
555 // ----- Look if the $p_filelist is a string
556 else if (is_string($p_filelist)) {
557 // ----- Create a list from the string
558 $v_string_list = explode(PCLZIP_SEPARATOR
, $p_filelist);
561 // ----- Invalid variable type for $p_filelist
563 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
567 // ----- Reformat the string list
568 if (sizeof($v_string_list) != 0) {
569 foreach ($v_string_list as $v_string) {
570 $v_att_list[][PCLZIP_ATT_FILE_NAME
] = $v_string;
574 // ----- For each file in the list check the attributes
575 $v_supported_attributes
576 = array ( PCLZIP_ATT_FILE_NAME
=> 'mandatory'
577 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME
=> 'optional'
578 ,PCLZIP_ATT_FILE_NEW_FULL_NAME
=> 'optional'
579 ,PCLZIP_ATT_FILE_MTIME
=> 'optional'
580 ,PCLZIP_ATT_FILE_CONTENT
=> 'optional'
581 ,PCLZIP_ATT_FILE_COMMENT
=> 'optional'
583 foreach ($v_att_list as $v_entry) {
584 $v_result = $this->privFileDescrParseAtt($v_entry,
587 $v_supported_attributes);
588 if ($v_result != 1) {
593 // ----- Expand the filelist (expand directories)
594 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
595 if ($v_result != 1) {
599 // ----- Call the create fct
600 $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
601 if ($v_result != 1) {
606 return $p_result_list;
608 // --------------------------------------------------------------------------------
610 // --------------------------------------------------------------------------------
611 // Function : listContent()
613 // This public method, gives the list of the files and directories, with their
615 // The properties of each entries in the list are (used also in other functions) :
616 // filename : Name of the file. For a create or add action it is the filename
617 // given by the user. For an extract function it is the filename
618 // of the extracted file.
619 // stored_filename : Name of the file / directory stored in the archive.
620 // size : Size of the stored file.
621 // compressed_size : Size of the file's data compressed in the archive
622 // (without the headers overhead)
623 // mtime : Last known modification date of the file (UNIX timestamp)
624 // comment : Comment associated with the file
625 // folder : true | false
626 // index : index of the file in the archive
627 // status : status of the action (depending of the action) :
630 // filtered : the file / dir is not extracted (filtered by user)
631 // already_a_directory : the file can not be extracted because a
632 // directory with the same name already exists
633 // write_protected : the file can not be extracted because a file
634 // with the same name already exists and is
636 // newer_exist : the file was not extracted because a newer file exists
637 // path_creation_fail : the file is not extracted because the folder
638 // does not exist and can not be created
639 // write_error : the file was not extracted because there was a
640 // error while writing the file
641 // read_error : the file was not extracted because there was a error
642 // while reading the file
643 // invalid_header : the file was not extracted because of an archive
644 // format error (bad file header)
645 // Note that each time a method can continue operating when there
646 // is an action error on a file, the error is only logged in the file status.
648 // 0 on an unrecoverable failure,
649 // The list of the files in the archive.
650 // --------------------------------------------------------------------------------
651 function listContent()
655 // ----- Reset the error handler
656 $this->privErrorReset();
658 // ----- Check archive
659 if (!$this->privCheckFormat()) {
663 // ----- Call the extracting fct
665 if (($v_result = $this->privList($p_list)) != 1)
674 // --------------------------------------------------------------------------------
676 // --------------------------------------------------------------------------------
678 // extract($p_path="./", $p_remove_path="")
679 // extract([$p_option, $p_option_value, ...])
681 // This method supports two synopsis. The first one is historical.
682 // This method extract all the files / directories from the archive to the
683 // folder indicated in $p_path.
684 // If you want to ignore the 'root' part of path of the memorized files
685 // you can indicate this in the optional $p_remove_path parameter.
686 // By default, if a newer file with the same name already exists, the
687 // file is not extracted.
689 // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
690 // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
691 // at the end of the path value of PCLZIP_OPT_PATH.
693 // $p_path : Path where the files and directories are to be extracted
694 // $p_remove_path : First part ('root' part) of the memorized path
695 // (if any similar) to remove while extracting.
698 // PCLZIP_OPT_ADD_PATH :
699 // PCLZIP_OPT_REMOVE_PATH :
700 // PCLZIP_OPT_REMOVE_ALL_PATH :
701 // PCLZIP_CB_PRE_EXTRACT :
702 // PCLZIP_CB_POST_EXTRACT :
704 // 0 or a negative value on failure,
705 // The list of the extracted files, with a status of the action.
706 // (see PclZip::listContent() for list entry format)
707 // --------------------------------------------------------------------------------
712 // ----- Reset the error handler
713 $this->privErrorReset();
715 // ----- Check archive
716 if (!$this->privCheckFormat()) {
720 // ----- Set default values
721 $v_options = array();
725 $v_remove_all_path = false;
727 // ----- Look for variable options arguments
728 $v_size = func_num_args();
730 // ----- Default values for option
731 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING
] = FALSE;
733 // ----- Look for arguments
735 // ----- Get the arguments
736 $v_arg_list = func_get_args();
738 // ----- Look for first arg
739 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
741 // ----- Parse the options
742 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
743 array (PCLZIP_OPT_PATH
=> 'optional',
744 PCLZIP_OPT_REMOVE_PATH
=> 'optional',
745 PCLZIP_OPT_REMOVE_ALL_PATH
=> 'optional',
746 PCLZIP_OPT_ADD_PATH
=> 'optional',
747 PCLZIP_CB_PRE_EXTRACT
=> 'optional',
748 PCLZIP_CB_POST_EXTRACT
=> 'optional',
749 PCLZIP_OPT_SET_CHMOD
=> 'optional',
750 PCLZIP_OPT_BY_NAME
=> 'optional',
751 PCLZIP_OPT_BY_EREG
=> 'optional',
752 PCLZIP_OPT_BY_PREG
=> 'optional',
753 PCLZIP_OPT_BY_INDEX
=> 'optional',
754 PCLZIP_OPT_EXTRACT_AS_STRING
=> 'optional',
755 PCLZIP_OPT_EXTRACT_IN_OUTPUT
=> 'optional',
756 PCLZIP_OPT_REPLACE_NEWER
=> 'optional'
757 ,PCLZIP_OPT_STOP_ON_ERROR
=> 'optional'
758 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION
=> 'optional',
759 PCLZIP_OPT_TEMP_FILE_THRESHOLD
=> 'optional',
760 PCLZIP_OPT_TEMP_FILE_ON
=> 'optional',
761 PCLZIP_OPT_TEMP_FILE_OFF
=> 'optional'
763 if ($v_result != 1) {
767 // ----- Set the arguments
768 if (isset($v_options[PCLZIP_OPT_PATH
])) {
769 $v_path = $v_options[PCLZIP_OPT_PATH
];
771 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH
])) {
772 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH
];
774 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH
])) {
775 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH
];
777 if (isset($v_options[PCLZIP_OPT_ADD_PATH
])) {
778 // ----- Check for '/' in last path char
779 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
782 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH
];
786 // ----- Look for 2 args
787 // Here we need to support the first historic synopsis of the
791 // ----- Get the first argument
792 $v_path = $v_arg_list[0];
794 // ----- Look for the optional second argument
796 $v_remove_path = $v_arg_list[1];
798 else if ($v_size > 2) {
800 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid number / type of arguments");
808 // ----- Look for default option values
809 $this->privOptionDefaultThreshold($v_options);
813 // ----- Call the extracting fct
815 $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
816 $v_remove_all_path, $v_options);
825 // --------------------------------------------------------------------------------
828 // --------------------------------------------------------------------------------
830 // extractByIndex($p_index, $p_path="./", $p_remove_path="")
831 // extractByIndex($p_index, [$p_option, $p_option_value, ...])
833 // This method supports two synopsis. The first one is historical.
834 // This method is doing a partial extract of the archive.
835 // The extracted files or folders are identified by their index in the
836 // archive (from 0 to n).
837 // Note that if the index identify a folder, only the folder entry is
838 // extracted, not all the files included in the archive.
840 // $p_index : A single index (integer) or a string of indexes of files to
841 // extract. The form of the string is "0,4-6,8-12" with only numbers
842 // and '-' for range or ',' to separate ranges. No spaces or ';'
844 // $p_path : Path where the files and directories are to be extracted
845 // $p_remove_path : First part ('root' part) of the memorized path
846 // (if any similar) to remove while extracting.
849 // PCLZIP_OPT_ADD_PATH :
850 // PCLZIP_OPT_REMOVE_PATH :
851 // PCLZIP_OPT_REMOVE_ALL_PATH :
852 // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
854 // The resulting content is in a new field 'content' in the file
856 // This option must be used alone (any other options are ignored).
857 // PCLZIP_CB_PRE_EXTRACT :
858 // PCLZIP_CB_POST_EXTRACT :
861 // The list of the extracted files, with a status of the action.
862 // (see PclZip::listContent() for list entry format)
863 // --------------------------------------------------------------------------------
864 //function extractByIndex($p_index, options...)
865 function extractByIndex($p_index)
869 // ----- Reset the error handler
870 $this->privErrorReset();
872 // ----- Check archive
873 if (!$this->privCheckFormat()) {
877 // ----- Set default values
878 $v_options = array();
882 $v_remove_all_path = false;
884 // ----- Look for variable options arguments
885 $v_size = func_num_args();
887 // ----- Default values for option
888 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING
] = FALSE;
890 // ----- Look for arguments
892 // ----- Get the arguments
893 $v_arg_list = func_get_args();
895 // ----- Remove form the options list the first argument
896 array_shift($v_arg_list);
899 // ----- Look for first arg
900 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
902 // ----- Parse the options
903 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
904 array (PCLZIP_OPT_PATH
=> 'optional',
905 PCLZIP_OPT_REMOVE_PATH
=> 'optional',
906 PCLZIP_OPT_REMOVE_ALL_PATH
=> 'optional',
907 PCLZIP_OPT_EXTRACT_AS_STRING
=> 'optional',
908 PCLZIP_OPT_ADD_PATH
=> 'optional',
909 PCLZIP_CB_PRE_EXTRACT
=> 'optional',
910 PCLZIP_CB_POST_EXTRACT
=> 'optional',
911 PCLZIP_OPT_SET_CHMOD
=> 'optional',
912 PCLZIP_OPT_REPLACE_NEWER
=> 'optional'
913 ,PCLZIP_OPT_STOP_ON_ERROR
=> 'optional'
914 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION
=> 'optional',
915 PCLZIP_OPT_TEMP_FILE_THRESHOLD
=> 'optional',
916 PCLZIP_OPT_TEMP_FILE_ON
=> 'optional',
917 PCLZIP_OPT_TEMP_FILE_OFF
=> 'optional'
919 if ($v_result != 1) {
923 // ----- Set the arguments
924 if (isset($v_options[PCLZIP_OPT_PATH
])) {
925 $v_path = $v_options[PCLZIP_OPT_PATH
];
927 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH
])) {
928 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH
];
930 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH
])) {
931 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH
];
933 if (isset($v_options[PCLZIP_OPT_ADD_PATH
])) {
934 // ----- Check for '/' in last path char
935 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
938 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH
];
940 if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING
])) {
941 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING
] = FALSE;
947 // ----- Look for 2 args
948 // Here we need to support the first historic synopsis of the
952 // ----- Get the first argument
953 $v_path = $v_arg_list[0];
955 // ----- Look for the optional second argument
957 $v_remove_path = $v_arg_list[1];
959 else if ($v_size > 2) {
961 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid number / type of arguments");
972 // Here I want to reuse extractByRule(), so I need to parse the $p_index
973 // with privParseOptions()
974 $v_arg_trick = array (PCLZIP_OPT_BY_INDEX
, $p_index);
975 $v_options_trick = array();
976 $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
977 array (PCLZIP_OPT_BY_INDEX
=> 'optional' ));
978 if ($v_result != 1) {
981 $v_options[PCLZIP_OPT_BY_INDEX
] = $v_options_trick[PCLZIP_OPT_BY_INDEX
];
983 // ----- Look for default option values
984 $this->privOptionDefaultThreshold($v_options);
986 // ----- Call the extracting fct
987 if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
994 // --------------------------------------------------------------------------------
996 // --------------------------------------------------------------------------------
998 // delete([$p_option, $p_option_value, ...])
1000 // This method removes files from the archive.
1001 // If no parameters are given, then all the archive is emptied.
1003 // None or optional arguments.
1005 // PCLZIP_OPT_BY_INDEX :
1006 // PCLZIP_OPT_BY_NAME :
1007 // PCLZIP_OPT_BY_EREG :
1008 // PCLZIP_OPT_BY_PREG :
1011 // The list of the files which are still present in the archive.
1012 // (see PclZip::listContent() for list entry format)
1013 // --------------------------------------------------------------------------------
1018 // ----- Reset the error handler
1019 $this->privErrorReset();
1021 // ----- Check archive
1022 if (!$this->privCheckFormat()) {
1026 // ----- Set default values
1027 $v_options = array();
1029 // ----- Look for variable options arguments
1030 $v_size = func_num_args();
1032 // ----- Look for arguments
1034 // ----- Get the arguments
1035 $v_arg_list = func_get_args();
1037 // ----- Parse the options
1038 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
1039 array (PCLZIP_OPT_BY_NAME
=> 'optional',
1040 PCLZIP_OPT_BY_EREG
=> 'optional',
1041 PCLZIP_OPT_BY_PREG
=> 'optional',
1042 PCLZIP_OPT_BY_INDEX
=> 'optional' ));
1043 if ($v_result != 1) {
1048 // ----- Magic quotes trick
1049 $this->privDisableMagicQuotes();
1051 // ----- Call the delete fct
1053 if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1054 $this->privSwapBackMagicQuotes();
1059 // ----- Magic quotes trick
1060 $this->privSwapBackMagicQuotes();
1065 // --------------------------------------------------------------------------------
1067 // --------------------------------------------------------------------------------
1068 // Function : deleteByIndex()
1070 // ***** Deprecated *****
1071 // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1072 // --------------------------------------------------------------------------------
1073 function deleteByIndex($p_index)
1076 $p_list = $this->delete(PCLZIP_OPT_BY_INDEX
, $p_index);
1081 // --------------------------------------------------------------------------------
1083 // --------------------------------------------------------------------------------
1084 // Function : properties()
1086 // This method gives the properties of the archive.
1087 // The properties are :
1088 // nb : Number of files in the archive
1089 // comment : Comment associated with the archive file
1090 // status : not_exist, ok
1095 // An array with the archive properties.
1096 // --------------------------------------------------------------------------------
1097 function properties()
1100 // ----- Reset the error handler
1101 $this->privErrorReset();
1103 // ----- Magic quotes trick
1104 $this->privDisableMagicQuotes();
1106 // ----- Check archive
1107 if (!$this->privCheckFormat()) {
1108 $this->privSwapBackMagicQuotes();
1112 // ----- Default properties
1114 $v_prop['comment'] = '';
1116 $v_prop['status'] = 'not_exist';
1118 // ----- Look if file exists
1119 if (@is_file
($this->zipname
))
1121 // ----- Open the zip file
1122 if (($this->zip_fd
= @fopen
($this->zipname
, 'rb')) == 0)
1124 $this->privSwapBackMagicQuotes();
1127 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open archive \''.$this->zipname
.'\' in binary read mode');
1133 // ----- Read the central directory informations
1134 $v_central_dir = array();
1135 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1137 $this->privSwapBackMagicQuotes();
1141 // ----- Close the zip file
1142 $this->privCloseFd();
1144 // ----- Set the user attributes
1145 $v_prop['comment'] = $v_central_dir['comment'];
1146 $v_prop['nb'] = $v_central_dir['entries'];
1147 $v_prop['status'] = 'ok';
1150 // ----- Magic quotes trick
1151 $this->privSwapBackMagicQuotes();
1156 // --------------------------------------------------------------------------------
1158 // --------------------------------------------------------------------------------
1159 // Function : duplicate()
1161 // This method creates an archive by copying the content of an other one. If
1162 // the archive already exist, it is replaced by the new one without any warning.
1164 // $p_archive : The filename of a valid archive, or
1165 // a valid PclZip object.
1168 // 0 or a negative value on error (error code).
1169 // --------------------------------------------------------------------------------
1170 function duplicate($p_archive)
1174 // ----- Reset the error handler
1175 $this->privErrorReset();
1177 // ----- Look if the $p_archive is a PclZip object
1178 if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1181 // ----- Duplicate the archive
1182 $v_result = $this->privDuplicate($p_archive->zipname
);
1185 // ----- Look if the $p_archive is a string (so a filename)
1186 else if (is_string($p_archive))
1189 // ----- Check that $p_archive is a valid zip file
1190 // TBC : Should also check the archive format
1191 if (!is_file($p_archive)) {
1193 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_FILE
, "No file with filename '".$p_archive."'");
1194 $v_result = PCLZIP_ERR_MISSING_FILE
;
1197 // ----- Duplicate the archive
1198 $v_result = $this->privDuplicate($p_archive);
1202 // ----- Invalid variable
1206 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid variable type p_archive_to_add");
1207 $v_result = PCLZIP_ERR_INVALID_PARAMETER
;
1213 // --------------------------------------------------------------------------------
1215 // --------------------------------------------------------------------------------
1216 // Function : merge()
1218 // This method merge the $p_archive_to_add archive at the end of the current
1220 // If the archive ($this) does not exist, the merge becomes a duplicate.
1221 // If the $p_archive_to_add archive does not exist, the merge is a success.
1223 // $p_archive_to_add : It can be directly the filename of a valid zip archive,
1224 // or a PclZip object archive.
1227 // 0 or negative values on error (see below).
1228 // --------------------------------------------------------------------------------
1229 function merge($p_archive_to_add)
1233 // ----- Reset the error handler
1234 $this->privErrorReset();
1236 // ----- Check archive
1237 if (!$this->privCheckFormat()) {
1241 // ----- Look if the $p_archive_to_add is a PclZip object
1242 if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1245 // ----- Merge the archive
1246 $v_result = $this->privMerge($p_archive_to_add);
1249 // ----- Look if the $p_archive_to_add is a string (so a filename)
1250 else if (is_string($p_archive_to_add))
1253 // ----- Create a temporary archive
1254 $v_object_archive = new PclZip($p_archive_to_add);
1256 // ----- Merge the archive
1257 $v_result = $this->privMerge($v_object_archive);
1260 // ----- Invalid variable
1264 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid variable type p_archive_to_add");
1265 $v_result = PCLZIP_ERR_INVALID_PARAMETER
;
1271 // --------------------------------------------------------------------------------
1275 // --------------------------------------------------------------------------------
1276 // Function : errorCode()
1279 // --------------------------------------------------------------------------------
1280 function errorCode()
1282 if (PCLZIP_ERROR_EXTERNAL
== 1) {
1283 return(PclErrorCode());
1286 return($this->error_code
);
1289 // --------------------------------------------------------------------------------
1291 // --------------------------------------------------------------------------------
1292 // Function : errorName()
1295 // --------------------------------------------------------------------------------
1296 function errorName($p_with_code=false)
1298 $v_name = array ( PCLZIP_ERR_NO_ERROR
=> 'PCLZIP_ERR_NO_ERROR',
1299 PCLZIP_ERR_WRITE_OPEN_FAIL
=> 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1300 PCLZIP_ERR_READ_OPEN_FAIL
=> 'PCLZIP_ERR_READ_OPEN_FAIL',
1301 PCLZIP_ERR_INVALID_PARAMETER
=> 'PCLZIP_ERR_INVALID_PARAMETER',
1302 PCLZIP_ERR_MISSING_FILE
=> 'PCLZIP_ERR_MISSING_FILE',
1303 PCLZIP_ERR_FILENAME_TOO_LONG
=> 'PCLZIP_ERR_FILENAME_TOO_LONG',
1304 PCLZIP_ERR_INVALID_ZIP
=> 'PCLZIP_ERR_INVALID_ZIP',
1305 PCLZIP_ERR_BAD_EXTRACTED_FILE
=> 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1306 PCLZIP_ERR_DIR_CREATE_FAIL
=> 'PCLZIP_ERR_DIR_CREATE_FAIL',
1307 PCLZIP_ERR_BAD_EXTENSION
=> 'PCLZIP_ERR_BAD_EXTENSION',
1308 PCLZIP_ERR_BAD_FORMAT
=> 'PCLZIP_ERR_BAD_FORMAT',
1309 PCLZIP_ERR_DELETE_FILE_FAIL
=> 'PCLZIP_ERR_DELETE_FILE_FAIL',
1310 PCLZIP_ERR_RENAME_FILE_FAIL
=> 'PCLZIP_ERR_RENAME_FILE_FAIL',
1311 PCLZIP_ERR_BAD_CHECKSUM
=> 'PCLZIP_ERR_BAD_CHECKSUM',
1312 PCLZIP_ERR_INVALID_ARCHIVE_ZIP
=> 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1313 PCLZIP_ERR_MISSING_OPTION_VALUE
=> 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1314 PCLZIP_ERR_INVALID_OPTION_VALUE
=> 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1315 PCLZIP_ERR_UNSUPPORTED_COMPRESSION
=> 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1316 PCLZIP_ERR_UNSUPPORTED_ENCRYPTION
=> 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
1317 ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
=> 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
1318 ,PCLZIP_ERR_DIRECTORY_RESTRICTION
=> 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
1321 if (isset($v_name[$this->error_code
])) {
1322 $v_value = $v_name[$this->error_code
];
1325 $v_value = 'NoName';
1329 return($v_value.' ('.$this->error_code
.')');
1335 // --------------------------------------------------------------------------------
1337 // --------------------------------------------------------------------------------
1338 // Function : errorInfo()
1341 // --------------------------------------------------------------------------------
1342 function errorInfo($p_full=false)
1344 if (PCLZIP_ERROR_EXTERNAL
== 1) {
1345 return(PclErrorString());
1349 return($this->errorName(true)." : ".$this->error_string
);
1352 return($this->error_string
." [code ".$this->error_code
."]");
1356 // --------------------------------------------------------------------------------
1359 // --------------------------------------------------------------------------------
1360 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1362 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
1363 // --------------------------------------------------------------------------------
1367 // --------------------------------------------------------------------------------
1368 // Function : privCheckFormat()
1370 // This method check that the archive exists and is a valid zip archive.
1371 // Several level of check exists. (futur)
1373 // $p_level : Level of check. Default 0.
1374 // 0 : Check the first bytes (magic codes) (default value))
1375 // 1 : 0 + Check the central directory (futur)
1376 // 2 : 1 + Check each file header (futur)
1379 // false on error, the error code is set.
1380 // --------------------------------------------------------------------------------
1381 function privCheckFormat($p_level=0)
1385 // ----- Reset the file system cache
1388 // ----- Reset the error handler
1389 $this->privErrorReset();
1391 // ----- Look if the file exits
1392 if (!is_file($this->zipname
)) {
1394 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_FILE
, "Missing archive file '".$this->zipname
."'");
1398 // ----- Check that the file is readeable
1399 if (!is_readable($this->zipname
)) {
1401 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, "Unable to read archive '".$this->zipname
."'");
1405 // ----- Check the magic code
1408 // ----- Check the central header
1411 // ----- Check each file header
1417 // --------------------------------------------------------------------------------
1419 // --------------------------------------------------------------------------------
1420 // Function : privParseOptions()
1422 // This internal methods reads the variable list of arguments ($p_options_list,
1423 // $p_size) and generate an array with the options and values ($v_result_list).
1424 // $v_requested_options contains the options that can be present and those that
1426 // $v_requested_options is an array, with the option value as key, and 'optional',
1427 // or 'mandatory' as value.
1433 // --------------------------------------------------------------------------------
1434 function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1438 // ----- Read the options
1440 while ($i<$p_size) {
1442 // ----- Check if the option is supported
1443 if (!isset($v_requested_options[$p_options_list[$i]])) {
1445 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1448 return PclZip
::errorCode();
1451 // ----- Look for next option
1452 switch ($p_options_list[$i]) {
1453 // ----- Look for options that request a path value
1454 case PCLZIP_OPT_PATH
:
1455 case PCLZIP_OPT_REMOVE_PATH
:
1456 case PCLZIP_OPT_ADD_PATH
:
1457 // ----- Check the number of parameters
1458 if (($i+
1) >= $p_size) {
1460 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1463 return PclZip
::errorCode();
1466 // ----- Get the value
1467 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+
1], FALSE);
1471 case PCLZIP_OPT_TEMP_FILE_THRESHOLD
:
1472 // ----- Check the number of parameters
1473 if (($i+
1) >= $p_size) {
1474 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1475 return PclZip
::errorCode();
1478 // ----- Check for incompatible options
1479 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF
])) {
1480 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1481 return PclZip
::errorCode();
1484 // ----- Check the value
1485 $v_value = $p_options_list[$i+
1];
1486 if ((!is_integer($v_value)) ||
($v_value<0)) {
1487 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1488 return PclZip
::errorCode();
1491 // ----- Get the value (and convert it in bytes)
1492 $v_result_list[$p_options_list[$i]] = $v_value*1048576;
1496 case PCLZIP_OPT_TEMP_FILE_ON
:
1497 // ----- Check for incompatible options
1498 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF
])) {
1499 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1500 return PclZip
::errorCode();
1503 $v_result_list[$p_options_list[$i]] = true;
1506 case PCLZIP_OPT_TEMP_FILE_OFF
:
1507 // ----- Check for incompatible options
1508 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON
])) {
1509 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
1510 return PclZip
::errorCode();
1512 // ----- Check for incompatible options
1513 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD
])) {
1514 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
1515 return PclZip
::errorCode();
1518 $v_result_list[$p_options_list[$i]] = true;
1521 case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION
:
1522 // ----- Check the number of parameters
1523 if (($i+
1) >= $p_size) {
1525 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1528 return PclZip
::errorCode();
1531 // ----- Get the value
1532 if ( is_string($p_options_list[$i+
1])
1533 && ($p_options_list[$i+
1] != '')) {
1534 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+
1], FALSE);
1541 // ----- Look for options that request an array of string for value
1542 case PCLZIP_OPT_BY_NAME
:
1543 // ----- Check the number of parameters
1544 if (($i+
1) >= $p_size) {
1546 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1549 return PclZip
::errorCode();
1552 // ----- Get the value
1553 if (is_string($p_options_list[$i+
1])) {
1554 $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+
1];
1556 else if (is_array($p_options_list[$i+
1])) {
1557 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+
1];
1561 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1564 return PclZip
::errorCode();
1569 // ----- Look for options that request an EREG or PREG expression
1570 case PCLZIP_OPT_BY_EREG
:
1571 // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
1572 // to PCLZIP_OPT_BY_PREG
1573 $p_options_list[$i] = PCLZIP_OPT_BY_PREG
;
1574 case PCLZIP_OPT_BY_PREG
:
1575 //case PCLZIP_OPT_CRYPT :
1576 // ----- Check the number of parameters
1577 if (($i+
1) >= $p_size) {
1579 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1582 return PclZip
::errorCode();
1585 // ----- Get the value
1586 if (is_string($p_options_list[$i+
1])) {
1587 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+
1];
1591 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1594 return PclZip
::errorCode();
1599 // ----- Look for options that takes a string
1600 case PCLZIP_OPT_COMMENT
:
1601 case PCLZIP_OPT_ADD_COMMENT
:
1602 case PCLZIP_OPT_PREPEND_COMMENT
:
1603 // ----- Check the number of parameters
1604 if (($i+
1) >= $p_size) {
1606 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
,
1607 "Missing parameter value for option '"
1608 .PclZipUtilOptionText($p_options_list[$i])
1612 return PclZip
::errorCode();
1615 // ----- Get the value
1616 if (is_string($p_options_list[$i+
1])) {
1617 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+
1];
1621 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
,
1622 "Wrong parameter value for option '"
1623 .PclZipUtilOptionText($p_options_list[$i])
1627 return PclZip
::errorCode();
1632 // ----- Look for options that request an array of index
1633 case PCLZIP_OPT_BY_INDEX
:
1634 // ----- Check the number of parameters
1635 if (($i+
1) >= $p_size) {
1637 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1640 return PclZip
::errorCode();
1643 // ----- Get the value
1644 $v_work_list = array();
1645 if (is_string($p_options_list[$i+
1])) {
1647 // ----- Remove spaces
1648 $p_options_list[$i+
1] = strtr($p_options_list[$i+
1], ' ', '');
1650 // ----- Parse items
1651 $v_work_list = explode(",", $p_options_list[$i+
1]);
1653 else if (is_integer($p_options_list[$i+
1])) {
1654 $v_work_list[0] = $p_options_list[$i+
1].'-'.$p_options_list[$i+
1];
1656 else if (is_array($p_options_list[$i+
1])) {
1657 $v_work_list = $p_options_list[$i+
1];
1661 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1664 return PclZip
::errorCode();
1667 // ----- Reduce the index list
1668 // each index item in the list must be a couple with a start and
1669 // an end value : [0,3], [5-5], [8-10], ...
1670 // ----- Check the format of each item
1673 for ($j=0; $j<sizeof($v_work_list); $j++
) {
1674 // ----- Explode the item
1675 $v_item_list = explode("-", $v_work_list[$j]);
1676 $v_size_item_list = sizeof($v_item_list);
1678 // ----- TBC : Here we might check that each item is a
1681 // ----- Look for single value
1682 if ($v_size_item_list == 1) {
1683 // ----- Set the option value
1684 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1685 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1687 elseif ($v_size_item_list == 2) {
1688 // ----- Set the option value
1689 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1690 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1694 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1697 return PclZip
::errorCode();
1701 // ----- Look for list sort
1702 if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1705 // ----- TBC : An automatic sort should be writen ...
1707 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1710 return PclZip
::errorCode();
1712 $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1715 // ----- Sort the items
1717 // TBC : To Be Completed
1720 // ----- Next option
1724 // ----- Look for options that request no value
1725 case PCLZIP_OPT_REMOVE_ALL_PATH
:
1726 case PCLZIP_OPT_EXTRACT_AS_STRING
:
1727 case PCLZIP_OPT_NO_COMPRESSION
:
1728 case PCLZIP_OPT_EXTRACT_IN_OUTPUT
:
1729 case PCLZIP_OPT_REPLACE_NEWER
:
1730 case PCLZIP_OPT_STOP_ON_ERROR
:
1731 $v_result_list[$p_options_list[$i]] = true;
1734 // ----- Look for options that request an octal value
1735 case PCLZIP_OPT_SET_CHMOD
:
1736 // ----- Check the number of parameters
1737 if (($i+
1) >= $p_size) {
1739 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1742 return PclZip
::errorCode();
1745 // ----- Get the value
1746 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+
1];
1750 // ----- Look for options that request a call-back
1751 case PCLZIP_CB_PRE_EXTRACT
:
1752 case PCLZIP_CB_POST_EXTRACT
:
1753 case PCLZIP_CB_PRE_ADD
:
1754 case PCLZIP_CB_POST_ADD
:
1756 case PCLZIP_CB_PRE_DELETE :
1757 case PCLZIP_CB_POST_DELETE :
1758 case PCLZIP_CB_PRE_LIST :
1759 case PCLZIP_CB_POST_LIST :
1761 // ----- Check the number of parameters
1762 if (($i+
1) >= $p_size) {
1764 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE
, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1767 return PclZip
::errorCode();
1770 // ----- Get the value
1771 $v_function_name = $p_options_list[$i+
1];
1773 // ----- Check that the value is a valid existing function
1774 if (!function_exists($v_function_name)) {
1776 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE
, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1779 return PclZip
::errorCode();
1782 // ----- Set the attribute
1783 $v_result_list[$p_options_list[$i]] = $v_function_name;
1789 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
,
1790 "Unknown parameter '"
1791 .$p_options_list[$i]."'");
1794 return PclZip
::errorCode();
1797 // ----- Next options
1801 // ----- Look for mandatory options
1802 if ($v_requested_options !== false) {
1803 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1804 // ----- Look for mandatory option
1805 if ($v_requested_options[$key] == 'mandatory') {
1806 // ----- Look if present
1807 if (!isset($v_result_list[$key])) {
1809 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1812 return PclZip
::errorCode();
1818 // ----- Look for default values
1819 if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD
])) {
1826 // --------------------------------------------------------------------------------
1828 // --------------------------------------------------------------------------------
1829 // Function : privOptionDefaultThreshold()
1833 // --------------------------------------------------------------------------------
1834 function privOptionDefaultThreshold(&$p_options)
1838 if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
])
1839 ||
isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF
])) {
1843 // ----- Get 'memory_limit' configuration value
1844 $v_memory_limit = ini_get('memory_limit');
1845 $v_memory_limit = trim($v_memory_limit);
1846 $last = strtolower(substr($v_memory_limit, -1));
1849 //$v_memory_limit = $v_memory_limit*1024*1024*1024;
1850 $v_memory_limit = $v_memory_limit*1073741824;
1852 //$v_memory_limit = $v_memory_limit*1024*1024;
1853 $v_memory_limit = $v_memory_limit*1048576;
1855 $v_memory_limit = $v_memory_limit*1024;
1857 $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO
);
1860 // ----- Sanity check : No threshold if value lower than 1M
1861 if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
] < 1048576) {
1862 unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
]);
1868 // --------------------------------------------------------------------------------
1870 // --------------------------------------------------------------------------------
1871 // Function : privFileDescrParseAtt()
1877 // --------------------------------------------------------------------------------
1878 function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
1882 // ----- For each file in the list check the attributes
1883 foreach ($p_file_list as $v_key => $v_value) {
1885 // ----- Check if the option is supported
1886 if (!isset($v_requested_options[$v_key])) {
1888 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid file attribute '".$v_key."' for this file");
1891 return PclZip
::errorCode();
1894 // ----- Look for attribute
1896 case PCLZIP_ATT_FILE_NAME
:
1897 if (!is_string($v_value)) {
1898 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1899 return PclZip
::errorCode();
1902 $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
1904 if ($p_filedescr['filename'] == '') {
1905 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
1906 return PclZip
::errorCode();
1911 case PCLZIP_ATT_FILE_NEW_SHORT_NAME
:
1912 if (!is_string($v_value)) {
1913 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1914 return PclZip
::errorCode();
1917 $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
1919 if ($p_filedescr['new_short_name'] == '') {
1920 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
1921 return PclZip
::errorCode();
1925 case PCLZIP_ATT_FILE_NEW_FULL_NAME
:
1926 if (!is_string($v_value)) {
1927 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1928 return PclZip
::errorCode();
1931 $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
1933 if ($p_filedescr['new_full_name'] == '') {
1934 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
1935 return PclZip
::errorCode();
1939 // ----- Look for options that takes a string
1940 case PCLZIP_ATT_FILE_COMMENT
:
1941 if (!is_string($v_value)) {
1942 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1943 return PclZip
::errorCode();
1946 $p_filedescr['comment'] = $v_value;
1949 case PCLZIP_ATT_FILE_MTIME
:
1950 if (!is_integer($v_value)) {
1951 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE
, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
1952 return PclZip
::errorCode();
1955 $p_filedescr['mtime'] = $v_value;
1958 case PCLZIP_ATT_FILE_CONTENT
:
1959 $p_filedescr['content'] = $v_value;
1964 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
,
1965 "Unknown parameter '".$v_key."'");
1968 return PclZip
::errorCode();
1971 // ----- Look for mandatory options
1972 if ($v_requested_options !== false) {
1973 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1974 // ----- Look for mandatory option
1975 if ($v_requested_options[$key] == 'mandatory') {
1976 // ----- Look if present
1977 if (!isset($p_file_list[$key])) {
1978 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1979 return PclZip
::errorCode();
1991 // --------------------------------------------------------------------------------
1993 // --------------------------------------------------------------------------------
1994 // Function : privFileDescrExpand()
1996 // This method look for each item of the list to see if its a file, a folder
1997 // or a string to be added as file. For any other type of files (link, other)
1998 // just ignore the item.
1999 // Then prepare the information that will be stored for that file.
2000 // When its a folder, expand the folder with all the files that are in that
2001 // folder (recursively).
2006 // --------------------------------------------------------------------------------
2007 function privFileDescrExpand(&$p_filedescr_list, &$p_options)
2011 // ----- Create a result list
2012 $v_result_list = array();
2014 // ----- Look each entry
2015 for ($i=0; $i<sizeof($p_filedescr_list); $i++
) {
2017 // ----- Get filedescr
2018 $v_descr = $p_filedescr_list[$i];
2020 // ----- Reduce the filename
2021 $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
2022 $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
2024 // ----- Look for real file or folder
2025 if (file_exists($v_descr['filename'])) {
2026 if (@is_file
($v_descr['filename'])) {
2027 $v_descr['type'] = 'file';
2029 else if (@is_dir
($v_descr['filename'])) {
2030 $v_descr['type'] = 'folder';
2032 else if (@is_link
($v_descr['filename'])) {
2042 // ----- Look for string added as file
2043 else if (isset($v_descr['content'])) {
2044 $v_descr['type'] = 'virtual_file';
2047 // ----- Missing file
2050 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_FILE
, "File '".$v_descr['filename']."' does not exist");
2053 return PclZip
::errorCode();
2056 // ----- Calculate the stored filename
2057 $this->privCalculateStoredFilename($v_descr, $p_options);
2059 // ----- Add the descriptor in result list
2060 $v_result_list[sizeof($v_result_list)] = $v_descr;
2062 // ----- Look for folder
2063 if ($v_descr['type'] == 'folder') {
2064 // ----- List of items in folder
2065 $v_dirlist_descr = array();
2067 if ($v_folder_handler = @opendir
($v_descr['filename'])) {
2068 while (($v_item_handler = @readdir
($v_folder_handler)) !== false) {
2070 // ----- Skip '.' and '..'
2071 if (($v_item_handler == '.') ||
($v_item_handler == '..')) {
2075 // ----- Compose the full filename
2076 $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
2078 // ----- Look for different stored filename
2079 // Because the name of the folder was changed, the name of the
2080 // files/sub-folders also change
2081 if (($v_descr['stored_filename'] != $v_descr['filename'])
2082 && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH
]))) {
2083 if ($v_descr['stored_filename'] != '') {
2084 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
2087 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
2094 @closedir
($v_folder_handler);
2097 // TBC : unable to open folder in read mode
2100 // ----- Expand each element of the list
2101 if ($v_dirlist_nb != 0) {
2103 if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
2107 // ----- Concat the resulting list
2108 $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
2113 // ----- Free local array
2114 unset($v_dirlist_descr);
2118 // ----- Get the result list
2119 $p_filedescr_list = $v_result_list;
2124 // --------------------------------------------------------------------------------
2126 // --------------------------------------------------------------------------------
2127 // Function : privCreate()
2131 // --------------------------------------------------------------------------------
2132 function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2135 $v_list_detail = array();
2137 // ----- Magic quotes trick
2138 $this->privDisableMagicQuotes();
2140 // ----- Open the file in write mode
2141 if (($v_result = $this->privOpenFd('wb')) != 1)
2147 // ----- Add the list of files
2148 $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2151 $this->privCloseFd();
2153 // ----- Magic quotes trick
2154 $this->privSwapBackMagicQuotes();
2159 // --------------------------------------------------------------------------------
2161 // --------------------------------------------------------------------------------
2162 // Function : privAdd()
2166 // --------------------------------------------------------------------------------
2167 function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2170 $v_list_detail = array();
2172 // ----- Look if the archive exists or is empty
2173 if ((!is_file($this->zipname
)) ||
(filesize($this->zipname
) == 0))
2176 // ----- Do a create
2177 $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2182 // ----- Magic quotes trick
2183 $this->privDisableMagicQuotes();
2185 // ----- Open the zip file
2186 if (($v_result=$this->privOpenFd('rb')) != 1)
2188 // ----- Magic quotes trick
2189 $this->privSwapBackMagicQuotes();
2195 // ----- Read the central directory informations
2196 $v_central_dir = array();
2197 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2199 $this->privCloseFd();
2200 $this->privSwapBackMagicQuotes();
2204 // ----- Go to beginning of File
2205 @rewind
($this->zip_fd
);
2207 // ----- Creates a temporay file
2208 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR
.uniqid('pclzip-').'.tmp';
2210 // ----- Open the temporary file in write mode
2211 if (($v_zip_temp_fd = @fopen
($v_zip_temp_name, 'wb')) == 0)
2213 $this->privCloseFd();
2214 $this->privSwapBackMagicQuotes();
2216 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2219 return PclZip
::errorCode();
2222 // ----- Copy the files from the archive to the temporary file
2223 // TBC : Here I should better append the file and go back to erase the central dir
2224 $v_size = $v_central_dir['offset'];
2225 while ($v_size != 0)
2227 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
2228 $v_buffer = fread($this->zip_fd
, $v_read_size);
2229 @fwrite
($v_zip_temp_fd, $v_buffer, $v_read_size);
2230 $v_size -= $v_read_size;
2233 // ----- Swap the file descriptor
2234 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2235 // the following methods on the temporary fil and not the real archive
2236 $v_swap = $this->zip_fd
;
2237 $this->zip_fd
= $v_zip_temp_fd;
2238 $v_zip_temp_fd = $v_swap;
2240 // ----- Add the files
2241 $v_header_list = array();
2242 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2244 fclose($v_zip_temp_fd);
2245 $this->privCloseFd();
2246 @unlink
($v_zip_temp_name);
2247 $this->privSwapBackMagicQuotes();
2253 // ----- Store the offset of the central dir
2254 $v_offset = @ftell
($this->zip_fd
);
2256 // ----- Copy the block of file headers from the old archive
2257 $v_size = $v_central_dir['size'];
2258 while ($v_size != 0)
2260 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
2261 $v_buffer = @fread
($v_zip_temp_fd, $v_read_size);
2262 @fwrite
($this->zip_fd
, $v_buffer, $v_read_size);
2263 $v_size -= $v_read_size;
2266 // ----- Create the Central Dir files header
2267 for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++
)
2269 // ----- Create the file header
2270 if ($v_header_list[$i]['status'] == 'ok') {
2271 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2272 fclose($v_zip_temp_fd);
2273 $this->privCloseFd();
2274 @unlink
($v_zip_temp_name);
2275 $this->privSwapBackMagicQuotes();
2283 // ----- Transform the header to a 'usable' info
2284 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2287 // ----- Zip file comment
2288 $v_comment = $v_central_dir['comment'];
2289 if (isset($p_options[PCLZIP_OPT_COMMENT
])) {
2290 $v_comment = $p_options[PCLZIP_OPT_COMMENT
];
2292 if (isset($p_options[PCLZIP_OPT_ADD_COMMENT
])) {
2293 $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT
];
2295 if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT
])) {
2296 $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT
].$v_comment;
2299 // ----- Calculate the size of the central header
2300 $v_size = @ftell
($this->zip_fd
)-$v_offset;
2302 // ----- Create the central dir footer
2303 if (($v_result = $this->privWriteCentralHeader($v_count+
$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
2305 // ----- Reset the file list
2306 unset($v_header_list);
2307 $this->privSwapBackMagicQuotes();
2313 // ----- Swap back the file descriptor
2314 $v_swap = $this->zip_fd
;
2315 $this->zip_fd
= $v_zip_temp_fd;
2316 $v_zip_temp_fd = $v_swap;
2319 $this->privCloseFd();
2321 // ----- Close the temporary file
2322 @fclose
($v_zip_temp_fd);
2324 // ----- Magic quotes trick
2325 $this->privSwapBackMagicQuotes();
2327 // ----- Delete the zip file
2328 // TBC : I should test the result ...
2329 @unlink
($this->zipname
);
2331 // ----- Rename the temporary file
2332 // TBC : I should test the result ...
2333 //@rename($v_zip_temp_name, $this->zipname);
2334 PclZipUtilRename($v_zip_temp_name, $this->zipname
);
2339 // --------------------------------------------------------------------------------
2341 // --------------------------------------------------------------------------------
2342 // Function : privOpenFd()
2345 // --------------------------------------------------------------------------------
2346 function privOpenFd($p_mode)
2350 // ----- Look if already open
2351 if ($this->zip_fd
!= 0)
2354 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Zip file \''.$this->zipname
.'\' already open');
2357 return PclZip
::errorCode();
2360 // ----- Open the zip file
2361 if (($this->zip_fd
= @fopen
($this->zipname
, $p_mode)) == 0)
2364 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open archive \''.$this->zipname
.'\' in '.$p_mode.' mode');
2367 return PclZip
::errorCode();
2373 // --------------------------------------------------------------------------------
2375 // --------------------------------------------------------------------------------
2376 // Function : privCloseFd()
2379 // --------------------------------------------------------------------------------
2380 function privCloseFd()
2384 if ($this->zip_fd
!= 0)
2385 @fclose
($this->zip_fd
);
2391 // --------------------------------------------------------------------------------
2393 // --------------------------------------------------------------------------------
2394 // Function : privAddList()
2396 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2397 // different from the real path of the file. This is usefull if you want to have PclTar
2398 // running in any directory, and memorize relative path from an other directory.
2400 // $p_list : An array containing the file or directory names to add in the tar
2401 // $p_result_list : list of added files with their properties (specially the status field)
2402 // $p_add_dir : Path to add in the filename path archived
2403 // $p_remove_dir : Path to remove in the filename path archived
2405 // --------------------------------------------------------------------------------
2406 // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2407 function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2411 // ----- Add the files
2412 $v_header_list = array();
2413 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2419 // ----- Store the offset of the central dir
2420 $v_offset = @ftell
($this->zip_fd
);
2422 // ----- Create the Central Dir files header
2423 for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++
)
2425 // ----- Create the file header
2426 if ($v_header_list[$i]['status'] == 'ok') {
2427 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2434 // ----- Transform the header to a 'usable' info
2435 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2438 // ----- Zip file comment
2440 if (isset($p_options[PCLZIP_OPT_COMMENT
])) {
2441 $v_comment = $p_options[PCLZIP_OPT_COMMENT
];
2444 // ----- Calculate the size of the central header
2445 $v_size = @ftell
($this->zip_fd
)-$v_offset;
2447 // ----- Create the central dir footer
2448 if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2450 // ----- Reset the file list
2451 unset($v_header_list);
2460 // --------------------------------------------------------------------------------
2462 // --------------------------------------------------------------------------------
2463 // Function : privAddFileList()
2466 // $p_filedescr_list : An array containing the file description
2467 // or directory names to add in the zip
2468 // $p_result_list : list of added files with their properties (specially the status field)
2470 // --------------------------------------------------------------------------------
2471 function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2474 $v_header = array();
2476 // ----- Recuperate the current number of elt in list
2477 $v_nb = sizeof($p_result_list);
2479 // ----- Loop on the files
2480 for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++
) {
2481 // ----- Format the filename
2482 $p_filedescr_list[$j]['filename']
2483 = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2486 // ----- Skip empty file names
2487 // TBC : Can this be possible ? not checked in DescrParseAtt ?
2488 if ($p_filedescr_list[$j]['filename'] == "") {
2492 // ----- Check the filename
2493 if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
2494 && (!file_exists($p_filedescr_list[$j]['filename']))) {
2495 PclZip
::privErrorLog(PCLZIP_ERR_MISSING_FILE
, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2496 return PclZip
::errorCode();
2499 // ----- Look if it is a file or a dir with no all path remove option
2500 // or a dir with all its path removed
2501 // if ( (is_file($p_filedescr_list[$j]['filename']))
2502 // || ( is_dir($p_filedescr_list[$j]['filename'])
2503 if ( ($p_filedescr_list[$j]['type'] == 'file')
2504 ||
($p_filedescr_list[$j]['type'] == 'virtual_file')
2505 ||
( ($p_filedescr_list[$j]['type'] == 'folder')
2506 && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH
])
2507 ||
!$p_options[PCLZIP_OPT_REMOVE_ALL_PATH
]))
2510 // ----- Add the file
2511 $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
2513 if ($v_result != 1) {
2517 // ----- Store the file infos
2518 $p_result_list[$v_nb++
] = $v_header;
2525 // --------------------------------------------------------------------------------
2527 // --------------------------------------------------------------------------------
2528 // Function : privAddFile()
2532 // --------------------------------------------------------------------------------
2533 function privAddFile($p_filedescr, &$p_header, &$p_options)
2537 // ----- Working variable
2538 $p_filename = $p_filedescr['filename'];
2540 // TBC : Already done in the fileAtt check ... ?
2541 if ($p_filename == "") {
2543 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER
, "Invalid file list parameter (invalid or empty list)");
2546 return PclZip
::errorCode();
2549 // ----- Look for a stored different filename
2551 if (isset($p_filedescr['stored_filename'])) {
2552 $v_stored_filename = $p_filedescr['stored_filename'];
2555 $v_stored_filename = $p_filedescr['stored_filename'];
2559 // ----- Set the file properties
2561 $p_header['version'] = 20;
2562 $p_header['version_extracted'] = 10;
2563 $p_header['flag'] = 0;
2564 $p_header['compression'] = 0;
2565 $p_header['crc'] = 0;
2566 $p_header['compressed_size'] = 0;
2567 $p_header['filename_len'] = strlen($p_filename);
2568 $p_header['extra_len'] = 0;
2569 $p_header['disk'] = 0;
2570 $p_header['internal'] = 0;
2571 $p_header['offset'] = 0;
2572 $p_header['filename'] = $p_filename;
2573 // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
2574 $p_header['stored_filename'] = $p_filedescr['stored_filename'];
2575 $p_header['extra'] = '';
2576 $p_header['status'] = 'ok';
2577 $p_header['index'] = -1;
2579 // ----- Look for regular file
2580 if ($p_filedescr['type']=='file') {
2581 $p_header['external'] = 0x00000000;
2582 $p_header['size'] = filesize($p_filename);
2585 // ----- Look for regular folder
2586 else if ($p_filedescr['type']=='folder') {
2587 $p_header['external'] = 0x00000010;
2588 $p_header['mtime'] = filemtime($p_filename);
2589 $p_header['size'] = filesize($p_filename);
2592 // ----- Look for virtual file
2593 else if ($p_filedescr['type'] == 'virtual_file') {
2594 $p_header['external'] = 0x00000000;
2595 $p_header['size'] = strlen($p_filedescr['content']);
2599 // ----- Look for filetime
2600 if (isset($p_filedescr['mtime'])) {
2601 $p_header['mtime'] = $p_filedescr['mtime'];
2603 else if ($p_filedescr['type'] == 'virtual_file') {
2604 $p_header['mtime'] = time();
2607 $p_header['mtime'] = filemtime($p_filename);
2610 // ------ Look for file comment
2611 if (isset($p_filedescr['comment'])) {
2612 $p_header['comment_len'] = strlen($p_filedescr['comment']);
2613 $p_header['comment'] = $p_filedescr['comment'];
2616 $p_header['comment_len'] = 0;
2617 $p_header['comment'] = '';
2620 // ----- Look for pre-add callback
2621 if (isset($p_options[PCLZIP_CB_PRE_ADD
])) {
2623 // ----- Generate a local information
2624 $v_local_header = array();
2625 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2627 // ----- Call the callback
2628 // Here I do not use call_user_func() because I need to send a reference to the
2630 // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
2631 $v_result = $p_options[PCLZIP_CB_PRE_ADD
](PCLZIP_CB_PRE_ADD
, $v_local_header);
2632 if ($v_result == 0) {
2633 // ----- Change the file status
2634 $p_header['status'] = "skipped";
2638 // ----- Update the informations
2639 // Only some fields can be modified
2640 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2641 $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2645 // ----- Look for empty stored filename
2646 if ($p_header['stored_filename'] == "") {
2647 $p_header['status'] = "filtered";
2650 // ----- Check the path length
2651 if (strlen($p_header['stored_filename']) > 0xFF) {
2652 $p_header['status'] = 'filename_too_long';
2655 // ----- Look if no error, or file not skipped
2656 if ($p_header['status'] == 'ok') {
2658 // ----- Look for a file
2659 if ($p_filedescr['type'] == 'file') {
2660 // ----- Look for using temporary file to zip
2661 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF
]))
2662 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON
])
2663 ||
(isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
])
2664 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
] <= $p_header['size'])) ) ) {
2665 $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
2666 if ($v_result < PCLZIP_ERR_NO_ERROR
) {
2671 // ----- Use "in memory" zip algo
2674 // ----- Open the source file
2675 if (($v_file = @fopen
($p_filename, "rb")) == 0) {
2676 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, "Unable to open file '$p_filename' in binary read mode");
2677 return PclZip
::errorCode();
2680 // ----- Read the file content
2681 $v_content = @fread
($v_file, $p_header['size']);
2683 // ----- Close the file
2686 // ----- Calculate the CRC
2687 $p_header['crc'] = @crc32
($v_content);
2689 // ----- Look for no compression
2690 if ($p_options[PCLZIP_OPT_NO_COMPRESSION
]) {
2691 // ----- Set header parameters
2692 $p_header['compressed_size'] = $p_header['size'];
2693 $p_header['compression'] = 0;
2696 // ----- Look for normal compression
2698 // ----- Compress the content
2699 $v_content = @gzdeflate
($v_content);
2701 // ----- Set header parameters
2702 $p_header['compressed_size'] = strlen($v_content);
2703 $p_header['compression'] = 8;
2706 // ----- Call the header generation
2707 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2712 // ----- Write the compressed (or not) content
2713 @fwrite
($this->zip_fd
, $v_content, $p_header['compressed_size']);
2719 // ----- Look for a virtual file (a file from string)
2720 else if ($p_filedescr['type'] == 'virtual_file') {
2722 $v_content = $p_filedescr['content'];
2724 // ----- Calculate the CRC
2725 $p_header['crc'] = @crc32
($v_content);
2727 // ----- Look for no compression
2728 if ($p_options[PCLZIP_OPT_NO_COMPRESSION
]) {
2729 // ----- Set header parameters
2730 $p_header['compressed_size'] = $p_header['size'];
2731 $p_header['compression'] = 0;
2734 // ----- Look for normal compression
2736 // ----- Compress the content
2737 $v_content = @gzdeflate
($v_content);
2739 // ----- Set header parameters
2740 $p_header['compressed_size'] = strlen($v_content);
2741 $p_header['compression'] = 8;
2744 // ----- Call the header generation
2745 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2750 // ----- Write the compressed (or not) content
2751 @fwrite
($this->zip_fd
, $v_content, $p_header['compressed_size']);
2754 // ----- Look for a directory
2755 else if ($p_filedescr['type'] == 'folder') {
2756 // ----- Look for directory last '/'
2757 if (@substr
($p_header['stored_filename'], -1) != '/') {
2758 $p_header['stored_filename'] .= '/';
2761 // ----- Set the file properties
2762 $p_header['size'] = 0;
2763 //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
2764 $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2766 // ----- Call the header generation
2767 if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2774 // ----- Look for post-add callback
2775 if (isset($p_options[PCLZIP_CB_POST_ADD
])) {
2777 // ----- Generate a local information
2778 $v_local_header = array();
2779 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2781 // ----- Call the callback
2782 // Here I do not use call_user_func() because I need to send a reference to the
2784 // eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
2785 $v_result = $p_options[PCLZIP_CB_POST_ADD
](PCLZIP_CB_POST_ADD
, $v_local_header);
2786 if ($v_result == 0) {
2791 // ----- Update the informations
2792 // Nothing can be modified
2798 // --------------------------------------------------------------------------------
2800 // --------------------------------------------------------------------------------
2801 // Function : privAddFileUsingTempFile()
2805 // --------------------------------------------------------------------------------
2806 function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
2808 $v_result=PCLZIP_ERR_NO_ERROR
;
2810 // ----- Working variable
2811 $p_filename = $p_filedescr['filename'];
2814 // ----- Open the source file
2815 if (($v_file = @fopen
($p_filename, "rb")) == 0) {
2816 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, "Unable to open file '$p_filename' in binary read mode");
2817 return PclZip
::errorCode();
2820 // ----- Creates a compressed temporary file
2821 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR
.uniqid('pclzip-').'.gz';
2822 if (($v_file_compressed = @gzopen
($v_gzip_temp_name, "wb")) == 0) {
2824 PclZip
::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL
, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
2825 return PclZip
::errorCode();
2828 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2829 $v_size = filesize($p_filename);
2830 while ($v_size != 0) {
2831 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
2832 $v_buffer = @fread
($v_file, $v_read_size);
2833 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
2834 @gzputs
($v_file_compressed, $v_buffer, $v_read_size);
2835 $v_size -= $v_read_size;
2838 // ----- Close the file
2840 @gzclose
($v_file_compressed);
2842 // ----- Check the minimum file size
2843 if (filesize($v_gzip_temp_name) < 18) {
2844 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
2845 return PclZip
::errorCode();
2848 // ----- Extract the compressed attributes
2849 if (($v_file_compressed = @fopen
($v_gzip_temp_name, "rb")) == 0) {
2850 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
2851 return PclZip
::errorCode();
2854 // ----- Read the gzip file header
2855 $v_binary_data = @fread
($v_file_compressed, 10);
2856 $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
2858 // ----- Check some parameters
2859 $v_data_header['os'] = bin2hex($v_data_header['os']);
2861 // ----- Read the gzip file footer
2862 @fseek
($v_file_compressed, filesize($v_gzip_temp_name)-8);
2863 $v_binary_data = @fread
($v_file_compressed, 8);
2864 $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
2866 // ----- Set the attributes
2867 $p_header['compression'] = ord($v_data_header['cm']);
2868 //$p_header['mtime'] = $v_data_header['mtime'];
2869 $p_header['crc'] = $v_data_footer['crc'];
2870 $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
2872 // ----- Close the file
2873 @fclose
($v_file_compressed);
2875 // ----- Call the header generation
2876 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2880 // ----- Add the compressed data
2881 if (($v_file_compressed = @fopen
($v_gzip_temp_name, "rb")) == 0)
2883 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
2884 return PclZip
::errorCode();
2887 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2888 fseek($v_file_compressed, 10);
2889 $v_size = $p_header['compressed_size'];
2890 while ($v_size != 0)
2892 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
2893 $v_buffer = @fread
($v_file_compressed, $v_read_size);
2894 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
2895 @fwrite
($this->zip_fd
, $v_buffer, $v_read_size);
2896 $v_size -= $v_read_size;
2899 // ----- Close the file
2900 @fclose
($v_file_compressed);
2902 // ----- Unlink the temporary file
2903 @unlink
($v_gzip_temp_name);
2908 // --------------------------------------------------------------------------------
2910 // --------------------------------------------------------------------------------
2911 // Function : privCalculateStoredFilename()
2913 // Based on file descriptor properties and global options, this method
2914 // calculate the filename that will be stored in the archive.
2917 // --------------------------------------------------------------------------------
2918 function privCalculateStoredFilename(&$p_filedescr, &$p_options)
2922 // ----- Working variables
2923 $p_filename = $p_filedescr['filename'];
2924 if (isset($p_options[PCLZIP_OPT_ADD_PATH
])) {
2925 $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH
];
2930 if (isset($p_options[PCLZIP_OPT_REMOVE_PATH
])) {
2931 $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH
];
2936 if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH
])) {
2937 $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH
];
2940 $p_remove_all_dir = 0;
2944 // ----- Look for full name change
2945 if (isset($p_filedescr['new_full_name'])) {
2946 // ----- Remove drive letter if any
2947 $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
2950 // ----- Look for path and/or short name change
2953 // ----- Look for short name change
2954 // Its when we cahnge just the filename but not the path
2955 if (isset($p_filedescr['new_short_name'])) {
2956 $v_path_info = pathinfo($p_filename);
2958 if ($v_path_info['dirname'] != '') {
2959 $v_dir = $v_path_info['dirname'].'/';
2961 $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
2964 // ----- Calculate the stored filename
2965 $v_stored_filename = $p_filename;
2968 // ----- Look for all path to remove
2969 if ($p_remove_all_dir) {
2970 $v_stored_filename = basename($p_filename);
2972 // ----- Look for partial path remove
2973 else if ($p_remove_dir != "") {
2974 if (substr($p_remove_dir, -1) != '/')
2975 $p_remove_dir .= "/";
2977 if ( (substr($p_filename, 0, 2) == "./")
2978 ||
(substr($p_remove_dir, 0, 2) == "./")) {
2980 if ( (substr($p_filename, 0, 2) == "./")
2981 && (substr($p_remove_dir, 0, 2) != "./")) {
2982 $p_remove_dir = "./".$p_remove_dir;
2984 if ( (substr($p_filename, 0, 2) != "./")
2985 && (substr($p_remove_dir, 0, 2) == "./")) {
2986 $p_remove_dir = substr($p_remove_dir, 2);
2990 $v_compare = PclZipUtilPathInclusion($p_remove_dir,
2991 $v_stored_filename);
2992 if ($v_compare > 0) {
2993 if ($v_compare == 2) {
2994 $v_stored_filename = "";
2997 $v_stored_filename = substr($v_stored_filename,
2998 strlen($p_remove_dir));
3003 // ----- Remove drive letter if any
3004 $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
3006 // ----- Look for path to add
3007 if ($p_add_dir != "") {
3008 if (substr($p_add_dir, -1) == "/")
3009 $v_stored_filename = $p_add_dir.$v_stored_filename;
3011 $v_stored_filename = $p_add_dir."/".$v_stored_filename;
3015 // ----- Filename (reduce the path of stored name)
3016 $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
3017 $p_filedescr['stored_filename'] = $v_stored_filename;
3022 // --------------------------------------------------------------------------------
3024 // --------------------------------------------------------------------------------
3025 // Function : privWriteFileHeader()
3029 // --------------------------------------------------------------------------------
3030 function privWriteFileHeader(&$p_header)
3034 // ----- Store the offset position of the file
3035 $p_header['offset'] = ftell($this->zip_fd
);
3037 // ----- Transform UNIX mtime to DOS format mdate/mtime
3038 $v_date = getdate($p_header['mtime']);
3039 $v_mtime = ($v_date['hours']<<11) +
($v_date['minutes']<<5) +
$v_date['seconds']/2;
3040 $v_mdate = (($v_date['year']-1980)<<9) +
($v_date['mon']<<5) +
$v_date['mday'];
3042 // ----- Packed data
3043 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
3044 $p_header['version_extracted'], $p_header['flag'],
3045 $p_header['compression'], $v_mtime, $v_mdate,
3046 $p_header['crc'], $p_header['compressed_size'],
3048 strlen($p_header['stored_filename']),
3049 $p_header['extra_len']);
3051 // ----- Write the first 148 bytes of the header in the archive
3052 fputs($this->zip_fd
, $v_binary_data, 30);
3054 // ----- Write the variable fields
3055 if (strlen($p_header['stored_filename']) != 0)
3057 fputs($this->zip_fd
, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3059 if ($p_header['extra_len'] != 0)
3061 fputs($this->zip_fd
, $p_header['extra'], $p_header['extra_len']);
3067 // --------------------------------------------------------------------------------
3069 // --------------------------------------------------------------------------------
3070 // Function : privWriteCentralFileHeader()
3074 // --------------------------------------------------------------------------------
3075 function privWriteCentralFileHeader(&$p_header)
3080 //for(reset($p_header); $key = key($p_header); next($p_header)) {
3083 // ----- Transform UNIX mtime to DOS format mdate/mtime
3084 $v_date = getdate($p_header['mtime']);
3085 $v_mtime = ($v_date['hours']<<11) +
($v_date['minutes']<<5) +
$v_date['seconds']/2;
3086 $v_mdate = (($v_date['year']-1980)<<9) +
($v_date['mon']<<5) +
$v_date['mday'];
3089 // ----- Packed data
3090 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
3091 $p_header['version'], $p_header['version_extracted'],
3092 $p_header['flag'], $p_header['compression'],
3093 $v_mtime, $v_mdate, $p_header['crc'],
3094 $p_header['compressed_size'], $p_header['size'],
3095 strlen($p_header['stored_filename']),
3096 $p_header['extra_len'], $p_header['comment_len'],
3097 $p_header['disk'], $p_header['internal'],
3098 $p_header['external'], $p_header['offset']);
3100 // ----- Write the 42 bytes of the header in the zip file
3101 fputs($this->zip_fd
, $v_binary_data, 46);
3103 // ----- Write the variable fields
3104 if (strlen($p_header['stored_filename']) != 0)
3106 fputs($this->zip_fd
, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3108 if ($p_header['extra_len'] != 0)
3110 fputs($this->zip_fd
, $p_header['extra'], $p_header['extra_len']);
3112 if ($p_header['comment_len'] != 0)
3114 fputs($this->zip_fd
, $p_header['comment'], $p_header['comment_len']);
3120 // --------------------------------------------------------------------------------
3122 // --------------------------------------------------------------------------------
3123 // Function : privWriteCentralHeader()
3127 // --------------------------------------------------------------------------------
3128 function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
3132 // ----- Packed data
3133 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
3134 $p_nb_entries, $p_size,
3135 $p_offset, strlen($p_comment));
3137 // ----- Write the 22 bytes of the header in the zip file
3138 fputs($this->zip_fd
, $v_binary_data, 22);
3140 // ----- Write the variable fields
3141 if (strlen($p_comment) != 0)
3143 fputs($this->zip_fd
, $p_comment, strlen($p_comment));
3149 // --------------------------------------------------------------------------------
3151 // --------------------------------------------------------------------------------
3152 // Function : privList()
3156 // --------------------------------------------------------------------------------
3157 function privList(&$p_list)
3161 // ----- Magic quotes trick
3162 $this->privDisableMagicQuotes();
3164 // ----- Open the zip file
3165 if (($this->zip_fd
= @fopen
($this->zipname
, 'rb')) == 0)
3167 // ----- Magic quotes trick
3168 $this->privSwapBackMagicQuotes();
3171 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open archive \''.$this->zipname
.'\' in binary read mode');
3174 return PclZip
::errorCode();
3177 // ----- Read the central directory informations
3178 $v_central_dir = array();
3179 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3181 $this->privSwapBackMagicQuotes();
3185 // ----- Go to beginning of Central Dir
3186 @rewind
($this->zip_fd
);
3187 if (@fseek
($this->zip_fd
, $v_central_dir['offset']))
3189 $this->privSwapBackMagicQuotes();
3192 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
3195 return PclZip
::errorCode();
3198 // ----- Read each entry
3199 for ($i=0; $i<$v_central_dir['entries']; $i++
)
3201 // ----- Read the file header
3202 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3204 $this->privSwapBackMagicQuotes();
3207 $v_header['index'] = $i;
3209 // ----- Get the only interesting attributes
3210 $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3214 // ----- Close the zip file
3215 $this->privCloseFd();
3217 // ----- Magic quotes trick
3218 $this->privSwapBackMagicQuotes();
3223 // --------------------------------------------------------------------------------
3225 // --------------------------------------------------------------------------------
3226 // Function : privConvertHeader2FileInfo()
3228 // This function takes the file informations from the central directory
3229 // entries and extract the interesting parameters that will be given back.
3230 // The resulting file infos are set in the array $p_info
3231 // $p_info['filename'] : Filename with full path. Given by user (add),
3232 // extracted in the filesystem (extract).
3233 // $p_info['stored_filename'] : Stored filename in the archive.
3234 // $p_info['size'] = Size of the file.
3235 // $p_info['compressed_size'] = Compressed size of the file.
3236 // $p_info['mtime'] = Last modification date of the file.
3237 // $p_info['comment'] = Comment associated with the file.
3238 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3239 // $p_info['status'] = status of the action on the file.
3240 // $p_info['crc'] = CRC of the file content.
3243 // --------------------------------------------------------------------------------
3244 function privConvertHeader2FileInfo($p_header, &$p_info)
3248 // ----- Get the interesting attributes
3249 $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
3250 $p_info['filename'] = $v_temp_path;
3251 $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
3252 $p_info['stored_filename'] = $v_temp_path;
3253 $p_info['size'] = $p_header['size'];
3254 $p_info['compressed_size'] = $p_header['compressed_size'];
3255 $p_info['mtime'] = $p_header['mtime'];
3256 $p_info['comment'] = $p_header['comment'];
3257 $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
3258 $p_info['index'] = $p_header['index'];
3259 $p_info['status'] = $p_header['status'];
3260 $p_info['crc'] = $p_header['crc'];
3265 // --------------------------------------------------------------------------------
3267 // --------------------------------------------------------------------------------
3268 // Function : privExtractByRule()
3270 // Extract a file or directory depending of rules (by index, by name, ...)
3272 // $p_file_list : An array where will be placed the properties of each
3274 // $p_path : Path to add while writing the extracted files
3275 // $p_remove_path : Path to remove (from the file memorized path) while writing the
3276 // extracted files. If the path does not match the file path,
3277 // the file is extracted with its memorized path.
3278 // $p_remove_path does not apply to 'list' mode.
3279 // $p_path and $p_remove_path are commulative.
3281 // 1 on success,0 or less on error (see error code list)
3282 // --------------------------------------------------------------------------------
3283 function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3287 // ----- Magic quotes trick
3288 $this->privDisableMagicQuotes();
3290 // ----- Check the path
3291 if ( ($p_path == "")
3292 ||
( (substr($p_path, 0, 1) != "/")
3293 && (substr($p_path, 0, 3) != "../")
3294 && (substr($p_path,1,2)!=":/")))
3295 $p_path = "./".$p_path;
3297 // ----- Reduce the path last (and duplicated) '/'
3298 if (($p_path != "./") && ($p_path != "/"))
3300 // ----- Look for the path end '/'
3301 while (substr($p_path, -1) == "/")
3303 $p_path = substr($p_path, 0, strlen($p_path)-1);
3307 // ----- Look for path to remove format (should end by /)
3308 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
3310 $p_remove_path .= '/';
3312 $p_remove_path_size = strlen($p_remove_path);
3314 // ----- Open the zip file
3315 if (($v_result = $this->privOpenFd('rb')) != 1)
3317 $this->privSwapBackMagicQuotes();
3321 // ----- Read the central directory informations
3322 $v_central_dir = array();
3323 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3325 // ----- Close the zip file
3326 $this->privCloseFd();
3327 $this->privSwapBackMagicQuotes();
3332 // ----- Start at beginning of Central Dir
3333 $v_pos_entry = $v_central_dir['offset'];
3335 // ----- Read each entry
3337 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++
)
3340 // ----- Read next Central dir entry
3341 @rewind
($this->zip_fd
);
3342 if (@fseek
($this->zip_fd
, $v_pos_entry))
3344 // ----- Close the zip file
3345 $this->privCloseFd();
3346 $this->privSwapBackMagicQuotes();
3349 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
3352 return PclZip
::errorCode();
3355 // ----- Read the file header
3356 $v_header = array();
3357 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3359 // ----- Close the zip file
3360 $this->privCloseFd();
3361 $this->privSwapBackMagicQuotes();
3366 // ----- Store the index
3367 $v_header['index'] = $i;
3369 // ----- Store the file position
3370 $v_pos_entry = ftell($this->zip_fd
);
3372 // ----- Look for the specific extract rules
3375 // ----- Look for extract by name rule
3376 if ( (isset($p_options[PCLZIP_OPT_BY_NAME
]))
3377 && ($p_options[PCLZIP_OPT_BY_NAME
] != 0)) {
3379 // ----- Look if the filename is in the list
3380 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME
])) && (!$v_extract); $j++
) {
3382 // ----- Look for a directory
3383 if (substr($p_options[PCLZIP_OPT_BY_NAME
][$j], -1) == "/") {
3385 // ----- Look if the directory is in the filename path
3386 if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME
][$j]))
3387 && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME
][$j])) == $p_options[PCLZIP_OPT_BY_NAME
][$j])) {
3391 // ----- Look for a filename
3392 elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME
][$j]) {
3398 // ----- Look for extract by ereg rule
3399 // ereg() is deprecated with PHP 5.3
3401 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
3402 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
3404 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
3410 // ----- Look for extract by preg rule
3411 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG
]))
3412 && ($p_options[PCLZIP_OPT_BY_PREG
] != "")) {
3414 if (preg_match($p_options[PCLZIP_OPT_BY_PREG
], $v_header['stored_filename'])) {
3419 // ----- Look for extract by index rule
3420 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX
]))
3421 && ($p_options[PCLZIP_OPT_BY_INDEX
] != 0)) {
3423 // ----- Look if the index is in the list
3424 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX
])) && (!$v_extract); $j++
) {
3426 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['end'])) {
3429 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['end']) {
3433 if ($p_options[PCLZIP_OPT_BY_INDEX
][$j]['start']>$i) {
3439 // ----- Look for no rule, which means extract all the archive
3444 // ----- Check compression method
3446 && ( ($v_header['compression'] != 8)
3447 && ($v_header['compression'] != 0))) {
3448 $v_header['status'] = 'unsupported_compression';
3450 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3451 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR
]))
3452 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR
]===true)) {
3454 $this->privSwapBackMagicQuotes();
3456 PclZip
::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION
,
3457 "Filename '".$v_header['stored_filename']."' is "
3458 ."compressed by an unsupported compression "
3459 ."method (".$v_header['compression'].") ");
3461 return PclZip
::errorCode();
3465 // ----- Check encrypted files
3466 if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
3467 $v_header['status'] = 'unsupported_encryption';
3469 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3470 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR
]))
3471 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR
]===true)) {
3473 $this->privSwapBackMagicQuotes();
3475 PclZip
::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION
,
3476 "Unsupported encryption for "
3477 ." filename '".$v_header['stored_filename']
3480 return PclZip
::errorCode();
3484 // ----- Look for real extraction
3485 if (($v_extract) && ($v_header['status'] != 'ok')) {
3486 $v_result = $this->privConvertHeader2FileInfo($v_header,
3487 $p_file_list[$v_nb_extracted++
]);
3488 if ($v_result != 1) {
3489 $this->privCloseFd();
3490 $this->privSwapBackMagicQuotes();
3497 // ----- Look for real extraction
3501 // ----- Go to the file position
3502 @rewind
($this->zip_fd
);
3503 if (@fseek
($this->zip_fd
, $v_header['offset']))
3505 // ----- Close the zip file
3506 $this->privCloseFd();
3508 $this->privSwapBackMagicQuotes();
3511 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
3514 return PclZip
::errorCode();
3517 // ----- Look for extraction as string
3518 if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING
]) {
3522 // ----- Extracting the file
3523 $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
3524 if ($v_result1 < 1) {
3525 $this->privCloseFd();
3526 $this->privSwapBackMagicQuotes();
3530 // ----- Get the only interesting attributes
3531 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
3533 // ----- Close the zip file
3534 $this->privCloseFd();
3535 $this->privSwapBackMagicQuotes();
3540 // ----- Set the file content
3541 $p_file_list[$v_nb_extracted]['content'] = $v_string;
3543 // ----- Next extracted file
3546 // ----- Look for user callback abort
3547 if ($v_result1 == 2) {
3551 // ----- Look for extraction in standard output
3552 elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT
]))
3553 && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT
])) {
3554 // ----- Extracting the file in standard output
3555 $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3556 if ($v_result1 < 1) {
3557 $this->privCloseFd();
3558 $this->privSwapBackMagicQuotes();
3562 // ----- Get the only interesting attributes
3563 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++
])) != 1) {
3564 $this->privCloseFd();
3565 $this->privSwapBackMagicQuotes();
3569 // ----- Look for user callback abort
3570 if ($v_result1 == 2) {
3574 // ----- Look for normal extraction
3576 // ----- Extracting the file
3577 $v_result1 = $this->privExtractFile($v_header,
3578 $p_path, $p_remove_path,
3581 if ($v_result1 < 1) {
3582 $this->privCloseFd();
3583 $this->privSwapBackMagicQuotes();
3587 // ----- Get the only interesting attributes
3588 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++
])) != 1)
3590 // ----- Close the zip file
3591 $this->privCloseFd();
3592 $this->privSwapBackMagicQuotes();
3597 // ----- Look for user callback abort
3598 if ($v_result1 == 2) {
3605 // ----- Close the zip file
3606 $this->privCloseFd();
3607 $this->privSwapBackMagicQuotes();
3612 // --------------------------------------------------------------------------------
3614 // --------------------------------------------------------------------------------
3615 // Function : privExtractFile()
3621 // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3622 // --------------------------------------------------------------------------------
3623 function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3627 // ----- Read the file header
3628 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3635 // ----- Check that the file header is coherent with $p_entry info
3636 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3640 // ----- Look for all path to remove
3641 if ($p_remove_all_path == true) {
3642 // ----- Look for folder entry that not need to be extracted
3643 if (($p_entry['external']&0x00000010)==0x00000010) {
3645 $p_entry['status'] = "filtered";
3650 // ----- Get the basename of the path
3651 $p_entry['filename'] = basename($p_entry['filename']);
3654 // ----- Look for path to remove
3655 else if ($p_remove_path != "")
3657 if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
3660 // ----- Change the file status
3661 $p_entry['status'] = "filtered";
3667 $p_remove_path_size = strlen($p_remove_path);
3668 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
3671 // ----- Remove the path
3672 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
3677 // ----- Add the path
3678 if ($p_path != '') {
3679 $p_entry['filename'] = $p_path."/".$p_entry['filename'];
3682 // ----- Check a base_dir_restriction
3683 if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION
])) {
3685 = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION
],
3686 $p_entry['filename']);
3687 if ($v_inclusion == 0) {
3689 PclZip
::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION
,
3690 "Filename '".$p_entry['filename']."' is "
3691 ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
3693 return PclZip
::errorCode();
3697 // ----- Look for pre-extract callback
3698 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT
])) {
3700 // ----- Generate a local information
3701 $v_local_header = array();
3702 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3704 // ----- Call the callback
3705 // Here I do not use call_user_func() because I need to send a reference to the
3707 // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
3708 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT
](PCLZIP_CB_PRE_EXTRACT
, $v_local_header);
3709 if ($v_result == 0) {
3710 // ----- Change the file status
3711 $p_entry['status'] = "skipped";
3715 // ----- Look for abort result
3716 if ($v_result == 2) {
3717 // ----- This status is internal and will be changed in 'skipped'
3718 $p_entry['status'] = "aborted";
3719 $v_result = PCLZIP_ERR_USER_ABORTED
;
3722 // ----- Update the informations
3723 // Only some fields can be modified
3724 $p_entry['filename'] = $v_local_header['filename'];
3728 // ----- Look if extraction should be done
3729 if ($p_entry['status'] == 'ok') {
3731 // ----- Look for specific actions while the file exist
3732 if (file_exists($p_entry['filename']))
3735 // ----- Look if file is a directory
3736 if (is_dir($p_entry['filename']))
3739 // ----- Change the file status
3740 $p_entry['status'] = "already_a_directory";
3742 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3743 // For historical reason first PclZip implementation does not stop
3744 // when this kind of error occurs.
3745 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR
]))
3746 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR
]===true)) {
3748 PclZip
::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY
,
3749 "Filename '".$p_entry['filename']."' is "
3750 ."already used by an existing directory");
3752 return PclZip
::errorCode();
3755 // ----- Look if file is write protected
3756 else if (!is_writeable($p_entry['filename']))
3759 // ----- Change the file status
3760 $p_entry['status'] = "write_protected";
3762 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3763 // For historical reason first PclZip implementation does not stop
3764 // when this kind of error occurs.
3765 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR
]))
3766 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR
]===true)) {
3768 PclZip
::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL
,
3769 "Filename '".$p_entry['filename']."' exists "
3770 ."and is write protected");
3772 return PclZip
::errorCode();
3776 // ----- Look if the extracted file is older
3777 else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
3779 // ----- Change the file status
3780 if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER
]))
3781 && ($p_options[PCLZIP_OPT_REPLACE_NEWER
]===true)) {
3784 $p_entry['status'] = "newer_exist";
3786 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3787 // For historical reason first PclZip implementation does not stop
3788 // when this kind of error occurs.
3789 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR
]))
3790 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR
]===true)) {
3792 PclZip
::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL
,
3793 "Newer version of '".$p_entry['filename']."' exists "
3794 ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
3796 return PclZip
::errorCode();
3804 // ----- Check the directory availability and create it if necessary
3806 if ((($p_entry['external']&0x00000010)==0x00000010) ||
(substr($p_entry['filename'], -1) == '/'))
3807 $v_dir_to_check = $p_entry['filename'];
3808 else if (!strstr($p_entry['filename'], "/"))
3809 $v_dir_to_check = "";
3811 $v_dir_to_check = dirname($p_entry['filename']);
3813 if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
3815 // ----- Change the file status
3816 $p_entry['status'] = "path_creation_fail";
3825 // ----- Look if extraction should be done
3826 if ($p_entry['status'] == 'ok') {
3828 // ----- Do the extraction (if not a folder)
3829 if (!(($p_entry['external']&0x00000010)==0x00000010))
3831 // ----- Look for not compressed file
3832 if ($p_entry['compression'] == 0) {
3834 // ----- Opening destination file
3835 if (($v_dest_file = @fopen
($p_entry['filename'], 'wb')) == 0)
3838 // ----- Change the file status
3839 $p_entry['status'] = "write_error";
3846 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3847 $v_size = $p_entry['compressed_size'];
3848 while ($v_size != 0)
3850 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
3851 $v_buffer = @fread
($this->zip_fd
, $v_read_size);
3852 /* Try to speed up the code
3853 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
3854 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
3856 @fwrite
($v_dest_file, $v_buffer, $v_read_size);
3857 $v_size -= $v_read_size;
3860 // ----- Closing the destination file
3861 fclose($v_dest_file);
3863 // ----- Change the file mtime
3864 touch($p_entry['filename'], $p_entry['mtime']);
3870 // Need to be finished
3871 if (($p_entry['flag'] & 1) == 1) {
3872 PclZip
::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION
, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
3873 return PclZip
::errorCode();
3877 // ----- Look for using temporary file to unzip
3878 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF
]))
3879 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON
])
3880 ||
(isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
])
3881 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD
] <= $p_entry['size'])) ) ) {
3882 $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
3883 if ($v_result < PCLZIP_ERR_NO_ERROR
) {
3888 // ----- Look for extract in memory
3892 // ----- Read the compressed file in a buffer (one shot)
3893 $v_buffer = @fread
($this->zip_fd
, $p_entry['compressed_size']);
3895 // ----- Decompress the file
3896 $v_file_content = @gzinflate
($v_buffer);
3898 if ($v_file_content === FALSE) {
3900 // ----- Change the file status
3902 $p_entry['status'] = "error";
3907 // ----- Opening destination file
3908 if (($v_dest_file = @fopen
($p_entry['filename'], 'wb')) == 0) {
3910 // ----- Change the file status
3911 $p_entry['status'] = "write_error";
3916 // ----- Write the uncompressed data
3917 @fwrite
($v_dest_file, $v_file_content, $p_entry['size']);
3918 unset($v_file_content);
3920 // ----- Closing the destination file
3921 @fclose
($v_dest_file);
3925 // ----- Change the file mtime
3926 @touch
($p_entry['filename'], $p_entry['mtime']);
3929 // ----- Look for chmod option
3930 if (isset($p_options[PCLZIP_OPT_SET_CHMOD
])) {
3932 // ----- Change the mode of the file
3933 @chmod
($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD
]);
3939 // ----- Change abort status
3940 if ($p_entry['status'] == "aborted") {
3941 $p_entry['status'] = "skipped";
3944 // ----- Look for post-extract callback
3945 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT
])) {
3947 // ----- Generate a local information
3948 $v_local_header = array();
3949 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3951 // ----- Call the callback
3952 // Here I do not use call_user_func() because I need to send a reference to the
3954 // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
3955 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT
](PCLZIP_CB_POST_EXTRACT
, $v_local_header);
3957 // ----- Look for abort result
3958 if ($v_result == 2) {
3959 $v_result = PCLZIP_ERR_USER_ABORTED
;
3966 // --------------------------------------------------------------------------------
3968 // --------------------------------------------------------------------------------
3969 // Function : privExtractFileUsingTempFile()
3973 // --------------------------------------------------------------------------------
3974 function privExtractFileUsingTempFile(&$p_entry, &$p_options)
3978 // ----- Creates a temporary file
3979 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR
.uniqid('pclzip-').'.gz';
3980 if (($v_dest_file = @fopen
($v_gzip_temp_name, "wb")) == 0) {
3982 PclZip
::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL
, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
3983 return PclZip
::errorCode();
3987 // ----- Write gz file format header
3988 $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
3989 @fwrite
($v_dest_file, $v_binary_data, 10);
3991 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3992 $v_size = $p_entry['compressed_size'];
3993 while ($v_size != 0)
3995 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
3996 $v_buffer = @fread
($this->zip_fd
, $v_read_size);
3997 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3998 @fwrite
($v_dest_file, $v_buffer, $v_read_size);
3999 $v_size -= $v_read_size;
4002 // ----- Write gz file format footer
4003 $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
4004 @fwrite
($v_dest_file, $v_binary_data, 8);
4006 // ----- Close the temporary file
4007 @fclose
($v_dest_file);
4009 // ----- Opening destination file
4010 if (($v_dest_file = @fopen
($p_entry['filename'], 'wb')) == 0) {
4011 $p_entry['status'] = "write_error";
4015 // ----- Open the temporary gz file
4016 if (($v_src_file = @gzopen
($v_gzip_temp_name, 'rb')) == 0) {
4017 @fclose
($v_dest_file);
4018 $p_entry['status'] = "read_error";
4019 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
4020 return PclZip
::errorCode();
4024 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
4025 $v_size = $p_entry['size'];
4026 while ($v_size != 0) {
4027 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
4028 $v_buffer = @gzread
($v_src_file, $v_read_size);
4029 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
4030 @fwrite
($v_dest_file, $v_buffer, $v_read_size);
4031 $v_size -= $v_read_size;
4033 @fclose
($v_dest_file);
4034 @gzclose
($v_src_file);
4036 // ----- Delete the temporary file
4037 @unlink
($v_gzip_temp_name);
4042 // --------------------------------------------------------------------------------
4044 // --------------------------------------------------------------------------------
4045 // Function : privExtractFileInOutput()
4049 // --------------------------------------------------------------------------------
4050 function privExtractFileInOutput(&$p_entry, &$p_options)
4054 // ----- Read the file header
4055 if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
4060 // ----- Check that the file header is coherent with $p_entry info
4061 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4065 // ----- Look for pre-extract callback
4066 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT
])) {
4068 // ----- Generate a local information
4069 $v_local_header = array();
4070 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4072 // ----- Call the callback
4073 // Here I do not use call_user_func() because I need to send a reference to the
4075 // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
4076 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT
](PCLZIP_CB_PRE_EXTRACT
, $v_local_header);
4077 if ($v_result == 0) {
4078 // ----- Change the file status
4079 $p_entry['status'] = "skipped";
4083 // ----- Look for abort result
4084 if ($v_result == 2) {
4085 // ----- This status is internal and will be changed in 'skipped'
4086 $p_entry['status'] = "aborted";
4087 $v_result = PCLZIP_ERR_USER_ABORTED
;
4090 // ----- Update the informations
4091 // Only some fields can be modified
4092 $p_entry['filename'] = $v_local_header['filename'];
4097 // ----- Look if extraction should be done
4098 if ($p_entry['status'] == 'ok') {
4100 // ----- Do the extraction (if not a folder)
4101 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
4102 // ----- Look for not compressed file
4103 if ($p_entry['compressed_size'] == $p_entry['size']) {
4105 // ----- Read the file in a buffer (one shot)
4106 $v_buffer = @fread
($this->zip_fd
, $p_entry['compressed_size']);
4108 // ----- Send the file to the output
4114 // ----- Read the compressed file in a buffer (one shot)
4115 $v_buffer = @fread
($this->zip_fd
, $p_entry['compressed_size']);
4117 // ----- Decompress the file
4118 $v_file_content = gzinflate($v_buffer);
4121 // ----- Send the file to the output
4122 echo $v_file_content;
4123 unset($v_file_content);
4128 // ----- Change abort status
4129 if ($p_entry['status'] == "aborted") {
4130 $p_entry['status'] = "skipped";
4133 // ----- Look for post-extract callback
4134 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT
])) {
4136 // ----- Generate a local information
4137 $v_local_header = array();
4138 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4140 // ----- Call the callback
4141 // Here I do not use call_user_func() because I need to send a reference to the
4143 // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
4144 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT
](PCLZIP_CB_POST_EXTRACT
, $v_local_header);
4146 // ----- Look for abort result
4147 if ($v_result == 2) {
4148 $v_result = PCLZIP_ERR_USER_ABORTED
;
4154 // --------------------------------------------------------------------------------
4156 // --------------------------------------------------------------------------------
4157 // Function : privExtractFileAsString()
4161 // --------------------------------------------------------------------------------
4162 function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
4166 // ----- Read the file header
4167 $v_header = array();
4168 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
4175 // ----- Check that the file header is coherent with $p_entry info
4176 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4180 // ----- Look for pre-extract callback
4181 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT
])) {
4183 // ----- Generate a local information
4184 $v_local_header = array();
4185 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4187 // ----- Call the callback
4188 // Here I do not use call_user_func() because I need to send a reference to the
4190 // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
4191 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT
](PCLZIP_CB_PRE_EXTRACT
, $v_local_header);
4192 if ($v_result == 0) {
4193 // ----- Change the file status
4194 $p_entry['status'] = "skipped";
4198 // ----- Look for abort result
4199 if ($v_result == 2) {
4200 // ----- This status is internal and will be changed in 'skipped'
4201 $p_entry['status'] = "aborted";
4202 $v_result = PCLZIP_ERR_USER_ABORTED
;
4205 // ----- Update the informations
4206 // Only some fields can be modified
4207 $p_entry['filename'] = $v_local_header['filename'];
4211 // ----- Look if extraction should be done
4212 if ($p_entry['status'] == 'ok') {
4214 // ----- Do the extraction (if not a folder)
4215 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
4216 // ----- Look for not compressed file
4217 // if ($p_entry['compressed_size'] == $p_entry['size'])
4218 if ($p_entry['compression'] == 0) {
4220 // ----- Reading the file
4221 $p_string = @fread
($this->zip_fd
, $p_entry['compressed_size']);
4225 // ----- Reading the file
4226 $v_data = @fread
($this->zip_fd
, $p_entry['compressed_size']);
4228 // ----- Decompress the file
4229 if (($p_string = @gzinflate
($v_data)) === FALSE) {
4237 // TBC : error : can not extract a folder in a string
4242 // ----- Change abort status
4243 if ($p_entry['status'] == "aborted") {
4244 $p_entry['status'] = "skipped";
4247 // ----- Look for post-extract callback
4248 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT
])) {
4250 // ----- Generate a local information
4251 $v_local_header = array();
4252 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4254 // ----- Swap the content to header
4255 $v_local_header['content'] = $p_string;
4258 // ----- Call the callback
4259 // Here I do not use call_user_func() because I need to send a reference to the
4261 // eval('$v_result = '.$p_options[PCLZIP_CB_POST_EXTRACT].'(PCLZIP_CB_POST_EXTRACT, $v_local_header);');
4262 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT
](PCLZIP_CB_POST_EXTRACT
, $v_local_header);
4264 // ----- Swap back the content to header
4265 $p_string = $v_local_header['content'];
4266 unset($v_local_header['content']);
4268 // ----- Look for abort result
4269 if ($v_result == 2) {
4270 $v_result = PCLZIP_ERR_USER_ABORTED
;
4277 // --------------------------------------------------------------------------------
4279 // --------------------------------------------------------------------------------
4280 // Function : privReadFileHeader()
4284 // --------------------------------------------------------------------------------
4285 function privReadFileHeader(&$p_header)
4289 // ----- Read the 4 bytes signature
4290 $v_binary_data = @fread
($this->zip_fd
, 4);
4291 $v_data = unpack('Vid', $v_binary_data);
4293 // ----- Check signature
4294 if ($v_data['id'] != 0x04034b50)
4298 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'Invalid archive structure');
4301 return PclZip
::errorCode();
4304 // ----- Read the first 42 bytes of the header
4305 $v_binary_data = fread($this->zip_fd
, 26);
4307 // ----- Look for invalid block size
4308 if (strlen($v_binary_data) != 26)
4310 $p_header['filename'] = "";
4311 $p_header['status'] = "invalid_header";
4314 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, "Invalid block size : ".strlen($v_binary_data));
4317 return PclZip
::errorCode();
4320 // ----- Extract the values
4321 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
4323 // ----- Get filename
4324 $p_header['filename'] = fread($this->zip_fd
, $v_data['filename_len']);
4326 // ----- Get extra_fields
4327 if ($v_data['extra_len'] != 0) {
4328 $p_header['extra'] = fread($this->zip_fd
, $v_data['extra_len']);
4331 $p_header['extra'] = '';
4334 // ----- Extract properties
4335 $p_header['version_extracted'] = $v_data['version'];
4336 $p_header['compression'] = $v_data['compression'];
4337 $p_header['size'] = $v_data['size'];
4338 $p_header['compressed_size'] = $v_data['compressed_size'];
4339 $p_header['crc'] = $v_data['crc'];
4340 $p_header['flag'] = $v_data['flag'];
4341 $p_header['filename_len'] = $v_data['filename_len'];
4343 // ----- Recuperate date in UNIX format
4344 $p_header['mdate'] = $v_data['mdate'];
4345 $p_header['mtime'] = $v_data['mtime'];
4346 if ($p_header['mdate'] && $p_header['mtime'])
4348 // ----- Extract time
4349 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4350 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4351 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4353 // ----- Extract date
4354 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) +
1980;
4355 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4356 $v_day = $p_header['mdate'] & 0x001F;
4358 // ----- Get UNIX date format
4359 $p_header['mtime'] = @mktime
($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4364 $p_header['mtime'] = time();
4368 //for(reset($v_data); $key = key($v_data); next($v_data)) {
4371 // ----- Set the stored filename
4372 $p_header['stored_filename'] = $p_header['filename'];
4374 // ----- Set the status field
4375 $p_header['status'] = "ok";
4380 // --------------------------------------------------------------------------------
4382 // --------------------------------------------------------------------------------
4383 // Function : privReadCentralFileHeader()
4387 // --------------------------------------------------------------------------------
4388 function privReadCentralFileHeader(&$p_header)
4392 // ----- Read the 4 bytes signature
4393 $v_binary_data = @fread
($this->zip_fd
, 4);
4394 $v_data = unpack('Vid', $v_binary_data);
4396 // ----- Check signature
4397 if ($v_data['id'] != 0x02014b50)
4401 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'Invalid archive structure');
4404 return PclZip
::errorCode();
4407 // ----- Read the first 42 bytes of the header
4408 $v_binary_data = fread($this->zip_fd
, 42);
4410 // ----- Look for invalid block size
4411 if (strlen($v_binary_data) != 42)
4413 $p_header['filename'] = "";
4414 $p_header['status'] = "invalid_header";
4417 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, "Invalid block size : ".strlen($v_binary_data));
4420 return PclZip
::errorCode();
4423 // ----- Extract the values
4424 $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
4426 // ----- Get filename
4427 if ($p_header['filename_len'] != 0)
4428 $p_header['filename'] = fread($this->zip_fd
, $p_header['filename_len']);
4430 $p_header['filename'] = '';
4433 if ($p_header['extra_len'] != 0)
4434 $p_header['extra'] = fread($this->zip_fd
, $p_header['extra_len']);
4436 $p_header['extra'] = '';
4438 // ----- Get comment
4439 if ($p_header['comment_len'] != 0)
4440 $p_header['comment'] = fread($this->zip_fd
, $p_header['comment_len']);
4442 $p_header['comment'] = '';
4444 // ----- Extract properties
4446 // ----- Recuperate date in UNIX format
4447 //if ($p_header['mdate'] && $p_header['mtime'])
4448 // TBC : bug : this was ignoring time with 0/0/0
4451 // ----- Extract time
4452 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4453 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4454 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4456 // ----- Extract date
4457 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) +
1980;
4458 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4459 $v_day = $p_header['mdate'] & 0x001F;
4461 // ----- Get UNIX date format
4462 $p_header['mtime'] = @mktime
($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4467 $p_header['mtime'] = time();
4470 // ----- Set the stored filename
4471 $p_header['stored_filename'] = $p_header['filename'];
4473 // ----- Set default status to ok
4474 $p_header['status'] = 'ok';
4476 // ----- Look if it is a directory
4477 if (substr($p_header['filename'], -1) == '/') {
4478 //$p_header['external'] = 0x41FF0010;
4479 $p_header['external'] = 0x00000010;
4486 // --------------------------------------------------------------------------------
4488 // --------------------------------------------------------------------------------
4489 // Function : privCheckFileHeaders()
4495 // --------------------------------------------------------------------------------
4496 function privCheckFileHeaders(&$p_local_header, &$p_central_header)
4500 // ----- Check the static values
4502 if ($p_local_header['filename'] != $p_central_header['filename']) {
4504 if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
4506 if ($p_local_header['flag'] != $p_central_header['flag']) {
4508 if ($p_local_header['compression'] != $p_central_header['compression']) {
4510 if ($p_local_header['mtime'] != $p_central_header['mtime']) {
4512 if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
4515 // ----- Look for flag bit 3
4516 if (($p_local_header['flag'] & 8) == 8) {
4517 $p_local_header['size'] = $p_central_header['size'];
4518 $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
4519 $p_local_header['crc'] = $p_central_header['crc'];
4525 // --------------------------------------------------------------------------------
4527 // --------------------------------------------------------------------------------
4528 // Function : privReadEndCentralDir()
4532 // --------------------------------------------------------------------------------
4533 function privReadEndCentralDir(&$p_central_dir)
4537 // ----- Go to the end of the zip file
4538 $v_size = filesize($this->zipname
);
4539 @fseek
($this->zip_fd
, $v_size);
4540 if (@ftell
($this->zip_fd
) != $v_size)
4543 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'Unable to go to the end of the archive \''.$this->zipname
.'\'');
4546 return PclZip
::errorCode();
4549 // ----- First try : look if this is an archive with no commentaries (most of the time)
4550 // in this case the end of central dir is at 22 bytes of the file end
4553 @fseek
($this->zip_fd
, $v_size-22);
4554 if (($v_pos = @ftell
($this->zip_fd
)) != ($v_size-22))
4557 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'Unable to seek back to the middle of the archive \''.$this->zipname
.'\'');
4560 return PclZip
::errorCode();
4563 // ----- Read for bytes
4564 $v_binary_data = @fread
($this->zip_fd
, 4);
4565 $v_data = @unpack
('Vid', $v_binary_data);
4567 // ----- Check signature
4568 if ($v_data['id'] == 0x06054b50) {
4572 $v_pos = ftell($this->zip_fd
);
4575 // ----- Go back to the maximum possible size of the Central Dir End Record
4577 $v_maximum_size = 65557; // 0xFFFF + 22;
4578 if ($v_maximum_size > $v_size)
4579 $v_maximum_size = $v_size;
4580 @fseek
($this->zip_fd
, $v_size-$v_maximum_size);
4581 if (@ftell
($this->zip_fd
) != ($v_size-$v_maximum_size))
4584 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, 'Unable to seek back to the middle of the archive \''.$this->zipname
.'\'');
4587 return PclZip
::errorCode();
4590 // ----- Read byte per byte in order to find the signature
4591 $v_pos = ftell($this->zip_fd
);
4592 $v_bytes = 0x00000000;
4593 while ($v_pos < $v_size)
4595 // ----- Read a byte
4596 $v_byte = @fread
($this->zip_fd
, 1);
4598 // ----- Add the byte
4599 //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
4600 // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
4601 // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
4602 $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) |
Ord($v_byte);
4604 // ----- Compare the bytes
4605 if ($v_bytes == 0x504b0506)
4614 // ----- Look if not found end of central dir
4615 if ($v_pos == $v_size)
4619 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, "Unable to find End of Central Dir Record signature");
4622 return PclZip
::errorCode();
4626 // ----- Read the first 18 bytes of the header
4627 $v_binary_data = fread($this->zip_fd
, 18);
4629 // ----- Look for invalid block size
4630 if (strlen($v_binary_data) != 18)
4634 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4637 return PclZip
::errorCode();
4640 // ----- Extract the values
4641 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
4643 // ----- Check the global size
4644 if (($v_pos +
$v_data['comment_size'] +
18) != $v_size) {
4646 // ----- Removed in release 2.2 see readme file
4647 // The check of the file size is a little too strict.
4648 // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
4649 // While decrypted, zip has training 0 bytes
4652 PclZip
::privErrorLog(PCLZIP_ERR_BAD_FORMAT
,
4653 'The central dir is not at the end of the archive.'
4654 .' Some trailing bytes exists after the archive.');
4657 return PclZip
::errorCode();
4661 // ----- Get comment
4662 if ($v_data['comment_size'] != 0) {
4663 $p_central_dir['comment'] = fread($this->zip_fd
, $v_data['comment_size']);
4666 $p_central_dir['comment'] = '';
4668 $p_central_dir['entries'] = $v_data['entries'];
4669 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
4670 $p_central_dir['offset'] = $v_data['offset'];
4671 $p_central_dir['size'] = $v_data['size'];
4672 $p_central_dir['disk'] = $v_data['disk'];
4673 $p_central_dir['disk_start'] = $v_data['disk_start'];
4676 //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
4682 // --------------------------------------------------------------------------------
4684 // --------------------------------------------------------------------------------
4685 // Function : privDeleteByRule()
4689 // --------------------------------------------------------------------------------
4690 function privDeleteByRule(&$p_result_list, &$p_options)
4693 $v_list_detail = array();
4695 // ----- Open the zip file
4696 if (($v_result=$this->privOpenFd('rb')) != 1)
4702 // ----- Read the central directory informations
4703 $v_central_dir = array();
4704 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
4706 $this->privCloseFd();
4710 // ----- Go to beginning of File
4711 @rewind
($this->zip_fd
);
4713 // ----- Scan all the files
4714 // ----- Start at beginning of Central Dir
4715 $v_pos_entry = $v_central_dir['offset'];
4716 @rewind
($this->zip_fd
);
4717 if (@fseek
($this->zip_fd
, $v_pos_entry))
4719 // ----- Close the zip file
4720 $this->privCloseFd();
4723 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
4726 return PclZip
::errorCode();
4729 // ----- Read each entry
4730 $v_header_list = array();
4732 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++
)
4735 // ----- Read the file header
4736 $v_header_list[$v_nb_extracted] = array();
4737 if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
4739 // ----- Close the zip file
4740 $this->privCloseFd();
4746 // ----- Store the index
4747 $v_header_list[$v_nb_extracted]['index'] = $i;
4749 // ----- Look for the specific extract rules
4752 // ----- Look for extract by name rule
4753 if ( (isset($p_options[PCLZIP_OPT_BY_NAME
]))
4754 && ($p_options[PCLZIP_OPT_BY_NAME
] != 0)) {
4756 // ----- Look if the filename is in the list
4757 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME
])) && (!$v_found); $j++
) {
4759 // ----- Look for a directory
4760 if (substr($p_options[PCLZIP_OPT_BY_NAME
][$j], -1) == "/") {
4762 // ----- Look if the directory is in the filename path
4763 if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME
][$j]))
4764 && (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])) {
4767 elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
4768 && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME
][$j])) {
4772 // ----- Look for a filename
4773 elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME
][$j]) {
4779 // ----- Look for extract by ereg rule
4780 // ereg() is deprecated with PHP 5.3
4782 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
4783 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
4785 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4791 // ----- Look for extract by preg rule
4792 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG
]))
4793 && ($p_options[PCLZIP_OPT_BY_PREG
] != "")) {
4795 if (preg_match($p_options[PCLZIP_OPT_BY_PREG
], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4800 // ----- Look for extract by index rule
4801 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX
]))
4802 && ($p_options[PCLZIP_OPT_BY_INDEX
] != 0)) {
4804 // ----- Look if the index is in the list
4805 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX
])) && (!$v_found); $j++
) {
4807 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['end'])) {
4810 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX
][$j]['end']) {
4814 if ($p_options[PCLZIP_OPT_BY_INDEX
][$j]['start']>$i) {
4823 // ----- Look for deletion
4826 unset($v_header_list[$v_nb_extracted]);
4834 // ----- Look if something need to be deleted
4835 if ($v_nb_extracted > 0) {
4837 // ----- Creates a temporay file
4838 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR
.uniqid('pclzip-').'.tmp';
4840 // ----- Creates a temporary zip archive
4841 $v_temp_zip = new PclZip($v_zip_temp_name);
4843 // ----- Open the temporary zip file in write mode
4844 if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
4845 $this->privCloseFd();
4851 // ----- Look which file need to be kept
4852 for ($i=0; $i<sizeof($v_header_list); $i++
) {
4854 // ----- Calculate the position of the header
4855 @rewind
($this->zip_fd
);
4856 if (@fseek
($this->zip_fd
, $v_header_list[$i]['offset'])) {
4857 // ----- Close the zip file
4858 $this->privCloseFd();
4859 $v_temp_zip->privCloseFd();
4860 @unlink
($v_zip_temp_name);
4863 PclZip
::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP
, 'Invalid archive size');
4866 return PclZip
::errorCode();
4869 // ----- Read the file header
4870 $v_local_header = array();
4871 if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
4872 // ----- Close the zip file
4873 $this->privCloseFd();
4874 $v_temp_zip->privCloseFd();
4875 @unlink
($v_zip_temp_name);
4881 // ----- Check that local file header is same as central file header
4882 if ($this->privCheckFileHeaders($v_local_header,
4883 $v_header_list[$i]) != 1) {
4886 unset($v_local_header);
4888 // ----- Write the file header
4889 if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
4890 // ----- Close the zip file
4891 $this->privCloseFd();
4892 $v_temp_zip->privCloseFd();
4893 @unlink
($v_zip_temp_name);
4899 // ----- Read/write the data block
4900 if (($v_result = PclZipUtilCopyBlock($this->zip_fd
, $v_temp_zip->zip_fd
, $v_header_list[$i]['compressed_size'])) != 1) {
4901 // ----- Close the zip file
4902 $this->privCloseFd();
4903 $v_temp_zip->privCloseFd();
4904 @unlink
($v_zip_temp_name);
4911 // ----- Store the offset of the central dir
4912 $v_offset = @ftell
($v_temp_zip->zip_fd
);
4914 // ----- Re-Create the Central Dir files header
4915 for ($i=0; $i<sizeof($v_header_list); $i++
) {
4916 // ----- Create the file header
4917 if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
4918 $v_temp_zip->privCloseFd();
4919 $this->privCloseFd();
4920 @unlink
($v_zip_temp_name);
4926 // ----- Transform the header to a 'usable' info
4927 $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
4931 // ----- Zip file comment
4933 if (isset($p_options[PCLZIP_OPT_COMMENT
])) {
4934 $v_comment = $p_options[PCLZIP_OPT_COMMENT
];
4937 // ----- Calculate the size of the central header
4938 $v_size = @ftell
($v_temp_zip->zip_fd
)-$v_offset;
4940 // ----- Create the central dir footer
4941 if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
4942 // ----- Reset the file list
4943 unset($v_header_list);
4944 $v_temp_zip->privCloseFd();
4945 $this->privCloseFd();
4946 @unlink
($v_zip_temp_name);
4953 $v_temp_zip->privCloseFd();
4954 $this->privCloseFd();
4956 // ----- Delete the zip file
4957 // TBC : I should test the result ...
4958 @unlink
($this->zipname
);
4960 // ----- Rename the temporary file
4961 // TBC : I should test the result ...
4962 //@rename($v_zip_temp_name, $this->zipname);
4963 PclZipUtilRename($v_zip_temp_name, $this->zipname
);
4965 // ----- Destroy the temporary archive
4969 // ----- Remove every files : reset the file
4970 else if ($v_central_dir['entries'] != 0) {
4971 $this->privCloseFd();
4973 if (($v_result = $this->privOpenFd('wb')) != 1) {
4977 if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
4981 $this->privCloseFd();
4987 // --------------------------------------------------------------------------------
4989 // --------------------------------------------------------------------------------
4990 // Function : privDirCheck()
4992 // Check if a directory exists, if not it creates it and all the parents directory
4993 // which may be useful.
4995 // $p_dir : Directory path to check.
4998 // -1 : Unable to create directory
4999 // --------------------------------------------------------------------------------
5000 function privDirCheck($p_dir, $p_is_dir=false)
5005 // ----- Remove the final '/'
5006 if (($p_is_dir) && (substr($p_dir, -1)=='/'))
5008 $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
5011 // ----- Check the directory availability
5012 if ((is_dir($p_dir)) ||
($p_dir == ""))
5017 // ----- Extract parent directory
5018 $p_parent_dir = dirname($p_dir);
5020 // ----- Just a check
5021 if ($p_parent_dir != $p_dir)
5023 // ----- Look for parent directory
5024 if ($p_parent_dir != "")
5026 if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
5033 // ----- Create the directory
5034 if (!@mkdir
($p_dir, 0777))
5037 PclZip
::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL
, "Unable to create directory '$p_dir'");
5040 return PclZip
::errorCode();
5046 // --------------------------------------------------------------------------------
5048 // --------------------------------------------------------------------------------
5049 // Function : privMerge()
5051 // If $p_archive_to_add does not exist, the function exit with a success result.
5054 // --------------------------------------------------------------------------------
5055 function privMerge(&$p_archive_to_add)
5059 // ----- Look if the archive_to_add exists
5060 if (!is_file($p_archive_to_add->zipname
))
5063 // ----- Nothing to merge, so merge is a success
5070 // ----- Look if the archive exists
5071 if (!is_file($this->zipname
))
5074 // ----- Do a duplicate
5075 $v_result = $this->privDuplicate($p_archive_to_add->zipname
);
5081 // ----- Open the zip file
5082 if (($v_result=$this->privOpenFd('rb')) != 1)
5088 // ----- Read the central directory informations
5089 $v_central_dir = array();
5090 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
5092 $this->privCloseFd();
5096 // ----- Go to beginning of File
5097 @rewind
($this->zip_fd
);
5099 // ----- Open the archive_to_add file
5100 if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
5102 $this->privCloseFd();
5108 // ----- Read the central directory informations
5109 $v_central_dir_to_add = array();
5110 if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
5112 $this->privCloseFd();
5113 $p_archive_to_add->privCloseFd();
5118 // ----- Go to beginning of File
5119 @rewind
($p_archive_to_add->zip_fd
);
5121 // ----- Creates a temporay file
5122 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR
.uniqid('pclzip-').'.tmp';
5124 // ----- Open the temporary file in write mode
5125 if (($v_zip_temp_fd = @fopen
($v_zip_temp_name, 'wb')) == 0)
5127 $this->privCloseFd();
5128 $p_archive_to_add->privCloseFd();
5130 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
5133 return PclZip
::errorCode();
5136 // ----- Copy the files from the archive to the temporary file
5137 // TBC : Here I should better append the file and go back to erase the central dir
5138 $v_size = $v_central_dir['offset'];
5139 while ($v_size != 0)
5141 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
5142 $v_buffer = fread($this->zip_fd
, $v_read_size);
5143 @fwrite
($v_zip_temp_fd, $v_buffer, $v_read_size);
5144 $v_size -= $v_read_size;
5147 // ----- Copy the files from the archive_to_add into the temporary file
5148 $v_size = $v_central_dir_to_add['offset'];
5149 while ($v_size != 0)
5151 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
5152 $v_buffer = fread($p_archive_to_add->zip_fd
, $v_read_size);
5153 @fwrite
($v_zip_temp_fd, $v_buffer, $v_read_size);
5154 $v_size -= $v_read_size;
5157 // ----- Store the offset of the central dir
5158 $v_offset = @ftell
($v_zip_temp_fd);
5160 // ----- Copy the block of file headers from the old archive
5161 $v_size = $v_central_dir['size'];
5162 while ($v_size != 0)
5164 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
5165 $v_buffer = @fread
($this->zip_fd
, $v_read_size);
5166 @fwrite
($v_zip_temp_fd, $v_buffer, $v_read_size);
5167 $v_size -= $v_read_size;
5170 // ----- Copy the block of file headers from the archive_to_add
5171 $v_size = $v_central_dir_to_add['size'];
5172 while ($v_size != 0)
5174 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
5175 $v_buffer = @fread
($p_archive_to_add->zip_fd
, $v_read_size);
5176 @fwrite
($v_zip_temp_fd, $v_buffer, $v_read_size);
5177 $v_size -= $v_read_size;
5180 // ----- Merge the file comments
5181 $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
5183 // ----- Calculate the size of the (new) central header
5184 $v_size = @ftell
($v_zip_temp_fd)-$v_offset;
5186 // ----- Swap the file descriptor
5187 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
5188 // the following methods on the temporary fil and not the real archive fd
5189 $v_swap = $this->zip_fd
;
5190 $this->zip_fd
= $v_zip_temp_fd;
5191 $v_zip_temp_fd = $v_swap;
5193 // ----- Create the central dir footer
5194 if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+
$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
5196 $this->privCloseFd();
5197 $p_archive_to_add->privCloseFd();
5198 @fclose
($v_zip_temp_fd);
5199 $this->zip_fd
= null;
5201 // ----- Reset the file list
5202 unset($v_header_list);
5208 // ----- Swap back the file descriptor
5209 $v_swap = $this->zip_fd
;
5210 $this->zip_fd
= $v_zip_temp_fd;
5211 $v_zip_temp_fd = $v_swap;
5214 $this->privCloseFd();
5215 $p_archive_to_add->privCloseFd();
5217 // ----- Close the temporary file
5218 @fclose
($v_zip_temp_fd);
5220 // ----- Delete the zip file
5221 // TBC : I should test the result ...
5222 @unlink
($this->zipname
);
5224 // ----- Rename the temporary file
5225 // TBC : I should test the result ...
5226 //@rename($v_zip_temp_name, $this->zipname);
5227 PclZipUtilRename($v_zip_temp_name, $this->zipname
);
5232 // --------------------------------------------------------------------------------
5234 // --------------------------------------------------------------------------------
5235 // Function : privDuplicate()
5239 // --------------------------------------------------------------------------------
5240 function privDuplicate($p_archive_filename)
5244 // ----- Look if the $p_archive_filename exists
5245 if (!is_file($p_archive_filename))
5248 // ----- Nothing to duplicate, so duplicate is a success.
5255 // ----- Open the zip file
5256 if (($v_result=$this->privOpenFd('wb')) != 1)
5262 // ----- Open the temporary file in write mode
5263 if (($v_zip_temp_fd = @fopen
($p_archive_filename, 'rb')) == 0)
5265 $this->privCloseFd();
5267 PclZip
::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL
, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
5270 return PclZip
::errorCode();
5273 // ----- Copy the files from the archive to the temporary file
5274 // TBC : Here I should better append the file and go back to erase the central dir
5275 $v_size = filesize($p_archive_filename);
5276 while ($v_size != 0)
5278 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ?
$v_size : PCLZIP_READ_BLOCK_SIZE
);
5279 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
5280 @fwrite
($this->zip_fd
, $v_buffer, $v_read_size);
5281 $v_size -= $v_read_size;
5285 $this->privCloseFd();
5287 // ----- Close the temporary file
5288 @fclose
($v_zip_temp_fd);
5293 // --------------------------------------------------------------------------------
5295 // --------------------------------------------------------------------------------
5296 // Function : privErrorLog()
5299 // --------------------------------------------------------------------------------
5300 function privErrorLog($p_error_code=0, $p_error_string='')
5302 if (PCLZIP_ERROR_EXTERNAL
== 1) {
5303 PclError($p_error_code, $p_error_string);
5306 $this->error_code
= $p_error_code;
5307 $this->error_string
= $p_error_string;
5310 // --------------------------------------------------------------------------------
5312 // --------------------------------------------------------------------------------
5313 // Function : privErrorReset()
5316 // --------------------------------------------------------------------------------
5317 function privErrorReset()
5319 if (PCLZIP_ERROR_EXTERNAL
== 1) {
5323 $this->error_code
= 0;
5324 $this->error_string
= '';
5327 // --------------------------------------------------------------------------------
5329 // --------------------------------------------------------------------------------
5330 // Function : privDisableMagicQuotes()
5334 // --------------------------------------------------------------------------------
5335 function privDisableMagicQuotes()
5339 // ----- Look if function exists
5340 if ( (!function_exists("get_magic_quotes_runtime"))
5341 ||
(!function_exists("set_magic_quotes_runtime"))) {
5345 // ----- Look if already done
5346 if ($this->magic_quotes_status
!= -1) {
5350 // ----- Get and memorize the magic_quote value
5351 $this->magic_quotes_status
= @get_magic_quotes_runtime
();
5353 // ----- Disable magic_quotes
5354 if ($this->magic_quotes_status
== 1) {
5355 @set_magic_quotes_runtime
(0);
5361 // --------------------------------------------------------------------------------
5363 // --------------------------------------------------------------------------------
5364 // Function : privSwapBackMagicQuotes()
5368 // --------------------------------------------------------------------------------
5369 function privSwapBackMagicQuotes()
5373 // ----- Look if function exists
5374 if ( (!function_exists("get_magic_quotes_runtime"))
5375 ||
(!function_exists("set_magic_quotes_runtime"))) {
5379 // ----- Look if something to do
5380 if ($this->magic_quotes_status
!= -1) {
5384 // ----- Swap back magic_quotes
5385 if ($this->magic_quotes_status
== 1) {
5386 @set_magic_quotes_runtime
($this->magic_quotes_status
);
5392 // --------------------------------------------------------------------------------
5396 // --------------------------------------------------------------------------------
5398 // --------------------------------------------------------------------------------
5399 // Function : PclZipUtilPathReduction()
5403 // --------------------------------------------------------------------------------
5404 function PclZipUtilPathReduction($p_dir)
5408 // ----- Look for not empty path
5410 // ----- Explode path by directory names
5411 $v_list = explode("/", $p_dir);
5413 // ----- Study directories from last to first
5415 for ($i=sizeof($v_list)-1; $i>=0; $i--) {
5416 // ----- Look for current path
5417 if ($v_list[$i] == ".") {
5418 // ----- Ignore this directory
5419 // Should be the first $i=0, but no check is done
5421 else if ($v_list[$i] == "..") {
5424 else if ($v_list[$i] == "") {
5425 // ----- First '/' i.e. root slash
5427 $v_result = "/".$v_result;
5429 // ----- It is an invalid path, so the path is not modified
5435 // ----- Last '/' i.e. indicates a directory
5436 else if ($i == (sizeof($v_list)-1)) {
5437 $v_result = $v_list[$i];
5439 // ----- Double '/' inside the path
5441 // ----- Ignore only the double '//' in path,
5442 // but not the first and last '/'
5446 // ----- Look for item to skip
5451 $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?
"/".$v_result:"");
5456 // ----- Look for skip
5458 while ($v_skip > 0) {
5459 $v_result = '../'.$v_result;
5468 // --------------------------------------------------------------------------------
5470 // --------------------------------------------------------------------------------
5471 // Function : PclZipUtilPathInclusion()
5473 // This function indicates if the path $p_path is under the $p_dir tree. Or,
5474 // said in an other way, if the file or sub-dir $p_path is inside the dir
5476 // The function indicates also if the path is exactly the same as the dir.
5477 // This function supports path with duplicated '/' like '//', but does not
5478 // support '.' or '..' statements.
5481 // 0 if $p_path is not inside directory $p_dir
5482 // 1 if $p_path is inside directory $p_dir
5483 // 2 if $p_path is exactly the same as $p_dir
5484 // --------------------------------------------------------------------------------
5485 function PclZipUtilPathInclusion($p_dir, $p_path)
5489 // ----- Look for path beginning by ./
5490 if ( ($p_dir == '.')
5491 ||
((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
5492 $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
5494 if ( ($p_path == '.')
5495 ||
((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
5496 $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
5499 // ----- Explode dir and path by directory separator
5500 $v_list_dir = explode("/", $p_dir);
5501 $v_list_dir_size = sizeof($v_list_dir);
5502 $v_list_path = explode("/", $p_path);
5503 $v_list_path_size = sizeof($v_list_path);
5505 // ----- Study directories paths
5508 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
5510 // ----- Look for empty dir (path reduction)
5511 if ($v_list_dir[$i] == '') {
5515 if ($v_list_path[$j] == '') {
5520 // ----- Compare the items
5521 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
5530 // ----- Look if everything seems to be the same
5532 // ----- Skip all the empty items
5533 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++
;
5534 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++
;
5536 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
5537 // ----- There are exactly the same
5540 else if ($i < $v_list_dir_size) {
5541 // ----- The path is shorter than the dir
5549 // --------------------------------------------------------------------------------
5551 // --------------------------------------------------------------------------------
5552 // Function : PclZipUtilCopyBlock()
5555 // $p_mode : read/write compression mode
5556 // 0 : src & dest normal
5557 // 1 : src gzip, dest normal
5558 // 2 : src normal, dest gzip
5559 // 3 : src & dest gzip
5561 // --------------------------------------------------------------------------------
5562 function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
5568 while ($p_size != 0)
5570 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ?
$p_size : PCLZIP_READ_BLOCK_SIZE
);
5571 $v_buffer = @fread
($p_src, $v_read_size);
5572 @fwrite
($p_dest, $v_buffer, $v_read_size);
5573 $p_size -= $v_read_size;
5576 else if ($p_mode==1)
5578 while ($p_size != 0)
5580 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ?
$p_size : PCLZIP_READ_BLOCK_SIZE
);
5581 $v_buffer = @gzread
($p_src, $v_read_size);
5582 @fwrite
($p_dest, $v_buffer, $v_read_size);
5583 $p_size -= $v_read_size;
5586 else if ($p_mode==2)
5588 while ($p_size != 0)
5590 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ?
$p_size : PCLZIP_READ_BLOCK_SIZE
);
5591 $v_buffer = @fread
($p_src, $v_read_size);
5592 @gzwrite
($p_dest, $v_buffer, $v_read_size);
5593 $p_size -= $v_read_size;
5596 else if ($p_mode==3)
5598 while ($p_size != 0)
5600 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ?
$p_size : PCLZIP_READ_BLOCK_SIZE
);
5601 $v_buffer = @gzread
($p_src, $v_read_size);
5602 @gzwrite
($p_dest, $v_buffer, $v_read_size);
5603 $p_size -= $v_read_size;
5610 // --------------------------------------------------------------------------------
5612 // --------------------------------------------------------------------------------
5613 // Function : PclZipUtilRename()
5615 // This function tries to do a simple rename() function. If it fails, it
5616 // tries to copy the $p_src file in a new $p_dest file and then unlink the
5619 // $p_src : Old filename
5620 // $p_dest : New filename
5622 // 1 on success, 0 on failure.
5623 // --------------------------------------------------------------------------------
5624 function PclZipUtilRename($p_src, $p_dest)
5628 // ----- Try to rename the files
5629 if (!@rename
($p_src, $p_dest)) {
5631 // ----- Try to copy & unlink the src
5632 if (!@copy
($p_src, $p_dest)) {
5635 else if (!@unlink
($p_src)) {
5643 // --------------------------------------------------------------------------------
5645 // --------------------------------------------------------------------------------
5646 // Function : PclZipUtilOptionText()
5648 // Translate option value in text. Mainly for debug purpose.
5650 // $p_option : the option value.
5652 // The option text value.
5653 // --------------------------------------------------------------------------------
5654 function PclZipUtilOptionText($p_option)
5657 $v_list = get_defined_constants();
5658 for (reset($v_list); $v_key = key($v_list); next($v_list)) {
5659 $v_prefix = substr($v_key, 0, 10);
5660 if (( ($v_prefix == 'PCLZIP_OPT')
5661 ||
($v_prefix == 'PCLZIP_CB_')
5662 ||
($v_prefix == 'PCLZIP_ATT'))
5663 && ($v_list[$v_key] == $p_option)) {
5668 $v_result = 'Unknown';
5672 // --------------------------------------------------------------------------------
5674 // --------------------------------------------------------------------------------
5675 // Function : PclZipUtilTranslateWinPath()
5677 // Translate windows path by replacing '\' by '/' and optionally removing
5680 // $p_path : path to translate.
5681 // $p_remove_disk_letter : true | false
5683 // The path translated.
5684 // --------------------------------------------------------------------------------
5685 function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
5687 if (stristr(php_uname(), 'windows')) {
5688 // ----- Look for potential disk letter
5689 if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
5690 $p_path = substr($p_path, $v_position+
1);
5692 // ----- Change potential windows directory separator
5693 if ((strpos($p_path, '\\') > 0) ||
(substr($p_path, 0,1) == '\\')) {
5694 $p_path = strtr($p_path, '\\', '/');
5699 // --------------------------------------------------------------------------------