2 // --------------------------------------------------------------------------------
3 // PhpConcept Library - Zip Module 2.8
4 // --------------------------------------------------------------------------------
5 // License GNU/LGPL - Vincent Blavet - March 2006
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.
24 // --------------------------------------------------------------------------------
25 // $Id: pclzip.lib.php,v 1.55 2009/04/22 07:38:36 vblavet Exp $
26 // --------------------------------------------------------------------------------
29 if (!defined('PCLZIP_READ_BLOCK_SIZE')) {
30 define( 'PCLZIP_READ_BLOCK_SIZE', 2048 );
33 // ----- File list separator
34 // In version 1.x of PclZip, the separator for file list is a space
35 // (which is not a very smart choice, specifically for windows paths !).
36 // A better separator should be a comma (,). This constant gives you the
37 // abilty to change that.
38 // However notice that changing this value, may have impact on existing
39 // scripts, using space separated filenames.
40 // Recommanded values for compatibility with older versions :
41 //define( 'PCLZIP_SEPARATOR', ' ' );
42 // Recommanded values for smart separation of filenames.
43 if (!defined('PCLZIP_SEPARATOR')) {
44 define( 'PCLZIP_SEPARATOR', ',' );
47 // ----- Error configuration
48 // 0 : PclZip Class integrated error handling
49 // 1 : PclError external library error handling. By enabling this
50 // you must ensure that you have included PclError library.
51 // [2,...] : reserved for futur use
52 if (!defined('PCLZIP_ERROR_EXTERNAL')) {
53 define( 'PCLZIP_ERROR_EXTERNAL', 0 );
56 // ----- Optional static temporary directory
57 // By default temporary files are generated in the script current
60 // - MUST BE terminated by a '/'.
61 // - MUST be a valid, already created directory
63 // define( 'PCLZIP_TEMPORARY_DIR', '/temp/' );
64 // define( 'PCLZIP_TEMPORARY_DIR', 'C:/Temp/' );
65 if (!defined('PCLZIP_TEMPORARY_DIR')) {
66 define( 'PCLZIP_TEMPORARY_DIR', '' );
69 // ----- Optional threshold ratio for use of temporary files
70 // Pclzip sense the size of the file to add/extract and decide to
71 // use or not temporary file. The algorythm is looking for
72 // memory_limit of PHP and apply a ratio.
73 // threshold = memory_limit * ratio.
74 // Recommended values are under 0.5. Default 0.47.
76 // define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.5 );
77 if (!defined('PCLZIP_TEMPORARY_FILE_RATIO')) {
78 define( 'PCLZIP_TEMPORARY_FILE_RATIO', 0.47 );
81 // --------------------------------------------------------------------------------
82 // ***** UNDER THIS LINE NOTHING NEEDS TO BE MODIFIED *****
83 // --------------------------------------------------------------------------------
85 // ----- Global variables
86 $g_pclzip_version = "2.8";
89 // -1 : Unable to open file in binary write mode
90 // -2 : Unable to open file in binary read mode
91 // -3 : Invalid parameters
92 // -4 : File does not exist
93 // -5 : Filename is too long (max. 255)
94 // -6 : Not a valid zip file
95 // -7 : Invalid extracted file size
96 // -8 : Unable to create directory
97 // -9 : Invalid archive extension
98 // -10 : Invalid archive format
99 // -11 : Unable to delete file (unlink)
100 // -12 : Unable to rename file (rename)
101 // -13 : Invalid header checksum
102 // -14 : Invalid archive size
103 define( 'PCLZIP_ERR_USER_ABORTED', 2 );
104 define( 'PCLZIP_ERR_NO_ERROR', 0 );
105 define( 'PCLZIP_ERR_WRITE_OPEN_FAIL', -1 );
106 define( 'PCLZIP_ERR_READ_OPEN_FAIL', -2 );
107 define( 'PCLZIP_ERR_INVALID_PARAMETER', -3 );
108 define( 'PCLZIP_ERR_MISSING_FILE', -4 );
109 define( 'PCLZIP_ERR_FILENAME_TOO_LONG', -5 );
110 define( 'PCLZIP_ERR_INVALID_ZIP', -6 );
111 define( 'PCLZIP_ERR_BAD_EXTRACTED_FILE', -7 );
112 define( 'PCLZIP_ERR_DIR_CREATE_FAIL', -8 );
113 define( 'PCLZIP_ERR_BAD_EXTENSION', -9 );
114 define( 'PCLZIP_ERR_BAD_FORMAT', -10 );
115 define( 'PCLZIP_ERR_DELETE_FILE_FAIL', -11 );
116 define( 'PCLZIP_ERR_RENAME_FILE_FAIL', -12 );
117 define( 'PCLZIP_ERR_BAD_CHECKSUM', -13 );
118 define( 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
119 define( 'PCLZIP_ERR_MISSING_OPTION_VALUE', -15 );
120 define( 'PCLZIP_ERR_INVALID_OPTION_VALUE', -16 );
121 define( 'PCLZIP_ERR_ALREADY_A_DIRECTORY', -17 );
122 define( 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION', -18 );
123 define( 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION', -19 );
124 define( 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE', -20 );
125 define( 'PCLZIP_ERR_DIRECTORY_RESTRICTION', -21 );
127 // ----- Options values
128 define( 'PCLZIP_OPT_PATH', 77001 );
129 define( 'PCLZIP_OPT_ADD_PATH', 77002 );
130 define( 'PCLZIP_OPT_REMOVE_PATH', 77003 );
131 define( 'PCLZIP_OPT_REMOVE_ALL_PATH', 77004 );
132 define( 'PCLZIP_OPT_SET_CHMOD', 77005 );
133 define( 'PCLZIP_OPT_EXTRACT_AS_STRING', 77006 );
134 define( 'PCLZIP_OPT_NO_COMPRESSION', 77007 );
135 define( 'PCLZIP_OPT_BY_NAME', 77008 );
136 define( 'PCLZIP_OPT_BY_INDEX', 77009 );
137 define( 'PCLZIP_OPT_BY_EREG', 77010 );
138 define( 'PCLZIP_OPT_BY_PREG', 77011 );
139 define( 'PCLZIP_OPT_COMMENT', 77012 );
140 define( 'PCLZIP_OPT_ADD_COMMENT', 77013 );
141 define( 'PCLZIP_OPT_PREPEND_COMMENT', 77014 );
142 define( 'PCLZIP_OPT_EXTRACT_IN_OUTPUT', 77015 );
143 define( 'PCLZIP_OPT_REPLACE_NEWER', 77016 );
144 define( 'PCLZIP_OPT_STOP_ON_ERROR', 77017 );
145 // Having big trouble with crypt. Need to multiply 2 long int
146 // which is not correctly supported by PHP ...
147 //define( 'PCLZIP_OPT_CRYPT', 77018 );
148 define( 'PCLZIP_OPT_EXTRACT_DIR_RESTRICTION', 77019 );
149 define( 'PCLZIP_OPT_TEMP_FILE_THRESHOLD', 77020 );
150 define( 'PCLZIP_OPT_ADD_TEMP_FILE_THRESHOLD', 77020 ); // alias
151 define( 'PCLZIP_OPT_TEMP_FILE_ON', 77021 );
152 define( 'PCLZIP_OPT_ADD_TEMP_FILE_ON', 77021 ); // alias
153 define( 'PCLZIP_OPT_TEMP_FILE_OFF', 77022 );
154 define( 'PCLZIP_OPT_ADD_TEMP_FILE_OFF', 77022 ); // alias
156 // ----- File description attributes
157 define( 'PCLZIP_ATT_FILE_NAME', 79001 );
158 define( 'PCLZIP_ATT_FILE_NEW_SHORT_NAME', 79002 );
159 define( 'PCLZIP_ATT_FILE_NEW_FULL_NAME', 79003 );
160 define( 'PCLZIP_ATT_FILE_MTIME', 79004 );
161 define( 'PCLZIP_ATT_FILE_CONTENT', 79005 );
162 define( 'PCLZIP_ATT_FILE_COMMENT', 79006 );
164 // ----- Call backs values
165 define( 'PCLZIP_CB_PRE_EXTRACT', 78001 );
166 define( 'PCLZIP_CB_POST_EXTRACT', 78002 );
167 define( 'PCLZIP_CB_PRE_ADD', 78003 );
168 define( 'PCLZIP_CB_POST_ADD', 78004 );
170 define( 'PCLZIP_CB_PRE_LIST', 78005 );
171 define( 'PCLZIP_CB_POST_LIST', 78006 );
172 define( 'PCLZIP_CB_PRE_DELETE', 78007 );
173 define( 'PCLZIP_CB_POST_DELETE', 78008 );
176 // --------------------------------------------------------------------------------
179 // PclZip is the class that represent a Zip archive.
180 // The public methods allow the manipulation of the archive.
182 // Attributes must not be accessed directly.
184 // PclZip() : Object creator
185 // create() : Creates the Zip archive
186 // listContent() : List the content of the Zip archive
187 // extract() : Extract the content of the archive
188 // properties() : List the properties of the archive
189 // --------------------------------------------------------------------------------
192 // ----- Filename of the zip file
195 // ----- File descriptor of the zip file
198 // ----- Internal error handling
200 var $error_string = '';
202 // ----- Current status of the magic_quotes_runtime
203 // This value store the php configuration for magic_quotes
204 // The class can then disable the magic_quotes and reset it after
205 var $magic_quotes_status;
207 // --------------------------------------------------------------------------------
208 // Function : PclZip()
210 // Creates a PclZip object and set the name of the associated Zip archive
212 // Note that no real action is taken, if the archive does not exist it is not
213 // created. Use create() for that.
214 // --------------------------------------------------------------------------------
215 function PclZip($p_zipname)
217 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::PclZip', "zipname=$p_zipname");
219 // ----- Tests the zlib
220 if (!function_exists('gzopen'))
222 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 1, "zlib extension seems to be missing");
223 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
226 // ----- Set the attributes
227 $this->zipname = $p_zipname;
229 $this->magic_quotes_status = -1;
232 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 1);
235 // --------------------------------------------------------------------------------
237 // --------------------------------------------------------------------------------
239 // create($p_filelist, $p_add_dir="", $p_remove_dir="")
240 // create($p_filelist, $p_option, $p_option_value, ...)
242 // This method supports two different synopsis. The first one is historical.
243 // This method creates a Zip Archive. The Zip file is created in the
244 // filesystem. The files and directories indicated in $p_filelist
245 // are added in the archive. See the parameters description for the
246 // supported format of $p_filelist.
247 // When a directory is in the list, the directory and its content is added
249 // In this synopsis, the function takes an optional variable list of
250 // options. See bellow the supported options.
252 // $p_filelist : An array containing file or directory names, or
253 // a string containing one filename or one directory name, or
254 // a string containing a list of filenames and/or directory
255 // names separated by spaces.
256 // $p_add_dir : A path to add before the real path of the archived file,
257 // in order to have it memorized in the archive.
258 // $p_remove_dir : A path to remove from the real path of the file to archive,
259 // in order to have a shorter path memorized in the archive.
260 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
261 // is removed first, before $p_add_dir is added.
263 // PCLZIP_OPT_ADD_PATH :
264 // PCLZIP_OPT_REMOVE_PATH :
265 // PCLZIP_OPT_REMOVE_ALL_PATH :
266 // PCLZIP_OPT_COMMENT :
267 // PCLZIP_CB_PRE_ADD :
268 // PCLZIP_CB_POST_ADD :
271 // The list of the added files, with a status of the add action.
272 // (see PclZip::listContent() for list entry format)
273 // --------------------------------------------------------------------------------
274 function create($p_filelist)
276 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::create', "filelist='$p_filelist', ...");
279 // ----- Reset the error handler
280 $this->privErrorReset();
282 // ----- Set default values
283 $v_options = array();
284 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
286 // ----- Look for variable options arguments
287 $v_size = func_num_args();
288 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
290 // ----- Look for arguments
292 // ----- Get the arguments
293 $v_arg_list = func_get_args();
295 // ----- Remove from the options list the first argument
296 array_shift($v_arg_list);
299 // ----- Look for first arg
300 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
301 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
303 // ----- Parse the options
304 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
305 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
306 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
307 PCLZIP_OPT_ADD_PATH => 'optional',
308 PCLZIP_CB_PRE_ADD => 'optional',
309 PCLZIP_CB_POST_ADD => 'optional',
310 PCLZIP_OPT_NO_COMPRESSION => 'optional',
311 PCLZIP_OPT_COMMENT => 'optional',
312 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
313 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
314 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
315 //, PCLZIP_OPT_CRYPT => 'optional'
317 if ($v_result != 1) {
318 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
323 // ----- Look for 2 args
324 // Here we need to support the first historic synopsis of the
327 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
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");
339 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
345 // ----- Look for default option values
346 $this->privOptionDefaultThreshold($v_options);
349 $v_string_list = array();
350 $v_att_list = array();
351 $v_filedescr_list = array();
352 $p_result_list = array();
354 // ----- Look if the $p_filelist is really an array
355 if (is_array($p_filelist)) {
357 // ----- Look if the first element is also an array
358 // This will mean that this is a file description entry
359 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
360 $v_att_list = $p_filelist;
363 // ----- The list is a list of string names
365 $v_string_list = $p_filelist;
369 // ----- Look if the $p_filelist is a string
370 else if (is_string($p_filelist)) {
371 // ----- Create a list from the string
372 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
375 // ----- Invalid variable type for $p_filelist
377 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
378 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
382 // ----- Reformat the string list
383 if (sizeof($v_string_list) != 0) {
384 foreach ($v_string_list as $v_string) {
385 if ($v_string != '') {
386 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
389 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Ignore an empty filename");
394 // ----- For each file in the list check the attributes
395 $v_supported_attributes
396 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
397 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
398 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
399 ,PCLZIP_ATT_FILE_MTIME => 'optional'
400 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
401 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
403 foreach ($v_att_list as $v_entry) {
404 $v_result = $this->privFileDescrParseAtt($v_entry,
407 $v_supported_attributes);
408 if ($v_result != 1) {
409 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
414 // ----- Expand the filelist (expand directories)
415 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
416 if ($v_result != 1) {
417 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
421 // ----- Call the create fct
422 $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
423 if ($v_result != 1) {
424 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
429 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
430 return $p_result_list;
432 // --------------------------------------------------------------------------------
434 // --------------------------------------------------------------------------------
436 // add($p_filelist, $p_add_dir="", $p_remove_dir="")
437 // add($p_filelist, $p_option, $p_option_value, ...)
439 // This method supports two synopsis. The first one is historical.
440 // This methods add the list of files in an existing archive.
441 // If a file with the same name already exists, it is added at the end of the
442 // archive, the first one is still present.
443 // If the archive does not exist, it is created.
445 // $p_filelist : An array containing file or directory names, or
446 // a string containing one filename or one directory name, or
447 // a string containing a list of filenames and/or directory
448 // names separated by spaces.
449 // $p_add_dir : A path to add before the real path of the archived file,
450 // in order to have it memorized in the archive.
451 // $p_remove_dir : A path to remove from the real path of the file to archive,
452 // in order to have a shorter path memorized in the archive.
453 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
454 // is removed first, before $p_add_dir is added.
456 // PCLZIP_OPT_ADD_PATH :
457 // PCLZIP_OPT_REMOVE_PATH :
458 // PCLZIP_OPT_REMOVE_ALL_PATH :
459 // PCLZIP_OPT_COMMENT :
460 // PCLZIP_OPT_ADD_COMMENT :
461 // PCLZIP_OPT_PREPEND_COMMENT :
462 // PCLZIP_CB_PRE_ADD :
463 // PCLZIP_CB_POST_ADD :
466 // The list of the added files, with a status of the add action.
467 // (see PclZip::listContent() for list entry format)
468 // --------------------------------------------------------------------------------
469 function add($p_filelist)
471 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::add', "filelist='$p_filelist', ...");
474 // ----- Reset the error handler
475 $this->privErrorReset();
477 // ----- Set default values
478 $v_options = array();
479 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
481 // ----- Look for variable options arguments
482 $v_size = func_num_args();
483 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
485 // ----- Look for arguments
487 // ----- Get the arguments
488 $v_arg_list = func_get_args();
490 // ----- Remove form the options list the first argument
491 array_shift($v_arg_list);
494 // ----- Look for first arg
495 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
496 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options detected");
498 // ----- Parse the options
499 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
500 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
501 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
502 PCLZIP_OPT_ADD_PATH => 'optional',
503 PCLZIP_CB_PRE_ADD => 'optional',
504 PCLZIP_CB_POST_ADD => 'optional',
505 PCLZIP_OPT_NO_COMPRESSION => 'optional',
506 PCLZIP_OPT_COMMENT => 'optional',
507 PCLZIP_OPT_ADD_COMMENT => 'optional',
508 PCLZIP_OPT_PREPEND_COMMENT => 'optional',
509 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
510 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
511 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
512 //, PCLZIP_OPT_CRYPT => 'optional'
514 if ($v_result != 1) {
515 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
520 // ----- Look for 2 args
521 // Here we need to support the first historic synopsis of the
524 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
526 // ----- Get the first argument
527 $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
529 // ----- Look for the optional second argument
531 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
533 else if ($v_size > 2) {
535 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
544 // ----- Look for default option values
545 $this->privOptionDefaultThreshold($v_options);
548 $v_string_list = array();
549 $v_att_list = array();
550 $v_filedescr_list = array();
551 $p_result_list = array();
553 // ----- Look if the $p_filelist is really an array
554 if (is_array($p_filelist)) {
556 // ----- Look if the first element is also an array
557 // This will mean that this is a file description entry
558 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
559 $v_att_list = $p_filelist;
562 // ----- The list is a list of string names
564 $v_string_list = $p_filelist;
568 // ----- Look if the $p_filelist is a string
569 else if (is_string($p_filelist)) {
570 // ----- Create a list from the string
571 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
574 // ----- Invalid variable type for $p_filelist
576 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
577 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
581 // ----- Reformat the string list
582 if (sizeof($v_string_list) != 0) {
583 foreach ($v_string_list as $v_string) {
584 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
588 // ----- For each file in the list check the attributes
589 $v_supported_attributes
590 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
591 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
592 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
593 ,PCLZIP_ATT_FILE_MTIME => 'optional'
594 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
595 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
597 foreach ($v_att_list as $v_entry) {
598 $v_result = $this->privFileDescrParseAtt($v_entry,
601 $v_supported_attributes);
602 if ($v_result != 1) {
603 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
608 // ----- Expand the filelist (expand directories)
609 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
610 if ($v_result != 1) {
611 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
615 // ----- Call the create fct
616 $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
617 if ($v_result != 1) {
618 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
623 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_result_list);
624 return $p_result_list;
626 // --------------------------------------------------------------------------------
628 // --------------------------------------------------------------------------------
629 // Function : listContent()
631 // This public method, gives the list of the files and directories, with their
633 // The properties of each entries in the list are (used also in other functions) :
634 // filename : Name of the file. For a create or add action it is the filename
635 // given by the user. For an extract function it is the filename
636 // of the extracted file.
637 // stored_filename : Name of the file / directory stored in the archive.
638 // size : Size of the stored file.
639 // compressed_size : Size of the file's data compressed in the archive
640 // (without the headers overhead)
641 // mtime : Last known modification date of the file (UNIX timestamp)
642 // comment : Comment associated with the file
643 // folder : true | false
644 // index : index of the file in the archive
645 // status : status of the action (depending of the action) :
648 // filtered : the file / dir is not extracted (filtered by user)
649 // already_a_directory : the file can not be extracted because a
650 // directory with the same name already exists
651 // write_protected : the file can not be extracted because a file
652 // with the same name already exists and is
654 // newer_exist : the file was not extracted because a newer file exists
655 // path_creation_fail : the file is not extracted because the folder
656 // does not exist and can not be created
657 // write_error : the file was not extracted because there was a
658 // error while writing the file
659 // read_error : the file was not extracted because there was a error
660 // while reading the file
661 // invalid_header : the file was not extracted because of an archive
662 // format error (bad file header)
663 // Note that each time a method can continue operating when there
664 // is an action error on a file, the error is only logged in the file status.
666 // 0 on an unrecoverable failure,
667 // The list of the files in the archive.
668 // --------------------------------------------------------------------------------
669 function listContent()
671 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, 'PclZip::listContent', "");
674 // ----- Reset the error handler
675 $this->privErrorReset();
677 // ----- Check archive
678 if (!$this->privCheckFormat()) {
679 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
683 // ----- Call the extracting fct
685 if (($v_result = $this->privList($p_list)) != 1)
688 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
693 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
696 // --------------------------------------------------------------------------------
698 // --------------------------------------------------------------------------------
700 // extract($p_path="./", $p_remove_path="")
701 // extract([$p_option, $p_option_value, ...])
703 // This method supports two synopsis. The first one is historical.
704 // This method extract all the files / directories from the archive to the
705 // folder indicated in $p_path.
706 // If you want to ignore the 'root' part of path of the memorized files
707 // you can indicate this in the optional $p_remove_path parameter.
708 // By default, if a newer file with the same name already exists, the
709 // file is not extracted.
711 // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
712 // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
713 // at the end of the path value of PCLZIP_OPT_PATH.
715 // $p_path : Path where the files and directories are to be extracted
716 // $p_remove_path : First part ('root' part) of the memorized path
717 // (if any similar) to remove while extracting.
720 // PCLZIP_OPT_ADD_PATH :
721 // PCLZIP_OPT_REMOVE_PATH :
722 // PCLZIP_OPT_REMOVE_ALL_PATH :
723 // PCLZIP_CB_PRE_EXTRACT :
724 // PCLZIP_CB_POST_EXTRACT :
726 // 0 or a negative value on failure,
727 // The list of the extracted files, with a status of the action.
728 // (see PclZip::listContent() for list entry format)
729 // --------------------------------------------------------------------------------
732 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extract", "");
735 // ----- Reset the error handler
736 $this->privErrorReset();
738 // ----- Check archive
739 if (!$this->privCheckFormat()) {
740 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
744 // ----- Set default values
745 $v_options = array();
749 $v_remove_all_path = false;
751 // ----- Look for variable options arguments
752 $v_size = func_num_args();
753 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
755 // ----- Default values for option
756 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
758 // ----- Look for arguments
760 // ----- Get the arguments
761 $v_arg_list = func_get_args();
763 // ----- Look for first arg
764 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
765 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
767 // ----- Parse the options
768 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
769 array (PCLZIP_OPT_PATH => 'optional',
770 PCLZIP_OPT_REMOVE_PATH => 'optional',
771 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
772 PCLZIP_OPT_ADD_PATH => 'optional',
773 PCLZIP_CB_PRE_EXTRACT => 'optional',
774 PCLZIP_CB_POST_EXTRACT => 'optional',
775 PCLZIP_OPT_SET_CHMOD => 'optional',
776 PCLZIP_OPT_BY_NAME => 'optional',
777 PCLZIP_OPT_BY_EREG => 'optional',
778 PCLZIP_OPT_BY_PREG => 'optional',
779 PCLZIP_OPT_BY_INDEX => 'optional',
780 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
781 PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
782 PCLZIP_OPT_REPLACE_NEWER => 'optional'
783 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
784 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
785 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
786 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
787 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
789 if ($v_result != 1) {
790 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
794 // ----- Set the arguments
795 if (isset($v_options[PCLZIP_OPT_PATH])) {
796 $v_path = $v_options[PCLZIP_OPT_PATH];
798 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
799 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
801 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
802 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
804 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
805 // ----- Check for '/' in last path char
806 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
809 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
813 // ----- Look for 2 args
814 // Here we need to support the first historic synopsis of the
817 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
819 // ----- Get the first argument
820 $v_path = $v_arg_list[0];
822 // ----- Look for the optional second argument
824 $v_remove_path = $v_arg_list[1];
826 else if ($v_size > 2) {
828 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
831 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
837 // ----- Look for default option values
838 $this->privOptionDefaultThreshold($v_options);
841 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
843 // ----- Call the extracting fct
845 $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
846 $v_remove_all_path, $v_options);
849 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
854 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
857 // --------------------------------------------------------------------------------
860 // --------------------------------------------------------------------------------
862 // extractByIndex($p_index, $p_path="./", $p_remove_path="")
863 // extractByIndex($p_index, [$p_option, $p_option_value, ...])
865 // This method supports two synopsis. The first one is historical.
866 // This method is doing a partial extract of the archive.
867 // The extracted files or folders are identified by their index in the
868 // archive (from 0 to n).
869 // Note that if the index identify a folder, only the folder entry is
870 // extracted, not all the files included in the archive.
872 // $p_index : A single index (integer) or a string of indexes of files to
873 // extract. The form of the string is "0,4-6,8-12" with only numbers
874 // and '-' for range or ',' to separate ranges. No spaces or ';'
876 // $p_path : Path where the files and directories are to be extracted
877 // $p_remove_path : First part ('root' part) of the memorized path
878 // (if any similar) to remove while extracting.
881 // PCLZIP_OPT_ADD_PATH :
882 // PCLZIP_OPT_REMOVE_PATH :
883 // PCLZIP_OPT_REMOVE_ALL_PATH :
884 // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
886 // The resulting content is in a new field 'content' in the file
888 // This option must be used alone (any other options are ignored).
889 // PCLZIP_CB_PRE_EXTRACT :
890 // PCLZIP_CB_POST_EXTRACT :
893 // The list of the extracted files, with a status of the action.
894 // (see PclZip::listContent() for list entry format)
895 // --------------------------------------------------------------------------------
896 //function extractByIndex($p_index, options...)
897 function extractByIndex($p_index)
899 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::extractByIndex", "index='$p_index', ...");
902 // ----- Reset the error handler
903 $this->privErrorReset();
905 // ----- Check archive
906 if (!$this->privCheckFormat()) {
907 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
911 // ----- Set default values
912 $v_options = array();
916 $v_remove_all_path = false;
918 // ----- Look for variable options arguments
919 $v_size = func_num_args();
920 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
922 // ----- Default values for option
923 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
925 // ----- Look for arguments
927 // ----- Get the arguments
928 $v_arg_list = func_get_args();
930 // ----- Remove form the options list the first argument
931 array_shift($v_arg_list);
934 // ----- Look for first arg
935 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
936 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Variable list of options");
938 // ----- Parse the options
939 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
940 array (PCLZIP_OPT_PATH => 'optional',
941 PCLZIP_OPT_REMOVE_PATH => 'optional',
942 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
943 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
944 PCLZIP_OPT_ADD_PATH => 'optional',
945 PCLZIP_CB_PRE_EXTRACT => 'optional',
946 PCLZIP_CB_POST_EXTRACT => 'optional',
947 PCLZIP_OPT_SET_CHMOD => 'optional',
948 PCLZIP_OPT_REPLACE_NEWER => 'optional'
949 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
950 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
951 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
952 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
953 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
955 if ($v_result != 1) {
956 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
960 // ----- Set the arguments
961 if (isset($v_options[PCLZIP_OPT_PATH])) {
962 $v_path = $v_options[PCLZIP_OPT_PATH];
964 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
965 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
967 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
968 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
970 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
971 // ----- Check for '/' in last path char
972 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
975 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
977 if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
978 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
979 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING not set.");
982 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Option PCLZIP_OPT_EXTRACT_AS_STRING set.");
986 // ----- Look for 2 args
987 // Here we need to support the first historic synopsis of the
990 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Static synopsis");
992 // ----- Get the first argument
993 $v_path = $v_arg_list[0];
995 // ----- Look for the optional second argument
997 $v_remove_path = $v_arg_list[1];
999 else if ($v_size > 2) {
1001 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
1004 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1011 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "index='$p_index', path='$v_path', remove_path='$v_remove_path', remove_all_path='".($v_remove_path?'true':'false')."'");
1014 // Here I want to reuse extractByRule(), so I need to parse the $p_index
1015 // with privParseOptions()
1016 $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
1017 $v_options_trick = array();
1018 $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
1019 array (PCLZIP_OPT_BY_INDEX => 'optional' ));
1020 if ($v_result != 1) {
1021 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1024 $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
1026 // ----- Look for default option values
1027 $this->privOptionDefaultThreshold($v_options);
1029 // ----- Call the extracting fct
1030 if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
1031 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
1036 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1039 // --------------------------------------------------------------------------------
1041 // --------------------------------------------------------------------------------
1043 // delete([$p_option, $p_option_value, ...])
1045 // This method removes files from the archive.
1046 // If no parameters are given, then all the archive is emptied.
1048 // None or optional arguments.
1050 // PCLZIP_OPT_BY_INDEX :
1051 // PCLZIP_OPT_BY_NAME :
1052 // PCLZIP_OPT_BY_EREG :
1053 // PCLZIP_OPT_BY_PREG :
1056 // The list of the files which are still present in the archive.
1057 // (see PclZip::listContent() for list entry format)
1058 // --------------------------------------------------------------------------------
1061 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::delete", "");
1064 // ----- Reset the error handler
1065 $this->privErrorReset();
1067 // ----- Check archive
1068 if (!$this->privCheckFormat()) {
1069 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1073 // ----- Set default values
1074 $v_options = array();
1076 // ----- Look for variable options arguments
1077 $v_size = func_num_args();
1078 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "$v_size arguments passed to the method");
1080 // ----- Look for arguments
1082 // ----- Get the arguments
1083 $v_arg_list = func_get_args();
1085 // ----- Parse the options
1086 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
1087 array (PCLZIP_OPT_BY_NAME => 'optional',
1088 PCLZIP_OPT_BY_EREG => 'optional',
1089 PCLZIP_OPT_BY_PREG => 'optional',
1090 PCLZIP_OPT_BY_INDEX => 'optional' ));
1091 if ($v_result != 1) {
1092 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1097 // ----- Magic quotes trick
1098 $this->privDisableMagicQuotes();
1100 // ----- Call the delete fct
1102 if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1103 $this->privSwapBackMagicQuotes();
1105 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0, PclZip::errorInfo());
1109 // ----- Magic quotes trick
1110 $this->privSwapBackMagicQuotes();
1113 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_list);
1116 // --------------------------------------------------------------------------------
1118 // --------------------------------------------------------------------------------
1119 // Function : deleteByIndex()
1121 // ***** Deprecated *****
1122 // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1123 // --------------------------------------------------------------------------------
1124 function deleteByIndex($p_index)
1126 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::deleteByIndex", "index='$p_index'");
1128 $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1131 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $p_list);
1134 // --------------------------------------------------------------------------------
1136 // --------------------------------------------------------------------------------
1137 // Function : properties()
1139 // This method gives the properties of the archive.
1140 // The properties are :
1141 // nb : Number of files in the archive
1142 // comment : Comment associated with the archive file
1143 // status : not_exist, ok
1148 // An array with the archive properties.
1149 // --------------------------------------------------------------------------------
1150 function properties()
1152 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::properties", "");
1154 // ----- Reset the error handler
1155 $this->privErrorReset();
1157 // ----- Magic quotes trick
1158 $this->privDisableMagicQuotes();
1160 // ----- Check archive
1161 if (!$this->privCheckFormat()) {
1162 $this->privSwapBackMagicQuotes();
1163 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1167 // ----- Default properties
1169 $v_prop['comment'] = '';
1171 $v_prop['status'] = 'not_exist';
1173 // ----- Look if file exists
1174 if (@is_file($this->zipname))
1176 // ----- Open the zip file
1177 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
1178 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1180 $this->privSwapBackMagicQuotes();
1183 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1186 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), 0);
1190 // ----- Read the central directory informations
1191 $v_central_dir = array();
1192 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1194 $this->privSwapBackMagicQuotes();
1195 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1199 // ----- Close the zip file
1200 $this->privCloseFd();
1202 // ----- Set the user attributes
1203 $v_prop['comment'] = $v_central_dir['comment'];
1204 $v_prop['nb'] = $v_central_dir['entries'];
1205 $v_prop['status'] = 'ok';
1208 // ----- Magic quotes trick
1209 $this->privSwapBackMagicQuotes();
1212 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_prop);
1215 // --------------------------------------------------------------------------------
1217 // --------------------------------------------------------------------------------
1218 // Function : duplicate()
1220 // This method creates an archive by copying the content of an other one. If
1221 // the archive already exist, it is replaced by the new one without any warning.
1223 // $p_archive : The filename of a valid archive, or
1224 // a valid PclZip object.
1227 // 0 or a negative value on error (error code).
1228 // --------------------------------------------------------------------------------
1229 function duplicate($p_archive)
1231 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::duplicate", "");
1234 // ----- Reset the error handler
1235 $this->privErrorReset();
1237 // ----- Look if the $p_archive is a PclZip object
1238 if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1240 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is valid PclZip object '".$p_archive->zipname."'");
1242 // ----- Duplicate the archive
1243 $v_result = $this->privDuplicate($p_archive->zipname);
1246 // ----- Look if the $p_archive is a string (so a filename)
1247 else if (is_string($p_archive))
1249 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "The parameter is a filename '$p_archive'");
1251 // ----- Check that $p_archive is a valid zip file
1252 // TBC : Should also check the archive format
1253 if (!is_file($p_archive)) {
1255 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1256 $v_result = PCLZIP_ERR_MISSING_FILE;
1259 // ----- Duplicate the archive
1260 $v_result = $this->privDuplicate($p_archive);
1264 // ----- Invalid variable
1268 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1269 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1273 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1276 // --------------------------------------------------------------------------------
1278 // --------------------------------------------------------------------------------
1279 // Function : merge()
1281 // This method merge the $p_archive_to_add archive at the end of the current
1283 // If the archive ($this) does not exist, the merge becomes a duplicate.
1284 // If the $p_archive_to_add archive does not exist, the merge is a success.
1286 // $p_archive_to_add : It can be directly the filename of a valid zip archive,
1287 // or a PclZip object archive.
1290 // 0 or negative values on error (see below).
1291 // --------------------------------------------------------------------------------
1292 function merge($p_archive_to_add)
1294 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::merge", "");
1297 // ----- Reset the error handler
1298 $this->privErrorReset();
1300 // ----- Check archive
1301 if (!$this->privCheckFormat()) {
1302 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, 0);
1306 // ----- Look if the $p_archive_to_add is a PclZip object
1307 if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1309 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is valid PclZip object");
1311 // ----- Merge the archive
1312 $v_result = $this->privMerge($p_archive_to_add);
1315 // ----- Look if the $p_archive_to_add is a string (so a filename)
1316 else if (is_string($p_archive_to_add))
1318 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The parameter is a filename");
1320 // ----- Create a temporary archive
1321 $v_object_archive = new PclZip($p_archive_to_add);
1323 // ----- Merge the archive
1324 $v_result = $this->privMerge($v_object_archive);
1327 // ----- Invalid variable
1331 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1332 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1336 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1339 // --------------------------------------------------------------------------------
1343 // --------------------------------------------------------------------------------
1344 // Function : errorCode()
1347 // --------------------------------------------------------------------------------
1348 function errorCode()
1350 if (PCLZIP_ERROR_EXTERNAL == 1) {
1351 return(PclErrorCode());
1354 return($this->error_code);
1357 // --------------------------------------------------------------------------------
1359 // --------------------------------------------------------------------------------
1360 // Function : errorName()
1363 // --------------------------------------------------------------------------------
1364 function errorName($p_with_code=false)
1366 $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1367 PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1368 PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1369 PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1370 PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1371 PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1372 PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1373 PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1374 PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1375 PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1376 PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1377 PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1378 PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1379 PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1380 PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1381 PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1382 PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1383 PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1384 PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
1385 ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
1386 ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
1389 if (isset($v_name[$this->error_code])) {
1390 $v_value = $v_name[$this->error_code];
1393 $v_value = 'NoName';
1397 return($v_value.' ('.$this->error_code.')');
1403 // --------------------------------------------------------------------------------
1405 // --------------------------------------------------------------------------------
1406 // Function : errorInfo()
1409 // --------------------------------------------------------------------------------
1410 function errorInfo($p_full=false)
1412 if (PCLZIP_ERROR_EXTERNAL == 1) {
1413 return(PclErrorString());
1417 return($this->errorName(true)." : ".$this->error_string);
1420 return($this->error_string." [code ".$this->error_code."]");
1424 // --------------------------------------------------------------------------------
1427 // --------------------------------------------------------------------------------
1428 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1430 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
1431 // --------------------------------------------------------------------------------
1435 // --------------------------------------------------------------------------------
1436 // Function : privCheckFormat()
1438 // This method check that the archive exists and is a valid zip archive.
1439 // Several level of check exists. (futur)
1441 // $p_level : Level of check. Default 0.
1442 // 0 : Check the first bytes (magic codes) (default value))
1443 // 1 : 0 + Check the central directory (futur)
1444 // 2 : 1 + Check each file header (futur)
1447 // false on error, the error code is set.
1448 // --------------------------------------------------------------------------------
1449 function privCheckFormat($p_level=0)
1451 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCheckFormat", "");
1454 // ----- Reset the file system cache
1457 // ----- Reset the error handler
1458 $this->privErrorReset();
1460 // ----- Look if the file exits
1461 if (!is_file($this->zipname)) {
1463 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1464 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1468 // ----- Check that the file is readeable
1469 if (!is_readable($this->zipname)) {
1471 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1472 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, false, PclZip::errorInfo());
1476 // ----- Check the magic code
1479 // ----- Check the central header
1482 // ----- Check each file header
1486 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1489 // --------------------------------------------------------------------------------
1491 // --------------------------------------------------------------------------------
1492 // Function : privParseOptions()
1494 // This internal methods reads the variable list of arguments ($p_options_list,
1495 // $p_size) and generate an array with the options and values ($v_result_list).
1496 // $v_requested_options contains the options that can be present and those that
1498 // $v_requested_options is an array, with the option value as key, and 'optional',
1499 // or 'mandatory' as value.
1505 // --------------------------------------------------------------------------------
1506 function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1508 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privParseOptions", "");
1511 // ----- Read the options
1513 while ($i<$p_size) {
1514 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Looking for table index $i, option = '".PclZipUtilOptionText($p_options_list[$i])."(".$p_options_list[$i].")'");
1516 // ----- Check if the option is supported
1517 if (!isset($v_requested_options[$p_options_list[$i]])) {
1519 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1522 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1523 return PclZip::errorCode();
1526 // ----- Look for next option
1527 switch ($p_options_list[$i]) {
1528 // ----- Look for options that request a path value
1529 case PCLZIP_OPT_PATH :
1530 case PCLZIP_OPT_REMOVE_PATH :
1531 case PCLZIP_OPT_ADD_PATH :
1532 // ----- Check the number of parameters
1533 if (($i+1) >= $p_size) {
1535 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1539 return PclZip::errorCode();
1542 // ----- Get the value
1543 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1544 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1548 case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
1549 // ----- Check the number of parameters
1550 if (($i+1) >= $p_size) {
1551 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1552 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1553 return PclZip::errorCode();
1556 // ----- Check for incompatible options
1557 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1558 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1559 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1560 return PclZip::errorCode();
1563 // ----- Check the value
1564 $v_value = $p_options_list[$i+1];
1565 if ((!is_integer($v_value)) || ($v_value<0)) {
1566 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1567 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1568 return PclZip::errorCode();
1571 // ----- Get the value (and convert it in bytes)
1572 $v_result_list[$p_options_list[$i]] = $v_value*1048576;
1573 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1577 case PCLZIP_OPT_TEMP_FILE_ON :
1578 // ----- Check for incompatible options
1579 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1580 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1581 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1582 return PclZip::errorCode();
1585 $v_result_list[$p_options_list[$i]] = true;
1586 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1589 case PCLZIP_OPT_TEMP_FILE_OFF :
1590 // ----- Check for incompatible options
1591 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
1592 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
1593 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1594 return PclZip::errorCode();
1596 // ----- Check for incompatible options
1597 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1598 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
1599 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1600 return PclZip::errorCode();
1603 $v_result_list[$p_options_list[$i]] = true;
1604 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1607 case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
1608 // ----- Check the number of parameters
1609 if (($i+1) >= $p_size) {
1611 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1614 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1615 return PclZip::errorCode();
1618 // ----- Get the value
1619 if ( is_string($p_options_list[$i+1])
1620 && ($p_options_list[$i+1] != '')) {
1621 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1622 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1626 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." set with an empty value is ignored.");
1630 // ----- Look for options that request an array of string for value
1631 case PCLZIP_OPT_BY_NAME :
1632 // ----- Check the number of parameters
1633 if (($i+1) >= $p_size) {
1635 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1638 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1639 return PclZip::errorCode();
1642 // ----- Get the value
1643 if (is_string($p_options_list[$i+1])) {
1644 $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1646 else if (is_array($p_options_list[$i+1])) {
1647 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1651 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1654 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1655 return PclZip::errorCode();
1657 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1661 // ----- Look for options that request an EREG or PREG expression
1662 case PCLZIP_OPT_BY_EREG :
1663 case PCLZIP_OPT_BY_PREG :
1664 //case PCLZIP_OPT_CRYPT :
1665 // ----- Check the number of parameters
1666 if (($i+1) >= $p_size) {
1668 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1671 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1672 return PclZip::errorCode();
1675 // ----- Get the value
1676 if (is_string($p_options_list[$i+1])) {
1677 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1681 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1684 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1685 return PclZip::errorCode();
1687 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1691 // ----- Look for options that takes a string
1692 case PCLZIP_OPT_COMMENT :
1693 case PCLZIP_OPT_ADD_COMMENT :
1694 case PCLZIP_OPT_PREPEND_COMMENT :
1695 // ----- Check the number of parameters
1696 if (($i+1) >= $p_size) {
1698 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1699 "Missing parameter value for option '"
1700 .PclZipUtilOptionText($p_options_list[$i])
1704 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1705 return PclZip::errorCode();
1708 // ----- Get the value
1709 if (is_string($p_options_list[$i+1])) {
1710 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1714 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1715 "Wrong parameter value for option '"
1716 .PclZipUtilOptionText($p_options_list[$i])
1720 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1721 return PclZip::errorCode();
1723 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1727 // ----- Look for options that request an array of index
1728 case PCLZIP_OPT_BY_INDEX :
1729 // ----- Check the number of parameters
1730 if (($i+1) >= $p_size) {
1732 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1735 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1736 return PclZip::errorCode();
1739 // ----- Get the value
1740 $v_work_list = array();
1741 if (is_string($p_options_list[$i+1])) {
1742 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is a string '".$p_options_list[$i+1]."'");
1744 // ----- Remove spaces
1745 $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
1747 // ----- Parse items
1748 $v_work_list = explode(",", $p_options_list[$i+1]);
1750 else if (is_integer($p_options_list[$i+1])) {
1751 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an integer '".$p_options_list[$i+1]."'");
1752 $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
1754 else if (is_array($p_options_list[$i+1])) {
1755 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Index value is an array");
1756 $v_work_list = $p_options_list[$i+1];
1760 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1763 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1764 return PclZip::errorCode();
1767 // ----- Reduce the index list
1768 // each index item in the list must be a couple with a start and
1769 // an end value : [0,3], [5-5], [8-10], ...
1770 // ----- Check the format of each item
1773 for ($j=0; $j<sizeof($v_work_list); $j++) {
1774 // ----- Explode the item
1775 $v_item_list = explode("-", $v_work_list[$j]);
1776 $v_size_item_list = sizeof($v_item_list);
1778 // ----- TBC : Here we might check that each item is a
1781 // ----- Look for single value
1782 if ($v_size_item_list == 1) {
1783 // ----- Set the option value
1784 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1785 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1787 elseif ($v_size_item_list == 2) {
1788 // ----- Set the option value
1789 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1790 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1794 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1797 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1798 return PclZip::errorCode();
1801 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extracted index item = [".$v_result_list[$p_options_list[$i]][$j]['start'].",".$v_result_list[$p_options_list[$i]][$j]['end']."]");
1803 // ----- Look for list sort
1804 if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1805 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The list should be sorted ...");
1808 // ----- TBC : An automatic sort should be writen ...
1810 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1813 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1814 return PclZip::errorCode();
1816 $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1819 // ----- Sort the items
1821 // TBC : To Be Completed
1822 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "List sorting is not yet write ...");
1825 // ----- Next option
1829 // ----- Look for options that request no value
1830 case PCLZIP_OPT_REMOVE_ALL_PATH :
1831 case PCLZIP_OPT_EXTRACT_AS_STRING :
1832 case PCLZIP_OPT_NO_COMPRESSION :
1833 case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
1834 case PCLZIP_OPT_REPLACE_NEWER :
1835 case PCLZIP_OPT_STOP_ON_ERROR :
1836 $v_result_list[$p_options_list[$i]] = true;
1837 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1840 // ----- Look for options that request an octal value
1841 case PCLZIP_OPT_SET_CHMOD :
1842 // ----- Check the number of parameters
1843 if (($i+1) >= $p_size) {
1845 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1848 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1849 return PclZip::errorCode();
1852 // ----- Get the value
1853 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1854 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($p_options_list[$i])." = '".$v_result_list[$p_options_list[$i]]."'");
1858 // ----- Look for options that request a call-back
1859 case PCLZIP_CB_PRE_EXTRACT :
1860 case PCLZIP_CB_POST_EXTRACT :
1861 case PCLZIP_CB_PRE_ADD :
1862 case PCLZIP_CB_POST_ADD :
1864 case PCLZIP_CB_PRE_DELETE :
1865 case PCLZIP_CB_POST_DELETE :
1866 case PCLZIP_CB_PRE_LIST :
1867 case PCLZIP_CB_POST_LIST :
1869 // ----- Check the number of parameters
1870 if (($i+1) >= $p_size) {
1872 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1875 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1876 return PclZip::errorCode();
1879 // ----- Get the value
1880 $v_function_name = $p_options_list[$i+1];
1881 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "call-back ".PclZipUtilOptionText($p_options_list[$i])." = '".$v_function_name."'");
1883 // ----- Check that the value is a valid existing function
1884 if (!function_exists($v_function_name)) {
1886 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1889 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1890 return PclZip::errorCode();
1893 // ----- Set the attribute
1894 $v_result_list[$p_options_list[$i]] = $v_function_name;
1900 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1901 "Unknown parameter '"
1902 .$p_options_list[$i]."'");
1905 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1906 return PclZip::errorCode();
1909 // ----- Next options
1913 // ----- Look for mandatory options
1914 if ($v_requested_options !== false) {
1915 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1916 // ----- Look for mandatory option
1917 if ($v_requested_options[$key] == 'mandatory') {
1918 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
1919 // ----- Look if present
1920 if (!isset($v_result_list[$key])) {
1922 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1925 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
1926 return PclZip::errorCode();
1932 // ----- Look for default values
1933 if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1934 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Calculate auto threshold");
1939 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1942 // --------------------------------------------------------------------------------
1944 // --------------------------------------------------------------------------------
1945 // Function : privOptionDefaultThreshold()
1949 // --------------------------------------------------------------------------------
1950 function privOptionDefaultThreshold(&$p_options)
1952 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOptionDefaultThreshold", "");
1955 if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
1956 || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
1957 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1961 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Create an auto-threshold for use of temporay files");
1962 // ----- Get 'memory_limit' configuration value
1963 $v_memory_limit = ini_get('memory_limit');
1964 $v_memory_limit = trim($v_memory_limit);
1965 $last = strtolower(substr($v_memory_limit, -1));
1968 //$v_memory_limit = $v_memory_limit*1024*1024*1024;
1969 $v_memory_limit = $v_memory_limit*1073741824;
1971 //$v_memory_limit = $v_memory_limit*1024*1024;
1972 $v_memory_limit = $v_memory_limit*1048576;
1974 $v_memory_limit = $v_memory_limit*1024;
1976 $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
1978 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Current memory usage : ".memory_get_usage(TRUE)." bytes");
1979 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Threshold value is : ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]." bytes");
1981 // ----- Sanity check : No threshold if value lower than 1M
1982 if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
1983 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Unset the threshold (value ".$p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD].") because under 1Mo sanity check)");
1984 unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
1988 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
1991 // --------------------------------------------------------------------------------
1993 // --------------------------------------------------------------------------------
1994 // Function : privFileDescrParseAtt()
2000 // --------------------------------------------------------------------------------
2001 function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
2003 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrParseAtt", "");
2006 // ----- For each file in the list check the attributes
2007 foreach ($p_file_list as $v_key => $v_value) {
2009 // ----- Check if the option is supported
2010 if (!isset($v_requested_options[$v_key])) {
2012 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
2015 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2016 return PclZip::errorCode();
2019 // ----- Look for attribute
2021 case PCLZIP_ATT_FILE_NAME :
2022 if (!is_string($v_value)) {
2023 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2024 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2025 return PclZip::errorCode();
2028 $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
2029 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2031 if ($p_filedescr['filename'] == '') {
2032 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
2033 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2034 return PclZip::errorCode();
2039 case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
2040 if (!is_string($v_value)) {
2041 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2042 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2043 return PclZip::errorCode();
2046 $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
2047 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2049 if ($p_filedescr['new_short_name'] == '') {
2050 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
2051 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2052 return PclZip::errorCode();
2056 case PCLZIP_ATT_FILE_NEW_FULL_NAME :
2057 if (!is_string($v_value)) {
2058 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2059 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2060 return PclZip::errorCode();
2063 $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
2064 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2066 if ($p_filedescr['new_full_name'] == '') {
2067 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
2068 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2069 return PclZip::errorCode();
2073 // ----- Look for options that takes a string
2074 case PCLZIP_ATT_FILE_COMMENT :
2075 if (!is_string($v_value)) {
2076 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
2077 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2078 return PclZip::errorCode();
2081 $p_filedescr['comment'] = $v_value;
2082 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2085 case PCLZIP_ATT_FILE_MTIME :
2086 if (!is_integer($v_value)) {
2087 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
2088 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2089 return PclZip::errorCode();
2092 $p_filedescr['mtime'] = $v_value;
2093 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2096 case PCLZIP_ATT_FILE_CONTENT :
2097 $p_filedescr['content'] = $v_value;
2098 ////--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "".PclZipUtilOptionText($v_key)." = '".$v_value."'");
2103 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
2104 "Unknown parameter '".$v_key."'");
2107 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2108 return PclZip::errorCode();
2111 // ----- Look for mandatory options
2112 if ($v_requested_options !== false) {
2113 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
2114 // ----- Look for mandatory option
2115 if ($v_requested_options[$key] == 'mandatory') {
2116 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Detect a mandatory option : ".PclZipUtilOptionText($key)."(".$key.")");
2117 // ----- Look if present
2118 if (!isset($p_file_list[$key])) {
2119 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
2120 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2121 return PclZip::errorCode();
2131 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2134 // --------------------------------------------------------------------------------
2136 // --------------------------------------------------------------------------------
2137 // Function : privFileDescrExpand()
2139 // This method look for each item of the list to see if its a file, a folder
2140 // or a string to be added as file. For any other type of files (link, other)
2141 // just ignore the item.
2142 // Then prepare the information that will be stored for that file.
2143 // When its a folder, expand the folder with all the files that are in that
2144 // folder (recursively).
2149 // --------------------------------------------------------------------------------
2150 function privFileDescrExpand(&$p_filedescr_list, &$p_options)
2152 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privFileDescrExpand", "");
2155 // ----- Create a result list
2156 $v_result_list = array();
2158 // ----- Look each entry
2159 for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
2160 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Looking for file ".$i.".");
2162 // ----- Get filedescr
2163 $v_descr = $p_filedescr_list[$i];
2165 // ----- Reduce the filename
2166 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr before reduction :'".$v_descr['filename']."'");
2167 $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
2168 $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
2169 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Filedescr after reduction :'".$v_descr['filename']."'");
2171 // ----- Look for real file or folder
2172 if (file_exists($v_descr['filename'])) {
2173 if (@is_file($v_descr['filename'])) {
2174 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a file");
2175 $v_descr['type'] = 'file';
2177 else if (@is_dir($v_descr['filename'])) {
2178 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a folder");
2179 $v_descr['type'] = 'folder';
2181 else if (@is_link($v_descr['filename'])) {
2182 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : link");
2187 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Unsupported file type : unknown type");
2193 // ----- Look for string added as file
2194 else if (isset($v_descr['content'])) {
2195 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "This is a string added as a file");
2196 $v_descr['type'] = 'virtual_file';
2199 // ----- Missing file
2202 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$v_descr['filename']."' does not exist");
2203 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
2206 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2207 return PclZip::errorCode();
2210 // ----- Calculate the stored filename
2211 $this->privCalculateStoredFilename($v_descr, $p_options);
2213 // ----- Add the descriptor in result list
2214 $v_result_list[sizeof($v_result_list)] = $v_descr;
2216 // ----- Look for folder
2217 if ($v_descr['type'] == 'folder') {
2218 // ----- List of items in folder
2219 $v_dirlist_descr = array();
2221 if ($v_folder_handler = @opendir($v_descr['filename'])) {
2222 while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
2223 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for '".$v_item_handler."' in the directory");
2225 // ----- Skip '.' and '..'
2226 if (($v_item_handler == '.') || ($v_item_handler == '..')) {
2230 // ----- Compose the full filename
2231 $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
2233 // ----- Look for different stored filename
2234 // Because the name of the folder was changed, the name of the
2235 // files/sub-folders also change
2236 if ($v_descr['stored_filename'] != $v_descr['filename']) {
2237 if ($v_descr['stored_filename'] != '') {
2238 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
2241 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
2248 @closedir($v_folder_handler);
2251 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Unable to open dir '".$v_descr['filename']."' in read mode. Skipped.");
2252 // TBC : unable to open folder in read mode
2255 // ----- Expand each element of the list
2256 if ($v_dirlist_nb != 0) {
2258 if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
2259 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2263 // ----- Concat the resulting list
2264 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Merging result list (size '".sizeof($v_result_list)."') with dirlist (size '".sizeof($v_dirlist_descr)."')");
2265 $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
2266 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "merged result list is size '".sizeof($v_result_list)."'");
2269 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Nothing in this folder to expand.");
2272 // ----- Free local array
2273 unset($v_dirlist_descr);
2277 // ----- Get the result list
2278 $p_filedescr_list = $v_result_list;
2281 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2284 // --------------------------------------------------------------------------------
2286 // --------------------------------------------------------------------------------
2287 // Function : privCreate()
2291 // --------------------------------------------------------------------------------
2292 function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2294 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCreate", "list");
2296 $v_list_detail = array();
2298 // ----- Magic quotes trick
2299 $this->privDisableMagicQuotes();
2301 // ----- Open the file in write mode
2302 if (($v_result = $this->privOpenFd('wb')) != 1)
2305 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2309 // ----- Add the list of files
2310 $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2313 $this->privCloseFd();
2315 // ----- Magic quotes trick
2316 $this->privSwapBackMagicQuotes();
2319 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2322 // --------------------------------------------------------------------------------
2324 // --------------------------------------------------------------------------------
2325 // Function : privAdd()
2329 // --------------------------------------------------------------------------------
2330 function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2332 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAdd", "list");
2334 $v_list_detail = array();
2336 // ----- Look if the archive exists or is empty
2337 if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
2339 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Archive does not exist, or is empty, create it.");
2341 // ----- Do a create
2342 $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2345 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2348 // ----- Magic quotes trick
2349 $this->privDisableMagicQuotes();
2351 // ----- Open the zip file
2352 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2353 if (($v_result=$this->privOpenFd('rb')) != 1)
2355 // ----- Magic quotes trick
2356 $this->privSwapBackMagicQuotes();
2359 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2363 // ----- Read the central directory informations
2364 $v_central_dir = array();
2365 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2367 $this->privCloseFd();
2368 $this->privSwapBackMagicQuotes();
2369 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2373 // ----- Go to beginning of File
2374 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2375 @rewind($this->zip_fd);
2376 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Position in file : ".ftell($this->zip_fd)."'");
2378 // ----- Creates a temporay file
2379 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
2381 // ----- Open the temporary file in write mode
2382 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
2383 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
2385 $this->privCloseFd();
2386 $this->privSwapBackMagicQuotes();
2388 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2391 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2392 return PclZip::errorCode();
2395 // ----- Copy the files from the archive to the temporary file
2396 // TBC : Here I should better append the file and go back to erase the central dir
2397 $v_size = $v_central_dir['offset'];
2398 while ($v_size != 0)
2400 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2401 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2402 $v_buffer = fread($this->zip_fd, $v_read_size);
2403 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2404 $v_size -= $v_read_size;
2407 // ----- Swap the file descriptor
2408 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2409 // the following methods on the temporary fil and not the real archive
2410 $v_swap = $this->zip_fd;
2411 $this->zip_fd = $v_zip_temp_fd;
2412 $v_zip_temp_fd = $v_swap;
2414 // ----- Add the files
2415 $v_header_list = array();
2416 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2418 fclose($v_zip_temp_fd);
2419 $this->privCloseFd();
2420 @unlink($v_zip_temp_name);
2421 $this->privSwapBackMagicQuotes();
2424 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2428 // ----- Store the offset of the central dir
2429 $v_offset = @ftell($this->zip_fd);
2430 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "New offset of central dir : $v_offset");
2432 // ----- Copy the block of file headers from the old archive
2433 $v_size = $v_central_dir['size'];
2434 while ($v_size != 0)
2436 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2437 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read $v_read_size bytes");
2438 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
2439 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
2440 $v_size -= $v_read_size;
2443 // ----- Create the Central Dir files header
2444 for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
2446 // ----- Create the file header
2447 if ($v_header_list[$i]['status'] == 'ok') {
2448 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2449 fclose($v_zip_temp_fd);
2450 $this->privCloseFd();
2451 @unlink($v_zip_temp_name);
2452 $this->privSwapBackMagicQuotes();
2455 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2461 // ----- Transform the header to a 'usable' info
2462 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2465 // ----- Zip file comment
2466 $v_comment = $v_central_dir['comment'];
2467 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2468 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2470 if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
2471 $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
2473 if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
2474 $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
2477 // ----- Calculate the size of the central header
2478 $v_size = @ftell($this->zip_fd)-$v_offset;
2480 // ----- Create the central dir footer
2481 if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
2483 // ----- Reset the file list
2484 unset($v_header_list);
2485 $this->privSwapBackMagicQuotes();
2488 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2492 // ----- Swap back the file descriptor
2493 $v_swap = $this->zip_fd;
2494 $this->zip_fd = $v_zip_temp_fd;
2495 $v_zip_temp_fd = $v_swap;
2498 $this->privCloseFd();
2500 // ----- Close the temporary file
2501 @fclose($v_zip_temp_fd);
2503 // ----- Magic quotes trick
2504 $this->privSwapBackMagicQuotes();
2506 // ----- Delete the zip file
2507 // TBC : I should test the result ...
2508 @unlink($this->zipname);
2510 // ----- Rename the temporary file
2511 // TBC : I should test the result ...
2512 //@rename($v_zip_temp_name, $this->zipname);
2513 PclZipUtilRename($v_zip_temp_name, $this->zipname);
2516 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2519 // --------------------------------------------------------------------------------
2521 // --------------------------------------------------------------------------------
2522 // Function : privOpenFd()
2525 // --------------------------------------------------------------------------------
2526 function privOpenFd($p_mode)
2528 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privOpenFd", 'mode='.$p_mode);
2531 // ----- Look if already open
2532 if ($this->zip_fd != 0)
2535 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
2538 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2539 return PclZip::errorCode();
2542 // ----- Open the zip file
2543 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Open file in '.$p_mode.' mode');
2544 if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
2547 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
2550 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2551 return PclZip::errorCode();
2555 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2558 // --------------------------------------------------------------------------------
2560 // --------------------------------------------------------------------------------
2561 // Function : privCloseFd()
2564 // --------------------------------------------------------------------------------
2565 function privCloseFd()
2567 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCloseFd", "");
2570 if ($this->zip_fd != 0)
2571 @fclose($this->zip_fd);
2575 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2578 // --------------------------------------------------------------------------------
2580 // --------------------------------------------------------------------------------
2581 // Function : privAddList()
2583 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2584 // different from the real path of the file. This is usefull if you want to have PclTar
2585 // running in any directory, and memorize relative path from an other directory.
2587 // $p_list : An array containing the file or directory names to add in the tar
2588 // $p_result_list : list of added files with their properties (specially the status field)
2589 // $p_add_dir : Path to add in the filename path archived
2590 // $p_remove_dir : Path to remove in the filename path archived
2592 // --------------------------------------------------------------------------------
2593 // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2594 function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2596 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddList", "list");
2599 // ----- Add the files
2600 $v_header_list = array();
2601 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2604 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2608 // ----- Store the offset of the central dir
2609 $v_offset = @ftell($this->zip_fd);
2611 // ----- Create the Central Dir files header
2612 for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
2614 // ----- Create the file header
2615 if ($v_header_list[$i]['status'] == 'ok') {
2616 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2618 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2624 // ----- Transform the header to a 'usable' info
2625 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2628 // ----- Zip file comment
2630 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2631 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2634 // ----- Calculate the size of the central header
2635 $v_size = @ftell($this->zip_fd)-$v_offset;
2637 // ----- Create the central dir footer
2638 if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2640 // ----- Reset the file list
2641 unset($v_header_list);
2644 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2649 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2652 // --------------------------------------------------------------------------------
2654 // --------------------------------------------------------------------------------
2655 // Function : privAddFileList()
2658 // $p_filedescr_list : An array containing the file description
2659 // or directory names to add in the zip
2660 // $p_result_list : list of added files with their properties (specially the status field)
2662 // --------------------------------------------------------------------------------
2663 function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2665 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileList", "filedescr_list");
2667 $v_header = array();
2669 // ----- Recuperate the current number of elt in list
2670 $v_nb = sizeof($p_result_list);
2671 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Before add, list have ".$v_nb." elements");
2673 // ----- Loop on the files
2674 for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
2675 // ----- Format the filename
2676 $p_filedescr_list[$j]['filename']
2677 = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2679 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Looking for file '".$p_filedescr_list[$j]['filename']."'");
2681 // ----- Skip empty file names
2682 // TBC : Can this be possible ? not checked in DescrParseAtt ?
2683 if ($p_filedescr_list[$j]['filename'] == "") {
2684 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Skip empty filename");
2688 // ----- Check the filename
2689 if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
2690 && (!file_exists($p_filedescr_list[$j]['filename']))) {
2691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2692 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2693 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2694 return PclZip::errorCode();
2697 // ----- Look if it is a file or a dir with no all path remove option
2698 // or a dir with all its path removed
2699 // if ( (is_file($p_filedescr_list[$j]['filename']))
2700 // || ( is_dir($p_filedescr_list[$j]['filename'])
2701 if ( ($p_filedescr_list[$j]['type'] == 'file')
2702 || ($p_filedescr_list[$j]['type'] == 'virtual_file')
2703 || ( ($p_filedescr_list[$j]['type'] == 'folder')
2704 && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
2705 || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
2708 // ----- Add the file
2709 $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
2711 if ($v_result != 1) {
2712 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2716 // ----- Store the file infos
2717 $p_result_list[$v_nb++] = $v_header;
2720 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "After add, list have ".$v_nb." elements");
2723 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2726 // --------------------------------------------------------------------------------
2728 // --------------------------------------------------------------------------------
2729 // Function : privAddFile()
2733 // --------------------------------------------------------------------------------
2734 function privAddFile($p_filedescr, &$p_header, &$p_options)
2736 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFile", "filename='".$p_filedescr['filename']."'");
2739 // ----- Working variable
2740 $p_filename = $p_filedescr['filename'];
2742 // TBC : Already done in the fileAtt check ... ?
2743 if ($p_filename == "") {
2745 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
2748 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2749 return PclZip::errorCode();
2752 // ----- Look for a stored different filename
2754 if (isset($p_filedescr['stored_filename'])) {
2755 $v_stored_filename = $p_filedescr['stored_filename'];
2756 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is NOT the same "'.$v_stored_filename.'"');
2759 $v_stored_filename = $p_filedescr['stored_filename'];
2760 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'Stored filename is the same');
2764 // ----- Set the file properties
2766 $p_header['version'] = 20;
2767 $p_header['version_extracted'] = 10;
2768 $p_header['flag'] = 0;
2769 $p_header['compression'] = 0;
2770 $p_header['crc'] = 0;
2771 $p_header['compressed_size'] = 0;
2772 $p_header['filename_len'] = strlen($p_filename);
2773 $p_header['extra_len'] = 0;
2774 $p_header['disk'] = 0;
2775 $p_header['internal'] = 0;
2776 $p_header['offset'] = 0;
2777 $p_header['filename'] = $p_filename;
2778 // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
2779 $p_header['stored_filename'] = $p_filedescr['stored_filename'];
2780 $p_header['extra'] = '';
2781 $p_header['status'] = 'ok';
2782 $p_header['index'] = -1;
2784 // ----- Look for regular file
2785 if ($p_filedescr['type']=='file') {
2786 $p_header['external'] = 0x00000000;
2787 $p_header['size'] = filesize($p_filename);
2790 // ----- Look for regular folder
2791 else if ($p_filedescr['type']=='folder') {
2792 $p_header['external'] = 0x00000010;
2793 $p_header['mtime'] = filemtime($p_filename);
2794 $p_header['size'] = filesize($p_filename);
2797 // ----- Look for virtual file
2798 else if ($p_filedescr['type'] == 'virtual_file') {
2799 $p_header['external'] = 0x00000000;
2800 $p_header['size'] = strlen($p_filedescr['content']);
2803 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 5, "Header external extension '".sprintf("0x%X",$p_header['external'])."'");
2805 // ----- Look for filetime
2806 if (isset($p_filedescr['mtime'])) {
2807 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3,"Overload mtime value with :'".$p_filedescr['mtime']."'");
2808 $p_header['mtime'] = $p_filedescr['mtime'];
2810 else if ($p_filedescr['type'] == 'virtual_file') {
2811 $p_header['mtime'] = time();
2812 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Virtual file : use current time '".$p_header['mtime']."' for mtime value.");
2815 $p_header['mtime'] = filemtime($p_filename);
2818 // ------ Look for file comment
2819 if (isset($p_filedescr['comment'])) {
2820 $p_header['comment_len'] = strlen($p_filedescr['comment']);
2821 $p_header['comment'] = $p_filedescr['comment'];
2824 $p_header['comment_len'] = 0;
2825 $p_header['comment'] = '';
2828 // ----- Look for pre-add callback
2829 if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2830 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A pre-callback '".$p_options[PCLZIP_CB_PRE_ADD]."()') is defined for the extraction");
2832 // ----- Generate a local information
2833 $v_local_header = array();
2834 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2836 // ----- Call the callback
2837 // Here I do not use call_user_func() because I need to send a reference to the
2839 eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
2840 if ($v_result == 0) {
2841 // ----- Change the file status
2842 $p_header['status'] = "skipped";
2846 // ----- Update the informations
2847 // Only some fields can be modified
2848 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2849 $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2850 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "New stored filename is '".$p_header['stored_filename']."'");
2854 // ----- Look for empty stored filename
2855 if ($p_header['stored_filename'] == "") {
2856 $p_header['status'] = "filtered";
2859 // ----- Check the path length
2860 if (strlen($p_header['stored_filename']) > 0xFF) {
2861 $p_header['status'] = 'filename_too_long';
2864 // ----- Look if no error, or file not skipped
2865 if ($p_header['status'] == 'ok') {
2867 // ----- Look for a file
2868 if ($p_filedescr['type'] == 'file') {
2869 // ----- Look for using temporary file to zip
2870 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
2871 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
2872 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
2873 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
2874 $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
2875 if ($v_result < PCLZIP_ERR_NO_ERROR) {
2880 // ----- Use "in memory" zip algo
2882 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"In memory compression.");
2883 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory usage : ".memory_get_usage(TRUE)." bytes");
2884 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Current memory peak : ".memory_get_peak_usage(TRUE)." bytes");
2885 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
2887 // ----- Open the source file
2888 if (($v_file = @fopen($p_filename, "rb")) == 0) {
2889 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2890 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
2891 return PclZip::errorCode();
2894 // ----- Read the file content
2895 $v_content = @fread($v_file, $p_header['size']);
2896 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after reading file : ".memory_get_usage(TRUE)." bytes");
2897 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after reading file : ".memory_get_peak_usage(TRUE)." bytes");
2899 // ----- Close the file
2902 // ----- Calculate the CRC
2903 $p_header['crc'] = @crc32($v_content);
2905 // ----- Look for no compression
2906 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2907 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2908 // ----- Set header parameters
2909 $p_header['compressed_size'] = $p_header['size'];
2910 $p_header['compression'] = 0;
2913 // ----- Look for normal compression
2915 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2916 // ----- Compress the content
2917 $v_content = @gzdeflate($v_content);
2918 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory usage after gzdeflate : ".memory_get_usage(TRUE)." bytes");
2919 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2,"Memory peak after gzdeflate : ".memory_get_peak_usage(TRUE)." bytes");
2921 // ----- Set header parameters
2922 $p_header['compressed_size'] = strlen($v_content);
2923 $p_header['compression'] = 8;
2926 // ----- Call the header generation
2927 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2929 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2933 // ----- Write the compressed (or not) content
2934 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2940 // ----- Look for a virtual file (a file from string)
2941 else if ($p_filedescr['type'] == 'virtual_file') {
2943 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Add by string");
2944 $v_content = $p_filedescr['content'];
2946 // ----- Calculate the CRC
2947 $p_header['crc'] = @crc32($v_content);
2949 // ----- Look for no compression
2950 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2951 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will not be compressed");
2952 // ----- Set header parameters
2953 $p_header['compressed_size'] = $p_header['size'];
2954 $p_header['compression'] = 0;
2957 // ----- Look for normal compression
2959 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "File will be compressed");
2960 // ----- Compress the content
2961 $v_content = @gzdeflate($v_content);
2963 // ----- Set header parameters
2964 $p_header['compressed_size'] = strlen($v_content);
2965 $p_header['compression'] = 8;
2968 // ----- Call the header generation
2969 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2971 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
2975 // ----- Write the compressed (or not) content
2976 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2979 // ----- Look for a directory
2980 else if ($p_filedescr['type'] == 'folder') {
2981 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a folder");
2982 // ----- Look for directory last '/'
2983 if (@substr($p_header['stored_filename'], -1) != '/') {
2984 $p_header['stored_filename'] .= '/';
2987 // ----- Set the file properties
2988 $p_header['size'] = 0;
2989 //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
2990 $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2992 // ----- Call the header generation
2993 if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2995 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3001 // ----- Look for post-add callback
3002 if (isset($p_options[PCLZIP_CB_POST_ADD])) {
3003 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "A post-callback '".$p_options[PCLZIP_CB_POST_ADD]."()') is defined for the extraction");
3005 // ----- Generate a local information
3006 $v_local_header = array();
3007 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
3009 // ----- Call the callback
3010 // Here I do not use call_user_func() because I need to send a reference to the
3012 eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
3013 if ($v_result == 0) {
3018 // ----- Update the informations
3019 // Nothing can be modified
3023 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3026 // --------------------------------------------------------------------------------
3028 // --------------------------------------------------------------------------------
3029 // Function : privAddFileUsingTempFile()
3033 // --------------------------------------------------------------------------------
3034 function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
3036 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privAddFileUsingTempFile", "filename='".$p_filedescr['filename']."'");
3037 $v_result=PCLZIP_ERR_NO_ERROR;
3039 // ----- Working variable
3040 $p_filename = $p_filedescr['filename'];
3042 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "'".$p_filename."' is a file");
3044 // ----- Open the source file
3045 if (($v_file = @fopen($p_filename, "rb")) == 0) {
3046 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
3047 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3048 return PclZip::errorCode();
3051 // ----- Creates a compressed temporary file
3052 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
3053 if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
3055 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
3056 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3057 return PclZip::errorCode();
3060 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3061 $v_size = filesize($p_filename);
3062 while ($v_size != 0) {
3063 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3064 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
3065 $v_buffer = @fread($v_file, $v_read_size);
3066 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3067 @gzputs($v_file_compressed, $v_buffer, $v_read_size);
3068 $v_size -= $v_read_size;
3071 // ----- Close the file
3073 @gzclose($v_file_compressed);
3075 // ----- Check the minimum file size
3076 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "gzip file size ".filesize($v_gzip_temp_name));
3077 if (filesize($v_gzip_temp_name) < 18) {
3078 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
3079 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3080 return PclZip::errorCode();
3083 // ----- Extract the compressed attributes
3084 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
3085 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
3086 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3087 return PclZip::errorCode();
3090 // ----- Read the gzip file header
3091 $v_binary_data = @fread($v_file_compressed, 10);
3092 $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
3094 // ----- Check some parameters
3095 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id1]='.bin2hex($v_data_header['id1']));
3096 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[id2]='.bin2hex($v_data_header['id2']));
3097 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[cm]='.bin2hex($v_data_header['cm']));
3098 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[flag]='.bin2hex($v_data_header['flag']));
3099 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[mtime]='.$v_data_header['mtime']);
3100 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[xfl]='.bin2hex($v_data_header['xfl']));
3101 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, '$v_data_header[os]='.bin2hex($v_data_header['os']));
3102 $v_data_header['os'] = bin2hex($v_data_header['os']);
3104 // ----- Read the gzip file footer
3105 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after header ".ftell($v_file_compressed));
3106 @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
3107 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position at beginning of footer ".ftell($v_file_compressed));
3108 $v_binary_data = @fread($v_file_compressed, 8);
3109 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position after footer ".ftell($v_file_compressed));
3110 $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
3112 // ----- Set the attributes
3113 $p_header['compression'] = ord($v_data_header['cm']);
3114 //$p_header['mtime'] = $v_data_header['mtime'];
3115 $p_header['crc'] = $v_data_footer['crc'];
3116 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Compressed size ".(filesize($v_gzip_temp_name)-18));
3117 $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
3119 // ----- Close the file
3120 @fclose($v_file_compressed);
3122 // ----- Call the header generation
3123 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
3124 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3128 // ----- Add the compressed data
3129 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
3131 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
3132 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3133 return PclZip::errorCode();
3136 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3137 fseek($v_file_compressed, 10);
3138 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "File position before reading compressed data ".ftell($v_file_compressed));
3139 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, ' '.$p_header['compressed_size'].' bytes to read');
3140 $v_size = $p_header['compressed_size'];
3141 while ($v_size != 0)
3143 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3144 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Read ".$v_read_size." bytes");
3145 $v_buffer = @fread($v_file_compressed, $v_read_size);
3146 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3147 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
3148 $v_size -= $v_read_size;
3151 // ----- Close the file
3152 @fclose($v_file_compressed);
3154 // ----- Unlink the temporary file
3155 @unlink($v_gzip_temp_name);
3158 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3161 // --------------------------------------------------------------------------------
3163 // --------------------------------------------------------------------------------
3164 // Function : privCalculateStoredFilename()
3166 // Based on file descriptor properties and global options, this method
3167 // calculate the filename that will be stored in the archive.
3170 // --------------------------------------------------------------------------------
3171 function privCalculateStoredFilename(&$p_filedescr, &$p_options)
3173 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privCalculateStoredFilename", "filename='".$p_filedescr['filename']."'");
3176 // ----- Working variables
3177 $p_filename = $p_filedescr['filename'];
3178 if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
3179 $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
3184 if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
3185 $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
3190 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Remove path ='".$p_remove_dir."'");
3191 if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
3192 $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
3195 $p_remove_all_dir = 0;
3198 // ----- Look for full name change
3199 if (isset($p_filedescr['new_full_name'])) {
3200 // ----- Remove drive letter if any
3201 $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
3202 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing full name of '".$p_filename."' for '".$v_stored_filename."'");
3205 // ----- Look for path and/or short name change
3208 // ----- Look for short name change
3209 // Its when we cahnge just the filename but not the path
3210 if (isset($p_filedescr['new_short_name'])) {
3211 $v_path_info = pathinfo($p_filename);
3213 if ($v_path_info['dirname'] != '') {
3214 $v_dir = $v_path_info['dirname'].'/';
3216 $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
3217 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Changing short name of '".$p_filename."' for '".$v_stored_filename."'");
3220 // ----- Calculate the stored filename
3221 $v_stored_filename = $p_filename;
3224 // ----- Look for all path to remove
3225 if ($p_remove_all_dir) {
3226 $v_stored_filename = basename($p_filename);
3227 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove all path selected change '".$p_filename."' for '".$v_stored_filename."'");
3229 // ----- Look for partial path remove
3230 else if ($p_remove_dir != "") {
3231 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Partial path to remove");
3232 if (substr($p_remove_dir, -1) != '/')
3233 $p_remove_dir .= "/";
3235 if ( (substr($p_filename, 0, 2) == "./")
3236 || (substr($p_remove_dir, 0, 2) == "./")) {
3238 if ( (substr($p_filename, 0, 2) == "./")
3239 && (substr($p_remove_dir, 0, 2) != "./")) {
3240 $p_remove_dir = "./".$p_remove_dir;
3242 if ( (substr($p_filename, 0, 2) != "./")
3243 && (substr($p_remove_dir, 0, 2) == "./")) {
3244 $p_remove_dir = substr($p_remove_dir, 2);
3248 $v_compare = PclZipUtilPathInclusion($p_remove_dir,
3249 $v_stored_filename);
3250 if ($v_compare > 0) {
3251 if ($v_compare == 2) {
3252 $v_stored_filename = "";
3253 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Path to remove is the current folder");
3256 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Remove path '$p_remove_dir' in file '$v_stored_filename'");
3257 $v_stored_filename = substr($v_stored_filename,
3258 strlen($p_remove_dir));
3259 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Result is '$v_stored_filename'");
3264 // ----- Remove drive letter if any
3265 $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
3267 // ----- Look for path to add
3268 if ($p_add_dir != "") {
3269 if (substr($p_add_dir, -1) == "/")
3270 $v_stored_filename = $p_add_dir.$v_stored_filename;
3272 $v_stored_filename = $p_add_dir."/".$v_stored_filename;
3273 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Add path '$p_add_dir' in file '$p_filename' = '$v_stored_filename'");
3277 // ----- Filename (reduce the path of stored name)
3278 $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
3279 $p_filedescr['stored_filename'] = $v_stored_filename;
3280 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, "Stored filename will be '".$p_filedescr['stored_filename']."', strlen ".strlen($p_filedescr['stored_filename']));
3283 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3286 // --------------------------------------------------------------------------------
3288 // --------------------------------------------------------------------------------
3289 // Function : privWriteFileHeader()
3293 // --------------------------------------------------------------------------------
3294 function privWriteFileHeader(&$p_header)
3296 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
3299 // ----- Store the offset position of the file
3300 $p_header['offset'] = ftell($this->zip_fd);
3301 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 2, 'File offset of the header :'.$p_header['offset']);
3303 // ----- Transform UNIX mtime to DOS format mdate/mtime
3304 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3305 $v_date = getdate($p_header['mtime']);
3306 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3307 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3309 // ----- Packed data
3310 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
3311 $p_header['version_extracted'], $p_header['flag'],
3312 $p_header['compression'], $v_mtime, $v_mdate,
3313 $p_header['crc'], $p_header['compressed_size'],
3315 strlen($p_header['stored_filename']),
3316 $p_header['extra_len']);
3318 // ----- Write the first 148 bytes of the header in the archive
3319 fputs($this->zip_fd, $v_binary_data, 30);
3321 // ----- Write the variable fields
3322 if (strlen($p_header['stored_filename']) != 0)
3324 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3326 if ($p_header['extra_len'] != 0)
3328 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3332 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3335 // --------------------------------------------------------------------------------
3337 // --------------------------------------------------------------------------------
3338 // Function : privWriteCentralFileHeader()
3342 // --------------------------------------------------------------------------------
3343 function privWriteCentralFileHeader(&$p_header)
3345 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralFileHeader", 'file="'.$p_header['filename'].'", stored as "'.$p_header['stored_filename'].'"');
3349 //for(reset($p_header); $key = key($p_header); next($p_header)) {
3350 // //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "header[$key] = ".$p_header[$key]);
3353 // ----- Transform UNIX mtime to DOS format mdate/mtime
3354 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Date : \''.date("d/m/y H:i:s", $p_header['mtime']).'\'');
3355 $v_date = getdate($p_header['mtime']);
3356 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3357 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3359 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, 'Comment size : \''.$p_header['comment_len'].'\'');
3361 // ----- Packed data
3362 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
3363 $p_header['version'], $p_header['version_extracted'],
3364 $p_header['flag'], $p_header['compression'],
3365 $v_mtime, $v_mdate, $p_header['crc'],
3366 $p_header['compressed_size'], $p_header['size'],
3367 strlen($p_header['stored_filename']),
3368 $p_header['extra_len'], $p_header['comment_len'],
3369 $p_header['disk'], $p_header['internal'],
3370 $p_header['external'], $p_header['offset']);
3372 // ----- Write the 42 bytes of the header in the zip file
3373 fputs($this->zip_fd, $v_binary_data, 46);
3375 // ----- Write the variable fields
3376 if (strlen($p_header['stored_filename']) != 0)
3378 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3380 if ($p_header['extra_len'] != 0)
3382 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3384 if ($p_header['comment_len'] != 0)
3386 fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
3390 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3393 // --------------------------------------------------------------------------------
3395 // --------------------------------------------------------------------------------
3396 // Function : privWriteCentralHeader()
3400 // --------------------------------------------------------------------------------
3401 function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
3403 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privWriteCentralHeader", 'nb_entries='.$p_nb_entries.', size='.$p_size.', offset='.$p_offset.', comment="'.$p_comment.'"');
3406 // ----- Packed data
3407 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
3408 $p_nb_entries, $p_size,
3409 $p_offset, strlen($p_comment));
3411 // ----- Write the 22 bytes of the header in the zip file
3412 fputs($this->zip_fd, $v_binary_data, 22);
3414 // ----- Write the variable fields
3415 if (strlen($p_comment) != 0)
3417 fputs($this->zip_fd, $p_comment, strlen($p_comment));
3421 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3424 // --------------------------------------------------------------------------------
3426 // --------------------------------------------------------------------------------
3427 // Function : privList()
3431 // --------------------------------------------------------------------------------
3432 function privList(&$p_list)
3434 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privList", "list");
3437 // ----- Magic quotes trick
3438 $this->privDisableMagicQuotes();
3440 // ----- Open the zip file
3441 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
3442 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
3444 // ----- Magic quotes trick
3445 $this->privSwapBackMagicQuotes();
3448 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
3451 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3452 return PclZip::errorCode();
3455 // ----- Read the central directory informations
3456 $v_central_dir = array();
3457 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3459 $this->privSwapBackMagicQuotes();
3460 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3464 // ----- Go to beginning of Central Dir
3465 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Offset : ".$v_central_dir['offset']."'");
3466 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3467 @rewind($this->zip_fd);
3468 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3469 if (@fseek($this->zip_fd, $v_central_dir['offset']))
3471 $this->privSwapBackMagicQuotes();
3474 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3477 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3478 return PclZip::errorCode();
3480 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position in file : ".ftell($this->zip_fd)."'");
3482 // ----- Read each entry
3483 for ($i=0; $i<$v_central_dir['entries']; $i++)
3485 // ----- Read the file header
3486 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3488 $this->privSwapBackMagicQuotes();
3489 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3492 $v_header['index'] = $i;
3494 // ----- Get the only interesting attributes
3495 $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3499 // ----- Close the zip file
3500 $this->privCloseFd();
3502 // ----- Magic quotes trick
3503 $this->privSwapBackMagicQuotes();
3506 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3509 // --------------------------------------------------------------------------------
3511 // --------------------------------------------------------------------------------
3512 // Function : privConvertHeader2FileInfo()
3514 // This function takes the file informations from the central directory
3515 // entries and extract the interesting parameters that will be given back.
3516 // The resulting file infos are set in the array $p_info
3517 // $p_info['filename'] : Filename with full path. Given by user (add),
3518 // extracted in the filesystem (extract).
3519 // $p_info['stored_filename'] : Stored filename in the archive.
3520 // $p_info['size'] = Size of the file.
3521 // $p_info['compressed_size'] = Compressed size of the file.
3522 // $p_info['mtime'] = Last modification date of the file.
3523 // $p_info['comment'] = Comment associated with the file.
3524 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3525 // $p_info['status'] = status of the action on the file.
3526 // $p_info['crc'] = CRC of the file content.
3529 // --------------------------------------------------------------------------------
3530 function privConvertHeader2FileInfo($p_header, &$p_info)
3532 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privConvertHeader2FileInfo", "Filename='".$p_header['filename']."'");
3535 // ----- Get the interesting attributes
3536 $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
3537 $p_info['filename'] = $v_temp_path;
3538 $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
3539 $p_info['stored_filename'] = $v_temp_path;
3540 $p_info['size'] = $p_header['size'];
3541 $p_info['compressed_size'] = $p_header['compressed_size'];
3542 $p_info['mtime'] = $p_header['mtime'];
3543 $p_info['comment'] = $p_header['comment'];
3544 $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
3545 $p_info['index'] = $p_header['index'];
3546 $p_info['status'] = $p_header['status'];
3547 $p_info['crc'] = $p_header['crc'];
3550 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3553 // --------------------------------------------------------------------------------
3555 // --------------------------------------------------------------------------------
3556 // Function : privExtractByRule()
3558 // Extract a file or directory depending of rules (by index, by name, ...)
3560 // $p_file_list : An array where will be placed the properties of each
3562 // $p_path : Path to add while writing the extracted files
3563 // $p_remove_path : Path to remove (from the file memorized path) while writing the
3564 // extracted files. If the path does not match the file path,
3565 // the file is extracted with its memorized path.
3566 // $p_remove_path does not apply to 'list' mode.
3567 // $p_path and $p_remove_path are commulative.
3569 // 1 on success,0 or less on error (see error code list)
3570 // --------------------------------------------------------------------------------
3571 function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3573 //--(MAGIC-PclTrace)--//PclTraceFctStart(__FILE__, __LINE__, "PclZip::privExtractByRule", "path='$p_path', remove_path='$p_remove_path', remove_all_path='".($p_remove_all_path?'true':'false')."'");
3576 // ----- Magic quotes trick
3577 $this->privDisableMagicQuotes();
3579 // ----- Check the path
3580 if ( ($p_path == "")
3581 || ( (substr($p_path, 0, 1) != "/")
3582 && (substr($p_path, 0, 3) != "../")
3583 && (substr($p_path,1,2)!=":/")))
3584 $p_path = "./".$p_path;
3586 // ----- Reduce the path last (and duplicated) '/'
3587 if (($p_path != "./") && ($p_path != "/"))
3589 // ----- Look for the path end '/'
3590 while (substr($p_path, -1) == "/")
3592 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Destination path [$p_path] ends by '/'");
3593 $p_path = substr($p_path, 0, strlen($p_path)-1);
3594 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Modified to [$p_path]");
3598 // ----- Look for path to remove format (should end by /)
3599 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
3601 $p_remove_path .= '/';
3603 $p_remove_path_size = strlen($p_remove_path);
3605 // ----- Open the zip file
3606 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Open file in binary read mode");
3607 if (($v_result = $this->privOpenFd('rb')) != 1)
3609 $this->privSwapBackMagicQuotes();
3610 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3614 // ----- Read the central directory informations
3615 $v_central_dir = array();
3616 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3618 // ----- Close the zip file
3619 $this->privCloseFd();
3620 $this->privSwapBackMagicQuotes();
3622 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3626 // ----- Start at beginning of Central Dir
3627 $v_pos_entry = $v_central_dir['offset'];
3629 // ----- Read each entry
3631 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
3633 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Read next file header entry : '$i'");
3635 // ----- Read next Central dir entry
3636 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position before rewind : ".ftell($this->zip_fd)."'");
3637 @rewind($this->zip_fd);
3638 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 4, "Position after rewind : ".ftell($this->zip_fd)."'");
3639 if (@fseek($this->zip_fd, $v_pos_entry))
3641 // ----- Close the zip file
3642 $this->privCloseFd();
3643 $this->privSwapBackMagicQuotes();
3646 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3649 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, PclZip::errorCode(), PclZip::errorInfo());
3650 return PclZip::errorCode();
3652 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Position after fseek : ".ftell($this->zip_fd)."'");
3654 // ----- Read the file header
3655 $v_header = array();
3656 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3658 // ----- Close the zip file
3659 $this->privCloseFd();
3660 $this->privSwapBackMagicQuotes();
3662 //--(MAGIC-PclTrace)--//PclTraceFctEnd(__FILE__, __LINE__, $v_result);
3666 // ----- Store the index
3667 $v_header['index'] = $i;
3669 // ----- Store the file position
3670 $v_pos_entry = ftell($this->zip_fd);
3672 // ----- Look for the specific extract rules
3675 // ----- Look for extract by name rule
3676 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
3677 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
3678 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Extract with rule 'ByName'");
3680 // ----- Look if the filename is in the list
3681 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
3682 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "Compare with file '".$p_options[PCLZIP_OPT_BY_NAME][$j]."'");
3684 // ----- Look for a directory
3685 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
3686 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The searched item is a directory");
3688 // ----- Look if the directory is in the filename path
3689 if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
3690 && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
3691 //--(MAGIC-PclTrace)--//PclTraceFctMessage(__FILE__, __LINE__, 3, "The directory is in the file path");