2 // --------------------------------------------------------------------------------
3 // PhpConcept Library - Zip Module 2.8.2
4 // --------------------------------------------------------------------------------
5 // License GNU/LGPL - Vincent Blavet - August 2009
6 // http://www.phpconcept.net
7 // --------------------------------------------------------------------------------
10 // PclZip is a PHP library that manage ZIP archives.
11 // So far tests show that archives generated by PclZip are readable by
12 // WinZip application and other tools.
15 // See readme.txt and http://www.phpconcept.net
18 // This library and the associated files are non commercial, non professional
20 // It should not have unexpected results. However if any damage is caused by
21 // this software the author can not be responsible.
22 // The use of this software is at the risk of the user.
24 // --------------------------------------------------------------------------------
25 // $Id: pclzip.lib.php,v 1.60 2009/09/30 21:01:04 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.2";
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 __construct($p_zipname)
218 // ----- Tests the zlib
219 if (!function_exists('gzopen'))
221 die('Abort '.basename(__FILE__).' : Missing zlib extensions');
224 // ----- Set the attributes
225 $this->zipname = $p_zipname;
227 $this->magic_quotes_status = -1;
233 public function PclZip($p_zipname) {
234 self::__construct($p_zipname);
236 // --------------------------------------------------------------------------------
238 // --------------------------------------------------------------------------------
240 // create($p_filelist, $p_add_dir="", $p_remove_dir="")
241 // create($p_filelist, $p_option, $p_option_value, ...)
243 // This method supports two different synopsis. The first one is historical.
244 // This method creates a Zip Archive. The Zip file is created in the
245 // filesystem. The files and directories indicated in $p_filelist
246 // are added in the archive. See the parameters description for the
247 // supported format of $p_filelist.
248 // When a directory is in the list, the directory and its content is added
250 // In this synopsis, the function takes an optional variable list of
251 // options. See bellow the supported options.
253 // $p_filelist : An array containing file or directory names, or
254 // a string containing one filename or one directory name, or
255 // a string containing a list of filenames and/or directory
256 // names separated by spaces.
257 // $p_add_dir : A path to add before the real path of the archived file,
258 // in order to have it memorized in the archive.
259 // $p_remove_dir : A path to remove from the real path of the file to archive,
260 // in order to have a shorter path memorized in the archive.
261 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
262 // is removed first, before $p_add_dir is added.
264 // PCLZIP_OPT_ADD_PATH :
265 // PCLZIP_OPT_REMOVE_PATH :
266 // PCLZIP_OPT_REMOVE_ALL_PATH :
267 // PCLZIP_OPT_COMMENT :
268 // PCLZIP_CB_PRE_ADD :
269 // PCLZIP_CB_POST_ADD :
272 // The list of the added files, with a status of the add action.
273 // (see PclZip::listContent() for list entry format)
274 // --------------------------------------------------------------------------------
275 function create($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();
289 // ----- Look for arguments
291 // ----- Get the arguments
292 $v_arg_list = func_get_args();
294 // ----- Remove from the options list the first argument
295 array_shift($v_arg_list);
298 // ----- Look for first arg
299 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
301 // ----- Parse the options
302 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
303 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
304 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
305 PCLZIP_OPT_ADD_PATH => 'optional',
306 PCLZIP_CB_PRE_ADD => 'optional',
307 PCLZIP_CB_POST_ADD => 'optional',
308 PCLZIP_OPT_NO_COMPRESSION => 'optional',
309 PCLZIP_OPT_COMMENT => 'optional',
310 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
311 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
312 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
313 //, PCLZIP_OPT_CRYPT => 'optional'
315 if ($v_result != 1) {
320 // ----- Look for 2 args
321 // Here we need to support the first historic synopsis of the
325 // ----- Get the first argument
326 $v_options[PCLZIP_OPT_ADD_PATH] = $v_arg_list[0];
328 // ----- Look for the optional second argument
330 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
332 else if ($v_size > 2) {
333 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
334 "Invalid number / type of arguments");
340 // ----- Look for default option values
341 $this->privOptionDefaultThreshold($v_options);
344 $v_string_list = array();
345 $v_att_list = array();
346 $v_filedescr_list = array();
347 $p_result_list = array();
349 // ----- Look if the $p_filelist is really an array
350 if (is_array($p_filelist)) {
352 // ----- Look if the first element is also an array
353 // This will mean that this is a file description entry
354 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
355 $v_att_list = $p_filelist;
358 // ----- The list is a list of string names
360 $v_string_list = $p_filelist;
364 // ----- Look if the $p_filelist is a string
365 else if (is_string($p_filelist)) {
366 // ----- Create a list from the string
367 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
370 // ----- Invalid variable type for $p_filelist
372 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_filelist");
376 // ----- Reformat the string list
377 if (sizeof($v_string_list) != 0) {
378 foreach ($v_string_list as $v_string) {
379 if ($v_string != '') {
380 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
387 // ----- For each file in the list check the attributes
388 $v_supported_attributes
389 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
390 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
391 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
392 ,PCLZIP_ATT_FILE_MTIME => 'optional'
393 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
394 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
396 foreach ($v_att_list as $v_entry) {
397 $v_result = $this->privFileDescrParseAtt($v_entry,
400 $v_supported_attributes);
401 if ($v_result != 1) {
406 // ----- Expand the filelist (expand directories)
407 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
408 if ($v_result != 1) {
412 // ----- Call the create fct
413 $v_result = $this->privCreate($v_filedescr_list, $p_result_list, $v_options);
414 if ($v_result != 1) {
419 return $p_result_list;
421 // --------------------------------------------------------------------------------
423 // --------------------------------------------------------------------------------
425 // add($p_filelist, $p_add_dir="", $p_remove_dir="")
426 // add($p_filelist, $p_option, $p_option_value, ...)
428 // This method supports two synopsis. The first one is historical.
429 // This methods add the list of files in an existing archive.
430 // If a file with the same name already exists, it is added at the end of the
431 // archive, the first one is still present.
432 // If the archive does not exist, it is created.
434 // $p_filelist : An array containing file or directory names, or
435 // a string containing one filename or one directory name, or
436 // a string containing a list of filenames and/or directory
437 // names separated by spaces.
438 // $p_add_dir : A path to add before the real path of the archived file,
439 // in order to have it memorized in the archive.
440 // $p_remove_dir : A path to remove from the real path of the file to archive,
441 // in order to have a shorter path memorized in the archive.
442 // When $p_add_dir and $p_remove_dir are set, $p_remove_dir
443 // is removed first, before $p_add_dir is added.
445 // PCLZIP_OPT_ADD_PATH :
446 // PCLZIP_OPT_REMOVE_PATH :
447 // PCLZIP_OPT_REMOVE_ALL_PATH :
448 // PCLZIP_OPT_COMMENT :
449 // PCLZIP_OPT_ADD_COMMENT :
450 // PCLZIP_OPT_PREPEND_COMMENT :
451 // PCLZIP_CB_PRE_ADD :
452 // PCLZIP_CB_POST_ADD :
455 // The list of the added files, with a status of the add action.
456 // (see PclZip::listContent() for list entry format)
457 // --------------------------------------------------------------------------------
458 function add($p_filelist)
462 // ----- Reset the error handler
463 $this->privErrorReset();
465 // ----- Set default values
466 $v_options = array();
467 $v_options[PCLZIP_OPT_NO_COMPRESSION] = FALSE;
469 // ----- Look for variable options arguments
470 $v_size = func_num_args();
472 // ----- Look for arguments
474 // ----- Get the arguments
475 $v_arg_list = func_get_args();
477 // ----- Remove form the options list the first argument
478 array_shift($v_arg_list);
481 // ----- Look for first arg
482 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
484 // ----- Parse the options
485 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
486 array (PCLZIP_OPT_REMOVE_PATH => 'optional',
487 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
488 PCLZIP_OPT_ADD_PATH => 'optional',
489 PCLZIP_CB_PRE_ADD => 'optional',
490 PCLZIP_CB_POST_ADD => 'optional',
491 PCLZIP_OPT_NO_COMPRESSION => 'optional',
492 PCLZIP_OPT_COMMENT => 'optional',
493 PCLZIP_OPT_ADD_COMMENT => 'optional',
494 PCLZIP_OPT_PREPEND_COMMENT => 'optional',
495 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
496 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
497 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
498 //, PCLZIP_OPT_CRYPT => 'optional'
500 if ($v_result != 1) {
505 // ----- Look for 2 args
506 // Here we need to support the first historic synopsis of the
510 // ----- Get the first argument
511 $v_options[PCLZIP_OPT_ADD_PATH] = $v_add_path = $v_arg_list[0];
513 // ----- Look for the optional second argument
515 $v_options[PCLZIP_OPT_REMOVE_PATH] = $v_arg_list[1];
517 else if ($v_size > 2) {
519 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
527 // ----- Look for default option values
528 $this->privOptionDefaultThreshold($v_options);
531 $v_string_list = array();
532 $v_att_list = array();
533 $v_filedescr_list = array();
534 $p_result_list = array();
536 // ----- Look if the $p_filelist is really an array
537 if (is_array($p_filelist)) {
539 // ----- Look if the first element is also an array
540 // This will mean that this is a file description entry
541 if (isset($p_filelist[0]) && is_array($p_filelist[0])) {
542 $v_att_list = $p_filelist;
545 // ----- The list is a list of string names
547 $v_string_list = $p_filelist;
551 // ----- Look if the $p_filelist is a string
552 else if (is_string($p_filelist)) {
553 // ----- Create a list from the string
554 $v_string_list = explode(PCLZIP_SEPARATOR, $p_filelist);
557 // ----- Invalid variable type for $p_filelist
559 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type '".gettype($p_filelist)."' for p_filelist");
563 // ----- Reformat the string list
564 if (sizeof($v_string_list) != 0) {
565 foreach ($v_string_list as $v_string) {
566 $v_att_list[][PCLZIP_ATT_FILE_NAME] = $v_string;
570 // ----- For each file in the list check the attributes
571 $v_supported_attributes
572 = array ( PCLZIP_ATT_FILE_NAME => 'mandatory'
573 ,PCLZIP_ATT_FILE_NEW_SHORT_NAME => 'optional'
574 ,PCLZIP_ATT_FILE_NEW_FULL_NAME => 'optional'
575 ,PCLZIP_ATT_FILE_MTIME => 'optional'
576 ,PCLZIP_ATT_FILE_CONTENT => 'optional'
577 ,PCLZIP_ATT_FILE_COMMENT => 'optional'
579 foreach ($v_att_list as $v_entry) {
580 $v_result = $this->privFileDescrParseAtt($v_entry,
583 $v_supported_attributes);
584 if ($v_result != 1) {
589 // ----- Expand the filelist (expand directories)
590 $v_result = $this->privFileDescrExpand($v_filedescr_list, $v_options);
591 if ($v_result != 1) {
595 // ----- Call the create fct
596 $v_result = $this->privAdd($v_filedescr_list, $p_result_list, $v_options);
597 if ($v_result != 1) {
602 return $p_result_list;
604 // --------------------------------------------------------------------------------
606 // --------------------------------------------------------------------------------
607 // Function : listContent()
609 // This public method, gives the list of the files and directories, with their
611 // The properties of each entries in the list are (used also in other functions) :
612 // filename : Name of the file. For a create or add action it is the filename
613 // given by the user. For an extract function it is the filename
614 // of the extracted file.
615 // stored_filename : Name of the file / directory stored in the archive.
616 // size : Size of the stored file.
617 // compressed_size : Size of the file's data compressed in the archive
618 // (without the headers overhead)
619 // mtime : Last known modification date of the file (UNIX timestamp)
620 // comment : Comment associated with the file
621 // folder : true | false
622 // index : index of the file in the archive
623 // status : status of the action (depending of the action) :
626 // filtered : the file / dir is not extracted (filtered by user)
627 // already_a_directory : the file can not be extracted because a
628 // directory with the same name already exists
629 // write_protected : the file can not be extracted because a file
630 // with the same name already exists and is
632 // newer_exist : the file was not extracted because a newer file exists
633 // path_creation_fail : the file is not extracted because the folder
634 // does not exist and can not be created
635 // write_error : the file was not extracted because there was a
636 // error while writing the file
637 // read_error : the file was not extracted because there was a error
638 // while reading the file
639 // invalid_header : the file was not extracted because of an archive
640 // format error (bad file header)
641 // Note that each time a method can continue operating when there
642 // is an action error on a file, the error is only logged in the file status.
644 // 0 on an unrecoverable failure,
645 // The list of the files in the archive.
646 // --------------------------------------------------------------------------------
647 function listContent()
651 // ----- Reset the error handler
652 $this->privErrorReset();
654 // ----- Check archive
655 if (!$this->privCheckFormat()) {
659 // ----- Call the extracting fct
661 if (($v_result = $this->privList($p_list)) != 1)
670 // --------------------------------------------------------------------------------
672 // --------------------------------------------------------------------------------
674 // extract($p_path="./", $p_remove_path="")
675 // extract([$p_option, $p_option_value, ...])
677 // This method supports two synopsis. The first one is historical.
678 // This method extract all the files / directories from the archive to the
679 // folder indicated in $p_path.
680 // If you want to ignore the 'root' part of path of the memorized files
681 // you can indicate this in the optional $p_remove_path parameter.
682 // By default, if a newer file with the same name already exists, the
683 // file is not extracted.
685 // If both PCLZIP_OPT_PATH and PCLZIP_OPT_ADD_PATH aoptions
686 // are used, the path indicated in PCLZIP_OPT_ADD_PATH is append
687 // at the end of the path value of PCLZIP_OPT_PATH.
689 // $p_path : Path where the files and directories are to be extracted
690 // $p_remove_path : First part ('root' part) of the memorized path
691 // (if any similar) to remove while extracting.
694 // PCLZIP_OPT_ADD_PATH :
695 // PCLZIP_OPT_REMOVE_PATH :
696 // PCLZIP_OPT_REMOVE_ALL_PATH :
697 // PCLZIP_CB_PRE_EXTRACT :
698 // PCLZIP_CB_POST_EXTRACT :
700 // 0 or a negative value on failure,
701 // The list of the extracted files, with a status of the action.
702 // (see PclZip::listContent() for list entry format)
703 // --------------------------------------------------------------------------------
708 // ----- Reset the error handler
709 $this->privErrorReset();
711 // ----- Check archive
712 if (!$this->privCheckFormat()) {
716 // ----- Set default values
717 $v_options = array();
721 $v_remove_all_path = false;
723 // ----- Look for variable options arguments
724 $v_size = func_num_args();
726 // ----- Default values for option
727 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
729 // ----- Look for arguments
731 // ----- Get the arguments
732 $v_arg_list = func_get_args();
734 // ----- Look for first arg
735 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
737 // ----- Parse the options
738 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
739 array (PCLZIP_OPT_PATH => 'optional',
740 PCLZIP_OPT_REMOVE_PATH => 'optional',
741 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
742 PCLZIP_OPT_ADD_PATH => 'optional',
743 PCLZIP_CB_PRE_EXTRACT => 'optional',
744 PCLZIP_CB_POST_EXTRACT => 'optional',
745 PCLZIP_OPT_SET_CHMOD => 'optional',
746 PCLZIP_OPT_BY_NAME => 'optional',
747 PCLZIP_OPT_BY_EREG => 'optional',
748 PCLZIP_OPT_BY_PREG => 'optional',
749 PCLZIP_OPT_BY_INDEX => 'optional',
750 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
751 PCLZIP_OPT_EXTRACT_IN_OUTPUT => 'optional',
752 PCLZIP_OPT_REPLACE_NEWER => 'optional'
753 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
754 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
755 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
756 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
757 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
759 if ($v_result != 1) {
763 // ----- Set the arguments
764 if (isset($v_options[PCLZIP_OPT_PATH])) {
765 $v_path = $v_options[PCLZIP_OPT_PATH];
767 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
768 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
770 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
771 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
773 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
774 // ----- Check for '/' in last path char
775 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
778 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
782 // ----- Look for 2 args
783 // Here we need to support the first historic synopsis of the
787 // ----- Get the first argument
788 $v_path = $v_arg_list[0];
790 // ----- Look for the optional second argument
792 $v_remove_path = $v_arg_list[1];
794 else if ($v_size > 2) {
796 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
804 // ----- Look for default option values
805 $this->privOptionDefaultThreshold($v_options);
809 // ----- Call the extracting fct
811 $v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path,
812 $v_remove_all_path, $v_options);
821 // --------------------------------------------------------------------------------
824 // --------------------------------------------------------------------------------
826 // extractByIndex($p_index, $p_path="./", $p_remove_path="")
827 // extractByIndex($p_index, [$p_option, $p_option_value, ...])
829 // This method supports two synopsis. The first one is historical.
830 // This method is doing a partial extract of the archive.
831 // The extracted files or folders are identified by their index in the
832 // archive (from 0 to n).
833 // Note that if the index identify a folder, only the folder entry is
834 // extracted, not all the files included in the archive.
836 // $p_index : A single index (integer) or a string of indexes of files to
837 // extract. The form of the string is "0,4-6,8-12" with only numbers
838 // and '-' for range or ',' to separate ranges. No spaces or ';'
840 // $p_path : Path where the files and directories are to be extracted
841 // $p_remove_path : First part ('root' part) of the memorized path
842 // (if any similar) to remove while extracting.
845 // PCLZIP_OPT_ADD_PATH :
846 // PCLZIP_OPT_REMOVE_PATH :
847 // PCLZIP_OPT_REMOVE_ALL_PATH :
848 // PCLZIP_OPT_EXTRACT_AS_STRING : The files are extracted as strings and
850 // The resulting content is in a new field 'content' in the file
852 // This option must be used alone (any other options are ignored).
853 // PCLZIP_CB_PRE_EXTRACT :
854 // PCLZIP_CB_POST_EXTRACT :
857 // The list of the extracted files, with a status of the action.
858 // (see PclZip::listContent() for list entry format)
859 // --------------------------------------------------------------------------------
860 //function extractByIndex($p_index, options...)
861 function extractByIndex($p_index)
865 // ----- Reset the error handler
866 $this->privErrorReset();
868 // ----- Check archive
869 if (!$this->privCheckFormat()) {
873 // ----- Set default values
874 $v_options = array();
878 $v_remove_all_path = false;
880 // ----- Look for variable options arguments
881 $v_size = func_num_args();
883 // ----- Default values for option
884 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
886 // ----- Look for arguments
888 // ----- Get the arguments
889 $v_arg_list = func_get_args();
891 // ----- Remove form the options list the first argument
892 array_shift($v_arg_list);
895 // ----- Look for first arg
896 if ((is_integer($v_arg_list[0])) && ($v_arg_list[0] > 77000)) {
898 // ----- Parse the options
899 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
900 array (PCLZIP_OPT_PATH => 'optional',
901 PCLZIP_OPT_REMOVE_PATH => 'optional',
902 PCLZIP_OPT_REMOVE_ALL_PATH => 'optional',
903 PCLZIP_OPT_EXTRACT_AS_STRING => 'optional',
904 PCLZIP_OPT_ADD_PATH => 'optional',
905 PCLZIP_CB_PRE_EXTRACT => 'optional',
906 PCLZIP_CB_POST_EXTRACT => 'optional',
907 PCLZIP_OPT_SET_CHMOD => 'optional',
908 PCLZIP_OPT_REPLACE_NEWER => 'optional'
909 ,PCLZIP_OPT_STOP_ON_ERROR => 'optional'
910 ,PCLZIP_OPT_EXTRACT_DIR_RESTRICTION => 'optional',
911 PCLZIP_OPT_TEMP_FILE_THRESHOLD => 'optional',
912 PCLZIP_OPT_TEMP_FILE_ON => 'optional',
913 PCLZIP_OPT_TEMP_FILE_OFF => 'optional'
915 if ($v_result != 1) {
919 // ----- Set the arguments
920 if (isset($v_options[PCLZIP_OPT_PATH])) {
921 $v_path = $v_options[PCLZIP_OPT_PATH];
923 if (isset($v_options[PCLZIP_OPT_REMOVE_PATH])) {
924 $v_remove_path = $v_options[PCLZIP_OPT_REMOVE_PATH];
926 if (isset($v_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
927 $v_remove_all_path = $v_options[PCLZIP_OPT_REMOVE_ALL_PATH];
929 if (isset($v_options[PCLZIP_OPT_ADD_PATH])) {
930 // ----- Check for '/' in last path char
931 if ((strlen($v_path) > 0) && (substr($v_path, -1) != '/')) {
934 $v_path .= $v_options[PCLZIP_OPT_ADD_PATH];
936 if (!isset($v_options[PCLZIP_OPT_EXTRACT_AS_STRING])) {
937 $v_options[PCLZIP_OPT_EXTRACT_AS_STRING] = FALSE;
943 // ----- Look for 2 args
944 // Here we need to support the first historic synopsis of the
948 // ----- Get the first argument
949 $v_path = $v_arg_list[0];
951 // ----- Look for the optional second argument
953 $v_remove_path = $v_arg_list[1];
955 else if ($v_size > 2) {
957 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid number / type of arguments");
968 // Here I want to reuse extractByRule(), so I need to parse the $p_index
969 // with privParseOptions()
970 $v_arg_trick = array (PCLZIP_OPT_BY_INDEX, $p_index);
971 $v_options_trick = array();
972 $v_result = $this->privParseOptions($v_arg_trick, sizeof($v_arg_trick), $v_options_trick,
973 array (PCLZIP_OPT_BY_INDEX => 'optional' ));
974 if ($v_result != 1) {
977 $v_options[PCLZIP_OPT_BY_INDEX] = $v_options_trick[PCLZIP_OPT_BY_INDEX];
979 // ----- Look for default option values
980 $this->privOptionDefaultThreshold($v_options);
982 // ----- Call the extracting fct
983 if (($v_result = $this->privExtractByRule($p_list, $v_path, $v_remove_path, $v_remove_all_path, $v_options)) < 1) {
990 // --------------------------------------------------------------------------------
992 // --------------------------------------------------------------------------------
994 // delete([$p_option, $p_option_value, ...])
996 // This method removes files from the archive.
997 // If no parameters are given, then all the archive is emptied.
999 // None or optional arguments.
1001 // PCLZIP_OPT_BY_INDEX :
1002 // PCLZIP_OPT_BY_NAME :
1003 // PCLZIP_OPT_BY_EREG :
1004 // PCLZIP_OPT_BY_PREG :
1007 // The list of the files which are still present in the archive.
1008 // (see PclZip::listContent() for list entry format)
1009 // --------------------------------------------------------------------------------
1014 // ----- Reset the error handler
1015 $this->privErrorReset();
1017 // ----- Check archive
1018 if (!$this->privCheckFormat()) {
1022 // ----- Set default values
1023 $v_options = array();
1025 // ----- Look for variable options arguments
1026 $v_size = func_num_args();
1028 // ----- Look for arguments
1030 // ----- Get the arguments
1031 $v_arg_list = func_get_args();
1033 // ----- Parse the options
1034 $v_result = $this->privParseOptions($v_arg_list, $v_size, $v_options,
1035 array (PCLZIP_OPT_BY_NAME => 'optional',
1036 PCLZIP_OPT_BY_EREG => 'optional',
1037 PCLZIP_OPT_BY_PREG => 'optional',
1038 PCLZIP_OPT_BY_INDEX => 'optional' ));
1039 if ($v_result != 1) {
1044 // ----- Magic quotes trick
1045 $this->privDisableMagicQuotes();
1047 // ----- Call the delete fct
1049 if (($v_result = $this->privDeleteByRule($v_list, $v_options)) != 1) {
1050 $this->privSwapBackMagicQuotes();
1055 // ----- Magic quotes trick
1056 $this->privSwapBackMagicQuotes();
1061 // --------------------------------------------------------------------------------
1063 // --------------------------------------------------------------------------------
1064 // Function : deleteByIndex()
1066 // ***** Deprecated *****
1067 // delete(PCLZIP_OPT_BY_INDEX, $p_index) should be prefered.
1068 // --------------------------------------------------------------------------------
1069 function deleteByIndex($p_index)
1072 $p_list = $this->delete(PCLZIP_OPT_BY_INDEX, $p_index);
1077 // --------------------------------------------------------------------------------
1079 // --------------------------------------------------------------------------------
1080 // Function : properties()
1082 // This method gives the properties of the archive.
1083 // The properties are :
1084 // nb : Number of files in the archive
1085 // comment : Comment associated with the archive file
1086 // status : not_exist, ok
1091 // An array with the archive properties.
1092 // --------------------------------------------------------------------------------
1093 function properties()
1096 // ----- Reset the error handler
1097 $this->privErrorReset();
1099 // ----- Magic quotes trick
1100 $this->privDisableMagicQuotes();
1102 // ----- Check archive
1103 if (!$this->privCheckFormat()) {
1104 $this->privSwapBackMagicQuotes();
1108 // ----- Default properties
1110 $v_prop['comment'] = '';
1112 $v_prop['status'] = 'not_exist';
1114 // ----- Look if file exists
1115 if (@is_file($this->zipname))
1117 // ----- Open the zip file
1118 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
1120 $this->privSwapBackMagicQuotes();
1123 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
1129 // ----- Read the central directory informations
1130 $v_central_dir = array();
1131 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
1133 $this->privSwapBackMagicQuotes();
1137 // ----- Close the zip file
1138 $this->privCloseFd();
1140 // ----- Set the user attributes
1141 $v_prop['comment'] = $v_central_dir['comment'];
1142 $v_prop['nb'] = $v_central_dir['entries'];
1143 $v_prop['status'] = 'ok';
1146 // ----- Magic quotes trick
1147 $this->privSwapBackMagicQuotes();
1152 // --------------------------------------------------------------------------------
1154 // --------------------------------------------------------------------------------
1155 // Function : duplicate()
1157 // This method creates an archive by copying the content of an other one. If
1158 // the archive already exist, it is replaced by the new one without any warning.
1160 // $p_archive : The filename of a valid archive, or
1161 // a valid PclZip object.
1164 // 0 or a negative value on error (error code).
1165 // --------------------------------------------------------------------------------
1166 function duplicate($p_archive)
1170 // ----- Reset the error handler
1171 $this->privErrorReset();
1173 // ----- Look if the $p_archive is a PclZip object
1174 if ((is_object($p_archive)) && (get_class($p_archive) == 'pclzip'))
1177 // ----- Duplicate the archive
1178 $v_result = $this->privDuplicate($p_archive->zipname);
1181 // ----- Look if the $p_archive is a string (so a filename)
1182 else if (is_string($p_archive))
1185 // ----- Check that $p_archive is a valid zip file
1186 // TBC : Should also check the archive format
1187 if (!is_file($p_archive)) {
1189 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "No file with filename '".$p_archive."'");
1190 $v_result = PCLZIP_ERR_MISSING_FILE;
1193 // ----- Duplicate the archive
1194 $v_result = $this->privDuplicate($p_archive);
1198 // ----- Invalid variable
1202 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1203 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1209 // --------------------------------------------------------------------------------
1211 // --------------------------------------------------------------------------------
1212 // Function : merge()
1214 // This method merge the $p_archive_to_add archive at the end of the current
1216 // If the archive ($this) does not exist, the merge becomes a duplicate.
1217 // If the $p_archive_to_add archive does not exist, the merge is a success.
1219 // $p_archive_to_add : It can be directly the filename of a valid zip archive,
1220 // or a PclZip object archive.
1223 // 0 or negative values on error (see below).
1224 // --------------------------------------------------------------------------------
1225 function merge($p_archive_to_add)
1229 // ----- Reset the error handler
1230 $this->privErrorReset();
1232 // ----- Check archive
1233 if (!$this->privCheckFormat()) {
1237 // ----- Look if the $p_archive_to_add is a PclZip object
1238 if ((is_object($p_archive_to_add)) && (get_class($p_archive_to_add) == 'pclzip'))
1241 // ----- Merge the archive
1242 $v_result = $this->privMerge($p_archive_to_add);
1245 // ----- Look if the $p_archive_to_add is a string (so a filename)
1246 else if (is_string($p_archive_to_add))
1249 // ----- Create a temporary archive
1250 $v_object_archive = new PclZip($p_archive_to_add);
1252 // ----- Merge the archive
1253 $v_result = $this->privMerge($v_object_archive);
1256 // ----- Invalid variable
1260 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid variable type p_archive_to_add");
1261 $v_result = PCLZIP_ERR_INVALID_PARAMETER;
1267 // --------------------------------------------------------------------------------
1271 // --------------------------------------------------------------------------------
1272 // Function : errorCode()
1275 // --------------------------------------------------------------------------------
1276 function errorCode()
1278 if (PCLZIP_ERROR_EXTERNAL == 1) {
1279 return(PclErrorCode());
1282 return($this->error_code);
1285 // --------------------------------------------------------------------------------
1287 // --------------------------------------------------------------------------------
1288 // Function : errorName()
1291 // --------------------------------------------------------------------------------
1292 function errorName($p_with_code=false)
1294 $v_name = array ( PCLZIP_ERR_NO_ERROR => 'PCLZIP_ERR_NO_ERROR',
1295 PCLZIP_ERR_WRITE_OPEN_FAIL => 'PCLZIP_ERR_WRITE_OPEN_FAIL',
1296 PCLZIP_ERR_READ_OPEN_FAIL => 'PCLZIP_ERR_READ_OPEN_FAIL',
1297 PCLZIP_ERR_INVALID_PARAMETER => 'PCLZIP_ERR_INVALID_PARAMETER',
1298 PCLZIP_ERR_MISSING_FILE => 'PCLZIP_ERR_MISSING_FILE',
1299 PCLZIP_ERR_FILENAME_TOO_LONG => 'PCLZIP_ERR_FILENAME_TOO_LONG',
1300 PCLZIP_ERR_INVALID_ZIP => 'PCLZIP_ERR_INVALID_ZIP',
1301 PCLZIP_ERR_BAD_EXTRACTED_FILE => 'PCLZIP_ERR_BAD_EXTRACTED_FILE',
1302 PCLZIP_ERR_DIR_CREATE_FAIL => 'PCLZIP_ERR_DIR_CREATE_FAIL',
1303 PCLZIP_ERR_BAD_EXTENSION => 'PCLZIP_ERR_BAD_EXTENSION',
1304 PCLZIP_ERR_BAD_FORMAT => 'PCLZIP_ERR_BAD_FORMAT',
1305 PCLZIP_ERR_DELETE_FILE_FAIL => 'PCLZIP_ERR_DELETE_FILE_FAIL',
1306 PCLZIP_ERR_RENAME_FILE_FAIL => 'PCLZIP_ERR_RENAME_FILE_FAIL',
1307 PCLZIP_ERR_BAD_CHECKSUM => 'PCLZIP_ERR_BAD_CHECKSUM',
1308 PCLZIP_ERR_INVALID_ARCHIVE_ZIP => 'PCLZIP_ERR_INVALID_ARCHIVE_ZIP',
1309 PCLZIP_ERR_MISSING_OPTION_VALUE => 'PCLZIP_ERR_MISSING_OPTION_VALUE',
1310 PCLZIP_ERR_INVALID_OPTION_VALUE => 'PCLZIP_ERR_INVALID_OPTION_VALUE',
1311 PCLZIP_ERR_UNSUPPORTED_COMPRESSION => 'PCLZIP_ERR_UNSUPPORTED_COMPRESSION',
1312 PCLZIP_ERR_UNSUPPORTED_ENCRYPTION => 'PCLZIP_ERR_UNSUPPORTED_ENCRYPTION'
1313 ,PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE => 'PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE'
1314 ,PCLZIP_ERR_DIRECTORY_RESTRICTION => 'PCLZIP_ERR_DIRECTORY_RESTRICTION'
1317 if (isset($v_name[$this->error_code])) {
1318 $v_value = $v_name[$this->error_code];
1321 $v_value = 'NoName';
1325 return($v_value.' ('.$this->error_code.')');
1331 // --------------------------------------------------------------------------------
1333 // --------------------------------------------------------------------------------
1334 // Function : errorInfo()
1337 // --------------------------------------------------------------------------------
1338 function errorInfo($p_full=false)
1340 if (PCLZIP_ERROR_EXTERNAL == 1) {
1341 return(PclErrorString());
1345 return($this->errorName(true)." : ".$this->error_string);
1348 return($this->error_string." [code ".$this->error_code."]");
1352 // --------------------------------------------------------------------------------
1355 // --------------------------------------------------------------------------------
1356 // ***** UNDER THIS LINE ARE DEFINED PRIVATE INTERNAL FUNCTIONS *****
1358 // ***** THESES FUNCTIONS MUST NOT BE USED DIRECTLY *****
1359 // --------------------------------------------------------------------------------
1363 // --------------------------------------------------------------------------------
1364 // Function : privCheckFormat()
1366 // This method check that the archive exists and is a valid zip archive.
1367 // Several level of check exists. (futur)
1369 // $p_level : Level of check. Default 0.
1370 // 0 : Check the first bytes (magic codes) (default value))
1371 // 1 : 0 + Check the central directory (futur)
1372 // 2 : 1 + Check each file header (futur)
1375 // false on error, the error code is set.
1376 // --------------------------------------------------------------------------------
1377 function privCheckFormat($p_level=0)
1381 // ----- Reset the file system cache
1384 // ----- Reset the error handler
1385 $this->privErrorReset();
1387 // ----- Look if the file exits
1388 if (!is_file($this->zipname)) {
1390 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "Missing archive file '".$this->zipname."'");
1394 // ----- Check that the file is readeable
1395 if (!is_readable($this->zipname)) {
1397 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to read archive '".$this->zipname."'");
1401 // ----- Check the magic code
1404 // ----- Check the central header
1407 // ----- Check each file header
1413 // --------------------------------------------------------------------------------
1415 // --------------------------------------------------------------------------------
1416 // Function : privParseOptions()
1418 // This internal methods reads the variable list of arguments ($p_options_list,
1419 // $p_size) and generate an array with the options and values ($v_result_list).
1420 // $v_requested_options contains the options that can be present and those that
1422 // $v_requested_options is an array, with the option value as key, and 'optional',
1423 // or 'mandatory' as value.
1429 // --------------------------------------------------------------------------------
1430 function privParseOptions(&$p_options_list, $p_size, &$v_result_list, $v_requested_options=false)
1434 // ----- Read the options
1436 while ($i<$p_size) {
1438 // ----- Check if the option is supported
1439 if (!isset($v_requested_options[$p_options_list[$i]])) {
1441 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid optional parameter '".$p_options_list[$i]."' for this method");
1444 return PclZip::errorCode();
1447 // ----- Look for next option
1448 switch ($p_options_list[$i]) {
1449 // ----- Look for options that request a path value
1450 case PCLZIP_OPT_PATH :
1451 case PCLZIP_OPT_REMOVE_PATH :
1452 case PCLZIP_OPT_ADD_PATH :
1453 // ----- Check the number of parameters
1454 if (($i+1) >= $p_size) {
1456 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1459 return PclZip::errorCode();
1462 // ----- Get the value
1463 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1467 case PCLZIP_OPT_TEMP_FILE_THRESHOLD :
1468 // ----- Check the number of parameters
1469 if (($i+1) >= $p_size) {
1470 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1471 return PclZip::errorCode();
1474 // ----- Check for incompatible options
1475 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1476 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1477 return PclZip::errorCode();
1480 // ----- Check the value
1481 $v_value = $p_options_list[$i+1];
1482 if ((!is_integer($v_value)) || ($v_value<0)) {
1483 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Integer expected for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1484 return PclZip::errorCode();
1487 // ----- Get the value (and convert it in bytes)
1488 $v_result_list[$p_options_list[$i]] = $v_value*1048576;
1492 case PCLZIP_OPT_TEMP_FILE_ON :
1493 // ----- Check for incompatible options
1494 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_OFF])) {
1495 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_OFF'");
1496 return PclZip::errorCode();
1499 $v_result_list[$p_options_list[$i]] = true;
1502 case PCLZIP_OPT_TEMP_FILE_OFF :
1503 // ----- Check for incompatible options
1504 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_ON])) {
1505 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_ON'");
1506 return PclZip::errorCode();
1508 // ----- Check for incompatible options
1509 if (isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1510 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Option '".PclZipUtilOptionText($p_options_list[$i])."' can not be used with option 'PCLZIP_OPT_TEMP_FILE_THRESHOLD'");
1511 return PclZip::errorCode();
1514 $v_result_list[$p_options_list[$i]] = true;
1517 case PCLZIP_OPT_EXTRACT_DIR_RESTRICTION :
1518 // ----- Check the number of parameters
1519 if (($i+1) >= $p_size) {
1521 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1524 return PclZip::errorCode();
1527 // ----- Get the value
1528 if ( is_string($p_options_list[$i+1])
1529 && ($p_options_list[$i+1] != '')) {
1530 $v_result_list[$p_options_list[$i]] = PclZipUtilTranslateWinPath($p_options_list[$i+1], FALSE);
1537 // ----- Look for options that request an array of string for value
1538 case PCLZIP_OPT_BY_NAME :
1539 // ----- Check the number of parameters
1540 if (($i+1) >= $p_size) {
1542 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1545 return PclZip::errorCode();
1548 // ----- Get the value
1549 if (is_string($p_options_list[$i+1])) {
1550 $v_result_list[$p_options_list[$i]][0] = $p_options_list[$i+1];
1552 else if (is_array($p_options_list[$i+1])) {
1553 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1557 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1560 return PclZip::errorCode();
1565 // ----- Look for options that request an EREG or PREG expression
1566 case PCLZIP_OPT_BY_EREG :
1567 // ereg() is deprecated starting with PHP 5.3. Move PCLZIP_OPT_BY_EREG
1568 // to PCLZIP_OPT_BY_PREG
1569 $p_options_list[$i] = PCLZIP_OPT_BY_PREG;
1570 case PCLZIP_OPT_BY_PREG :
1571 //case PCLZIP_OPT_CRYPT :
1572 // ----- Check the number of parameters
1573 if (($i+1) >= $p_size) {
1575 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1578 return PclZip::errorCode();
1581 // ----- Get the value
1582 if (is_string($p_options_list[$i+1])) {
1583 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1587 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Wrong parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1590 return PclZip::errorCode();
1595 // ----- Look for options that takes a string
1596 case PCLZIP_OPT_COMMENT :
1597 case PCLZIP_OPT_ADD_COMMENT :
1598 case PCLZIP_OPT_PREPEND_COMMENT :
1599 // ----- Check the number of parameters
1600 if (($i+1) >= $p_size) {
1602 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE,
1603 "Missing parameter value for option '"
1604 .PclZipUtilOptionText($p_options_list[$i])
1608 return PclZip::errorCode();
1611 // ----- Get the value
1612 if (is_string($p_options_list[$i+1])) {
1613 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1617 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE,
1618 "Wrong parameter value for option '"
1619 .PclZipUtilOptionText($p_options_list[$i])
1623 return PclZip::errorCode();
1628 // ----- Look for options that request an array of index
1629 case PCLZIP_OPT_BY_INDEX :
1630 // ----- Check the number of parameters
1631 if (($i+1) >= $p_size) {
1633 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1636 return PclZip::errorCode();
1639 // ----- Get the value
1640 $v_work_list = array();
1641 if (is_string($p_options_list[$i+1])) {
1643 // ----- Remove spaces
1644 $p_options_list[$i+1] = strtr($p_options_list[$i+1], ' ', '');
1646 // ----- Parse items
1647 $v_work_list = explode(",", $p_options_list[$i+1]);
1649 else if (is_integer($p_options_list[$i+1])) {
1650 $v_work_list[0] = $p_options_list[$i+1].'-'.$p_options_list[$i+1];
1652 else if (is_array($p_options_list[$i+1])) {
1653 $v_work_list = $p_options_list[$i+1];
1657 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Value must be integer, string or array for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1660 return PclZip::errorCode();
1663 // ----- Reduce the index list
1664 // each index item in the list must be a couple with a start and
1665 // an end value : [0,3], [5-5], [8-10], ...
1666 // ----- Check the format of each item
1669 for ($j=0; $j<sizeof($v_work_list); $j++) {
1670 // ----- Explode the item
1671 $v_item_list = explode("-", $v_work_list[$j]);
1672 $v_size_item_list = sizeof($v_item_list);
1674 // ----- TBC : Here we might check that each item is a
1677 // ----- Look for single value
1678 if ($v_size_item_list == 1) {
1679 // ----- Set the option value
1680 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1681 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[0];
1683 elseif ($v_size_item_list == 2) {
1684 // ----- Set the option value
1685 $v_result_list[$p_options_list[$i]][$j]['start'] = $v_item_list[0];
1686 $v_result_list[$p_options_list[$i]][$j]['end'] = $v_item_list[1];
1690 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Too many values in index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1693 return PclZip::errorCode();
1697 // ----- Look for list sort
1698 if ($v_result_list[$p_options_list[$i]][$j]['start'] < $v_sort_value) {
1701 // ----- TBC : An automatic sort should be writen ...
1703 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Invalid order of index range for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1706 return PclZip::errorCode();
1708 $v_sort_value = $v_result_list[$p_options_list[$i]][$j]['start'];
1711 // ----- Sort the items
1713 // TBC : To Be Completed
1716 // ----- Next option
1720 // ----- Look for options that request no value
1721 case PCLZIP_OPT_REMOVE_ALL_PATH :
1722 case PCLZIP_OPT_EXTRACT_AS_STRING :
1723 case PCLZIP_OPT_NO_COMPRESSION :
1724 case PCLZIP_OPT_EXTRACT_IN_OUTPUT :
1725 case PCLZIP_OPT_REPLACE_NEWER :
1726 case PCLZIP_OPT_STOP_ON_ERROR :
1727 $v_result_list[$p_options_list[$i]] = true;
1730 // ----- Look for options that request an octal value
1731 case PCLZIP_OPT_SET_CHMOD :
1732 // ----- Check the number of parameters
1733 if (($i+1) >= $p_size) {
1735 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1738 return PclZip::errorCode();
1741 // ----- Get the value
1742 $v_result_list[$p_options_list[$i]] = $p_options_list[$i+1];
1746 // ----- Look for options that request a call-back
1747 case PCLZIP_CB_PRE_EXTRACT :
1748 case PCLZIP_CB_POST_EXTRACT :
1749 case PCLZIP_CB_PRE_ADD :
1750 case PCLZIP_CB_POST_ADD :
1752 case PCLZIP_CB_PRE_DELETE :
1753 case PCLZIP_CB_POST_DELETE :
1754 case PCLZIP_CB_PRE_LIST :
1755 case PCLZIP_CB_POST_LIST :
1757 // ----- Check the number of parameters
1758 if (($i+1) >= $p_size) {
1760 PclZip::privErrorLog(PCLZIP_ERR_MISSING_OPTION_VALUE, "Missing parameter value for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1763 return PclZip::errorCode();
1766 // ----- Get the value
1767 $v_function_name = $p_options_list[$i+1];
1769 // ----- Check that the value is a valid existing function
1770 if (!function_exists($v_function_name)) {
1772 PclZip::privErrorLog(PCLZIP_ERR_INVALID_OPTION_VALUE, "Function '".$v_function_name."()' is not an existing function for option '".PclZipUtilOptionText($p_options_list[$i])."'");
1775 return PclZip::errorCode();
1778 // ----- Set the attribute
1779 $v_result_list[$p_options_list[$i]] = $v_function_name;
1785 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1786 "Unknown parameter '"
1787 .$p_options_list[$i]."'");
1790 return PclZip::errorCode();
1793 // ----- Next options
1797 // ----- Look for mandatory options
1798 if ($v_requested_options !== false) {
1799 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1800 // ----- Look for mandatory option
1801 if ($v_requested_options[$key] == 'mandatory') {
1802 // ----- Look if present
1803 if (!isset($v_result_list[$key])) {
1805 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1808 return PclZip::errorCode();
1814 // ----- Look for default values
1815 if (!isset($v_result_list[PCLZIP_OPT_TEMP_FILE_THRESHOLD])) {
1822 // --------------------------------------------------------------------------------
1824 // --------------------------------------------------------------------------------
1825 // Function : privOptionDefaultThreshold()
1829 // --------------------------------------------------------------------------------
1830 function privOptionDefaultThreshold(&$p_options)
1834 if (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
1835 || isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF])) {
1839 // ----- Get 'memory_limit' configuration value
1840 $v_memory_limit = ini_get('memory_limit');
1841 $v_memory_limit = trim($v_memory_limit);
1842 $last = strtolower(substr($v_memory_limit, -1));
1845 //$v_memory_limit = $v_memory_limit*1024*1024*1024;
1846 $v_memory_limit = $v_memory_limit*1073741824;
1848 //$v_memory_limit = $v_memory_limit*1024*1024;
1849 $v_memory_limit = $v_memory_limit*1048576;
1851 $v_memory_limit = $v_memory_limit*1024;
1853 $p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] = floor($v_memory_limit*PCLZIP_TEMPORARY_FILE_RATIO);
1856 // ----- Sanity check : No threshold if value lower than 1M
1857 if ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] < 1048576) {
1858 unset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD]);
1864 // --------------------------------------------------------------------------------
1866 // --------------------------------------------------------------------------------
1867 // Function : privFileDescrParseAtt()
1873 // --------------------------------------------------------------------------------
1874 function privFileDescrParseAtt(&$p_file_list, &$p_filedescr, $v_options, $v_requested_options=false)
1878 // ----- For each file in the list check the attributes
1879 foreach ($p_file_list as $v_key => $v_value) {
1881 // ----- Check if the option is supported
1882 if (!isset($v_requested_options[$v_key])) {
1884 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file attribute '".$v_key."' for this file");
1887 return PclZip::errorCode();
1890 // ----- Look for attribute
1892 case PCLZIP_ATT_FILE_NAME :
1893 if (!is_string($v_value)) {
1894 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1895 return PclZip::errorCode();
1898 $p_filedescr['filename'] = PclZipUtilPathReduction($v_value);
1900 if ($p_filedescr['filename'] == '') {
1901 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty filename for attribute '".PclZipUtilOptionText($v_key)."'");
1902 return PclZip::errorCode();
1907 case PCLZIP_ATT_FILE_NEW_SHORT_NAME :
1908 if (!is_string($v_value)) {
1909 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1910 return PclZip::errorCode();
1913 $p_filedescr['new_short_name'] = PclZipUtilPathReduction($v_value);
1915 if ($p_filedescr['new_short_name'] == '') {
1916 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty short filename for attribute '".PclZipUtilOptionText($v_key)."'");
1917 return PclZip::errorCode();
1921 case PCLZIP_ATT_FILE_NEW_FULL_NAME :
1922 if (!is_string($v_value)) {
1923 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1924 return PclZip::errorCode();
1927 $p_filedescr['new_full_name'] = PclZipUtilPathReduction($v_value);
1929 if ($p_filedescr['new_full_name'] == '') {
1930 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid empty full filename for attribute '".PclZipUtilOptionText($v_key)."'");
1931 return PclZip::errorCode();
1935 // ----- Look for options that takes a string
1936 case PCLZIP_ATT_FILE_COMMENT :
1937 if (!is_string($v_value)) {
1938 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". String expected for attribute '".PclZipUtilOptionText($v_key)."'");
1939 return PclZip::errorCode();
1942 $p_filedescr['comment'] = $v_value;
1945 case PCLZIP_ATT_FILE_MTIME :
1946 if (!is_integer($v_value)) {
1947 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ATTRIBUTE_VALUE, "Invalid type ".gettype($v_value).". Integer expected for attribute '".PclZipUtilOptionText($v_key)."'");
1948 return PclZip::errorCode();
1951 $p_filedescr['mtime'] = $v_value;
1954 case PCLZIP_ATT_FILE_CONTENT :
1955 $p_filedescr['content'] = $v_value;
1960 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER,
1961 "Unknown parameter '".$v_key."'");
1964 return PclZip::errorCode();
1967 // ----- Look for mandatory options
1968 if ($v_requested_options !== false) {
1969 for ($key=reset($v_requested_options); $key=key($v_requested_options); $key=next($v_requested_options)) {
1970 // ----- Look for mandatory option
1971 if ($v_requested_options[$key] == 'mandatory') {
1972 // ----- Look if present
1973 if (!isset($p_file_list[$key])) {
1974 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Missing mandatory parameter ".PclZipUtilOptionText($key)."(".$key.")");
1975 return PclZip::errorCode();
1987 // --------------------------------------------------------------------------------
1989 // --------------------------------------------------------------------------------
1990 // Function : privFileDescrExpand()
1992 // This method look for each item of the list to see if its a file, a folder
1993 // or a string to be added as file. For any other type of files (link, other)
1994 // just ignore the item.
1995 // Then prepare the information that will be stored for that file.
1996 // When its a folder, expand the folder with all the files that are in that
1997 // folder (recursively).
2002 // --------------------------------------------------------------------------------
2003 function privFileDescrExpand(&$p_filedescr_list, &$p_options)
2007 // ----- Create a result list
2008 $v_result_list = array();
2010 // ----- Look each entry
2011 for ($i=0; $i<sizeof($p_filedescr_list); $i++) {
2013 // ----- Get filedescr
2014 $v_descr = $p_filedescr_list[$i];
2016 // ----- Reduce the filename
2017 $v_descr['filename'] = PclZipUtilTranslateWinPath($v_descr['filename'], false);
2018 $v_descr['filename'] = PclZipUtilPathReduction($v_descr['filename']);
2020 // ----- Look for real file or folder
2021 if (file_exists($v_descr['filename'])) {
2022 if (@is_file($v_descr['filename'])) {
2023 $v_descr['type'] = 'file';
2025 else if (@is_dir($v_descr['filename'])) {
2026 $v_descr['type'] = 'folder';
2028 else if (@is_link($v_descr['filename'])) {
2038 // ----- Look for string added as file
2039 else if (isset($v_descr['content'])) {
2040 $v_descr['type'] = 'virtual_file';
2043 // ----- Missing file
2046 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$v_descr['filename']."' does not exist");
2049 return PclZip::errorCode();
2052 // ----- Calculate the stored filename
2053 $this->privCalculateStoredFilename($v_descr, $p_options);
2055 // ----- Add the descriptor in result list
2056 $v_result_list[sizeof($v_result_list)] = $v_descr;
2058 // ----- Look for folder
2059 if ($v_descr['type'] == 'folder') {
2060 // ----- List of items in folder
2061 $v_dirlist_descr = array();
2063 if ($v_folder_handler = @opendir($v_descr['filename'])) {
2064 while (($v_item_handler = @readdir($v_folder_handler)) !== false) {
2066 // ----- Skip '.' and '..'
2067 if (($v_item_handler == '.') || ($v_item_handler == '..')) {
2071 // ----- Compose the full filename
2072 $v_dirlist_descr[$v_dirlist_nb]['filename'] = $v_descr['filename'].'/'.$v_item_handler;
2074 // ----- Look for different stored filename
2075 // Because the name of the folder was changed, the name of the
2076 // files/sub-folders also change
2077 if (($v_descr['stored_filename'] != $v_descr['filename'])
2078 && (!isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))) {
2079 if ($v_descr['stored_filename'] != '') {
2080 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_descr['stored_filename'].'/'.$v_item_handler;
2083 $v_dirlist_descr[$v_dirlist_nb]['new_full_name'] = $v_item_handler;
2090 @closedir($v_folder_handler);
2093 // TBC : unable to open folder in read mode
2096 // ----- Expand each element of the list
2097 if ($v_dirlist_nb != 0) {
2099 if (($v_result = $this->privFileDescrExpand($v_dirlist_descr, $p_options)) != 1) {
2103 // ----- Concat the resulting list
2104 $v_result_list = array_merge($v_result_list, $v_dirlist_descr);
2109 // ----- Free local array
2110 unset($v_dirlist_descr);
2114 // ----- Get the result list
2115 $p_filedescr_list = $v_result_list;
2120 // --------------------------------------------------------------------------------
2122 // --------------------------------------------------------------------------------
2123 // Function : privCreate()
2127 // --------------------------------------------------------------------------------
2128 function privCreate($p_filedescr_list, &$p_result_list, &$p_options)
2131 $v_list_detail = array();
2133 // ----- Magic quotes trick
2134 $this->privDisableMagicQuotes();
2136 // ----- Open the file in write mode
2137 if (($v_result = $this->privOpenFd('wb')) != 1)
2143 // ----- Add the list of files
2144 $v_result = $this->privAddList($p_filedescr_list, $p_result_list, $p_options);
2147 $this->privCloseFd();
2149 // ----- Magic quotes trick
2150 $this->privSwapBackMagicQuotes();
2155 // --------------------------------------------------------------------------------
2157 // --------------------------------------------------------------------------------
2158 // Function : privAdd()
2162 // --------------------------------------------------------------------------------
2163 function privAdd($p_filedescr_list, &$p_result_list, &$p_options)
2166 $v_list_detail = array();
2168 // ----- Look if the archive exists or is empty
2169 if ((!is_file($this->zipname)) || (filesize($this->zipname) == 0))
2172 // ----- Do a create
2173 $v_result = $this->privCreate($p_filedescr_list, $p_result_list, $p_options);
2178 // ----- Magic quotes trick
2179 $this->privDisableMagicQuotes();
2181 // ----- Open the zip file
2182 if (($v_result=$this->privOpenFd('rb')) != 1)
2184 // ----- Magic quotes trick
2185 $this->privSwapBackMagicQuotes();
2191 // ----- Read the central directory informations
2192 $v_central_dir = array();
2193 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
2195 $this->privCloseFd();
2196 $this->privSwapBackMagicQuotes();
2200 // ----- Go to beginning of File
2201 @rewind($this->zip_fd);
2203 // ----- Creates a temporay file
2204 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
2206 // ----- Open the temporary file in write mode
2207 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
2209 $this->privCloseFd();
2210 $this->privSwapBackMagicQuotes();
2212 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
2215 return PclZip::errorCode();
2218 // ----- Copy the files from the archive to the temporary file
2219 // TBC : Here I should better append the file and go back to erase the central dir
2220 $v_size = $v_central_dir['offset'];
2221 while ($v_size != 0)
2223 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2224 $v_buffer = fread($this->zip_fd, $v_read_size);
2225 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
2226 $v_size -= $v_read_size;
2229 // ----- Swap the file descriptor
2230 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
2231 // the following methods on the temporary fil and not the real archive
2232 $v_swap = $this->zip_fd;
2233 $this->zip_fd = $v_zip_temp_fd;
2234 $v_zip_temp_fd = $v_swap;
2236 // ----- Add the files
2237 $v_header_list = array();
2238 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2240 fclose($v_zip_temp_fd);
2241 $this->privCloseFd();
2242 @unlink($v_zip_temp_name);
2243 $this->privSwapBackMagicQuotes();
2249 // ----- Store the offset of the central dir
2250 $v_offset = @ftell($this->zip_fd);
2252 // ----- Copy the block of file headers from the old archive
2253 $v_size = $v_central_dir['size'];
2254 while ($v_size != 0)
2256 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2257 $v_buffer = @fread($v_zip_temp_fd, $v_read_size);
2258 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
2259 $v_size -= $v_read_size;
2262 // ----- Create the Central Dir files header
2263 for ($i=0, $v_count=0; $i<sizeof($v_header_list); $i++)
2265 // ----- Create the file header
2266 if ($v_header_list[$i]['status'] == 'ok') {
2267 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2268 fclose($v_zip_temp_fd);
2269 $this->privCloseFd();
2270 @unlink($v_zip_temp_name);
2271 $this->privSwapBackMagicQuotes();
2279 // ----- Transform the header to a 'usable' info
2280 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2283 // ----- Zip file comment
2284 $v_comment = $v_central_dir['comment'];
2285 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2286 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2288 if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
2289 $v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
2291 if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
2292 $v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
2295 // ----- Calculate the size of the central header
2296 $v_size = @ftell($this->zip_fd)-$v_offset;
2298 // ----- Create the central dir footer
2299 if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
2301 // ----- Reset the file list
2302 unset($v_header_list);
2303 $this->privSwapBackMagicQuotes();
2309 // ----- Swap back the file descriptor
2310 $v_swap = $this->zip_fd;
2311 $this->zip_fd = $v_zip_temp_fd;
2312 $v_zip_temp_fd = $v_swap;
2315 $this->privCloseFd();
2317 // ----- Close the temporary file
2318 @fclose($v_zip_temp_fd);
2320 // ----- Magic quotes trick
2321 $this->privSwapBackMagicQuotes();
2323 // ----- Delete the zip file
2324 // TBC : I should test the result ...
2325 @unlink($this->zipname);
2327 // ----- Rename the temporary file
2328 // TBC : I should test the result ...
2329 //@rename($v_zip_temp_name, $this->zipname);
2330 PclZipUtilRename($v_zip_temp_name, $this->zipname);
2335 // --------------------------------------------------------------------------------
2337 // --------------------------------------------------------------------------------
2338 // Function : privOpenFd()
2341 // --------------------------------------------------------------------------------
2342 function privOpenFd($p_mode)
2346 // ----- Look if already open
2347 if ($this->zip_fd != 0)
2350 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
2353 return PclZip::errorCode();
2356 // ----- Open the zip file
2357 if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
2360 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
2363 return PclZip::errorCode();
2369 // --------------------------------------------------------------------------------
2371 // --------------------------------------------------------------------------------
2372 // Function : privCloseFd()
2375 // --------------------------------------------------------------------------------
2376 function privCloseFd()
2380 if ($this->zip_fd != 0)
2381 @fclose($this->zip_fd);
2387 // --------------------------------------------------------------------------------
2389 // --------------------------------------------------------------------------------
2390 // Function : privAddList()
2392 // $p_add_dir and $p_remove_dir will give the ability to memorize a path which is
2393 // different from the real path of the file. This is usefull if you want to have PclTar
2394 // running in any directory, and memorize relative path from an other directory.
2396 // $p_list : An array containing the file or directory names to add in the tar
2397 // $p_result_list : list of added files with their properties (specially the status field)
2398 // $p_add_dir : Path to add in the filename path archived
2399 // $p_remove_dir : Path to remove in the filename path archived
2401 // --------------------------------------------------------------------------------
2402 // function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
2403 function privAddList($p_filedescr_list, &$p_result_list, &$p_options)
2407 // ----- Add the files
2408 $v_header_list = array();
2409 if (($v_result = $this->privAddFileList($p_filedescr_list, $v_header_list, $p_options)) != 1)
2415 // ----- Store the offset of the central dir
2416 $v_offset = @ftell($this->zip_fd);
2418 // ----- Create the Central Dir files header
2419 for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
2421 // ----- Create the file header
2422 if ($v_header_list[$i]['status'] == 'ok') {
2423 if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
2430 // ----- Transform the header to a 'usable' info
2431 $this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
2434 // ----- Zip file comment
2436 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
2437 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
2440 // ----- Calculate the size of the central header
2441 $v_size = @ftell($this->zip_fd)-$v_offset;
2443 // ----- Create the central dir footer
2444 if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
2446 // ----- Reset the file list
2447 unset($v_header_list);
2456 // --------------------------------------------------------------------------------
2458 // --------------------------------------------------------------------------------
2459 // Function : privAddFileList()
2462 // $p_filedescr_list : An array containing the file description
2463 // or directory names to add in the zip
2464 // $p_result_list : list of added files with their properties (specially the status field)
2466 // --------------------------------------------------------------------------------
2467 function privAddFileList($p_filedescr_list, &$p_result_list, &$p_options)
2470 $v_header = array();
2472 // ----- Recuperate the current number of elt in list
2473 $v_nb = sizeof($p_result_list);
2475 // ----- Loop on the files
2476 for ($j=0; ($j<sizeof($p_filedescr_list)) && ($v_result==1); $j++) {
2477 // ----- Format the filename
2478 $p_filedescr_list[$j]['filename']
2479 = PclZipUtilTranslateWinPath($p_filedescr_list[$j]['filename'], false);
2482 // ----- Skip empty file names
2483 // TBC : Can this be possible ? not checked in DescrParseAtt ?
2484 if ($p_filedescr_list[$j]['filename'] == "") {
2488 // ----- Check the filename
2489 if ( ($p_filedescr_list[$j]['type'] != 'virtual_file')
2490 && (!file_exists($p_filedescr_list[$j]['filename']))) {
2491 PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '".$p_filedescr_list[$j]['filename']."' does not exist");
2492 return PclZip::errorCode();
2495 // ----- Look if it is a file or a dir with no all path remove option
2496 // or a dir with all its path removed
2497 // if ( (is_file($p_filedescr_list[$j]['filename']))
2498 // || ( is_dir($p_filedescr_list[$j]['filename'])
2499 if ( ($p_filedescr_list[$j]['type'] == 'file')
2500 || ($p_filedescr_list[$j]['type'] == 'virtual_file')
2501 || ( ($p_filedescr_list[$j]['type'] == 'folder')
2502 && ( !isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])
2503 || !$p_options[PCLZIP_OPT_REMOVE_ALL_PATH]))
2506 // ----- Add the file
2507 $v_result = $this->privAddFile($p_filedescr_list[$j], $v_header,
2509 if ($v_result != 1) {
2513 // ----- Store the file infos
2514 $p_result_list[$v_nb++] = $v_header;
2521 // --------------------------------------------------------------------------------
2523 // --------------------------------------------------------------------------------
2524 // Function : privAddFile()
2528 // --------------------------------------------------------------------------------
2529 function privAddFile($p_filedescr, &$p_header, &$p_options)
2533 // ----- Working variable
2534 $p_filename = $p_filedescr['filename'];
2536 // TBC : Already done in the fileAtt check ... ?
2537 if ($p_filename == "") {
2539 PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
2542 return PclZip::errorCode();
2545 // ----- Look for a stored different filename
2547 if (isset($p_filedescr['stored_filename'])) {
2548 $v_stored_filename = $p_filedescr['stored_filename'];
2551 $v_stored_filename = $p_filedescr['stored_filename'];
2555 // ----- Set the file properties
2557 $p_header['version'] = 20;
2558 $p_header['version_extracted'] = 10;
2559 $p_header['flag'] = 0;
2560 $p_header['compression'] = 0;
2561 $p_header['crc'] = 0;
2562 $p_header['compressed_size'] = 0;
2563 $p_header['filename_len'] = strlen($p_filename);
2564 $p_header['extra_len'] = 0;
2565 $p_header['disk'] = 0;
2566 $p_header['internal'] = 0;
2567 $p_header['offset'] = 0;
2568 $p_header['filename'] = $p_filename;
2569 // TBC : Removed $p_header['stored_filename'] = $v_stored_filename;
2570 $p_header['stored_filename'] = $p_filedescr['stored_filename'];
2571 $p_header['extra'] = '';
2572 $p_header['status'] = 'ok';
2573 $p_header['index'] = -1;
2575 // ----- Look for regular file
2576 if ($p_filedescr['type']=='file') {
2577 $p_header['external'] = 0x00000000;
2578 $p_header['size'] = filesize($p_filename);
2581 // ----- Look for regular folder
2582 else if ($p_filedescr['type']=='folder') {
2583 $p_header['external'] = 0x00000010;
2584 $p_header['mtime'] = filemtime($p_filename);
2585 $p_header['size'] = filesize($p_filename);
2588 // ----- Look for virtual file
2589 else if ($p_filedescr['type'] == 'virtual_file') {
2590 $p_header['external'] = 0x00000000;
2591 $p_header['size'] = strlen($p_filedescr['content']);
2595 // ----- Look for filetime
2596 if (isset($p_filedescr['mtime'])) {
2597 $p_header['mtime'] = $p_filedescr['mtime'];
2599 else if ($p_filedescr['type'] == 'virtual_file') {
2600 $p_header['mtime'] = time();
2603 $p_header['mtime'] = filemtime($p_filename);
2606 // ------ Look for file comment
2607 if (isset($p_filedescr['comment'])) {
2608 $p_header['comment_len'] = strlen($p_filedescr['comment']);
2609 $p_header['comment'] = $p_filedescr['comment'];
2612 $p_header['comment_len'] = 0;
2613 $p_header['comment'] = '';
2616 // ----- Look for pre-add callback
2617 if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
2619 // ----- Generate a local information
2620 $v_local_header = array();
2621 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2623 // ----- Call the callback
2624 // Here I do not use call_user_func() because I need to send a reference to the
2626 $v_result = $p_options[PCLZIP_CB_PRE_ADD](PCLZIP_CB_PRE_ADD, $v_local_header);
2627 if ($v_result == 0) {
2628 // ----- Change the file status
2629 $p_header['status'] = "skipped";
2633 // ----- Update the informations
2634 // Only some fields can be modified
2635 if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
2636 $p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
2640 // ----- Look for empty stored filename
2641 if ($p_header['stored_filename'] == "") {
2642 $p_header['status'] = "filtered";
2645 // ----- Check the path length
2646 if (strlen($p_header['stored_filename']) > 0xFF) {
2647 $p_header['status'] = 'filename_too_long';
2650 // ----- Look if no error, or file not skipped
2651 if ($p_header['status'] == 'ok') {
2653 // ----- Look for a file
2654 if ($p_filedescr['type'] == 'file') {
2655 // ----- Look for using temporary file to zip
2656 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
2657 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
2658 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
2659 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_header['size'])) ) ) {
2660 $v_result = $this->privAddFileUsingTempFile($p_filedescr, $p_header, $p_options);
2661 if ($v_result < PCLZIP_ERR_NO_ERROR) {
2666 // ----- Use "in memory" zip algo
2669 // ----- Open the source file
2670 if (($v_file = @fopen($p_filename, "rb")) == 0) {
2671 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2672 return PclZip::errorCode();
2675 // ----- Read the file content
2676 $v_content = @fread($v_file, $p_header['size']);
2678 // ----- Close the file
2681 // ----- Calculate the CRC
2682 $p_header['crc'] = @crc32($v_content);
2684 // ----- Look for no compression
2685 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2686 // ----- Set header parameters
2687 $p_header['compressed_size'] = $p_header['size'];
2688 $p_header['compression'] = 0;
2691 // ----- Look for normal compression
2693 // ----- Compress the content
2694 $v_content = @gzdeflate($v_content);
2696 // ----- Set header parameters
2697 $p_header['compressed_size'] = strlen($v_content);
2698 $p_header['compression'] = 8;
2701 // ----- Call the header generation
2702 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2707 // ----- Write the compressed (or not) content
2708 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2714 // ----- Look for a virtual file (a file from string)
2715 else if ($p_filedescr['type'] == 'virtual_file') {
2717 $v_content = $p_filedescr['content'];
2719 // ----- Calculate the CRC
2720 $p_header['crc'] = @crc32($v_content);
2722 // ----- Look for no compression
2723 if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
2724 // ----- Set header parameters
2725 $p_header['compressed_size'] = $p_header['size'];
2726 $p_header['compression'] = 0;
2729 // ----- Look for normal compression
2731 // ----- Compress the content
2732 $v_content = @gzdeflate($v_content);
2734 // ----- Set header parameters
2735 $p_header['compressed_size'] = strlen($v_content);
2736 $p_header['compression'] = 8;
2739 // ----- Call the header generation
2740 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2745 // ----- Write the compressed (or not) content
2746 @fwrite($this->zip_fd, $v_content, $p_header['compressed_size']);
2749 // ----- Look for a directory
2750 else if ($p_filedescr['type'] == 'folder') {
2751 // ----- Look for directory last '/'
2752 if (@substr($p_header['stored_filename'], -1) != '/') {
2753 $p_header['stored_filename'] .= '/';
2756 // ----- Set the file properties
2757 $p_header['size'] = 0;
2758 //$p_header['external'] = 0x41FF0010; // Value for a folder : to be checked
2759 $p_header['external'] = 0x00000010; // Value for a folder : to be checked
2761 // ----- Call the header generation
2762 if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
2769 // ----- Look for post-add callback
2770 if (isset($p_options[PCLZIP_CB_POST_ADD])) {
2772 // ----- Generate a local information
2773 $v_local_header = array();
2774 $this->privConvertHeader2FileInfo($p_header, $v_local_header);
2776 // ----- Call the callback
2777 // Here I do not use call_user_func() because I need to send a reference to the
2779 $v_result = $p_options[PCLZIP_CB_POST_ADD](PCLZIP_CB_POST_ADD, $v_local_header);
2780 if ($v_result == 0) {
2785 // ----- Update the informations
2786 // Nothing can be modified
2792 // --------------------------------------------------------------------------------
2794 // --------------------------------------------------------------------------------
2795 // Function : privAddFileUsingTempFile()
2799 // --------------------------------------------------------------------------------
2800 function privAddFileUsingTempFile($p_filedescr, &$p_header, &$p_options)
2802 $v_result=PCLZIP_ERR_NO_ERROR;
2804 // ----- Working variable
2805 $p_filename = $p_filedescr['filename'];
2808 // ----- Open the source file
2809 if (($v_file = @fopen($p_filename, "rb")) == 0) {
2810 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
2811 return PclZip::errorCode();
2814 // ----- Creates a compressed temporary file
2815 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
2816 if (($v_file_compressed = @gzopen($v_gzip_temp_name, "wb")) == 0) {
2818 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
2819 return PclZip::errorCode();
2822 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2823 $v_size = filesize($p_filename);
2824 while ($v_size != 0) {
2825 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2826 $v_buffer = @fread($v_file, $v_read_size);
2827 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
2828 @gzputs($v_file_compressed, $v_buffer, $v_read_size);
2829 $v_size -= $v_read_size;
2832 // ----- Close the file
2834 @gzclose($v_file_compressed);
2836 // ----- Check the minimum file size
2837 if (filesize($v_gzip_temp_name) < 18) {
2838 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'gzip temporary file \''.$v_gzip_temp_name.'\' has invalid filesize - should be minimum 18 bytes');
2839 return PclZip::errorCode();
2842 // ----- Extract the compressed attributes
2843 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0) {
2844 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
2845 return PclZip::errorCode();
2848 // ----- Read the gzip file header
2849 $v_binary_data = @fread($v_file_compressed, 10);
2850 $v_data_header = unpack('a1id1/a1id2/a1cm/a1flag/Vmtime/a1xfl/a1os', $v_binary_data);
2852 // ----- Check some parameters
2853 $v_data_header['os'] = bin2hex($v_data_header['os']);
2855 // ----- Read the gzip file footer
2856 @fseek($v_file_compressed, filesize($v_gzip_temp_name)-8);
2857 $v_binary_data = @fread($v_file_compressed, 8);
2858 $v_data_footer = unpack('Vcrc/Vcompressed_size', $v_binary_data);
2860 // ----- Set the attributes
2861 $p_header['compression'] = ord($v_data_header['cm']);
2862 //$p_header['mtime'] = $v_data_header['mtime'];
2863 $p_header['crc'] = $v_data_footer['crc'];
2864 $p_header['compressed_size'] = filesize($v_gzip_temp_name)-18;
2866 // ----- Close the file
2867 @fclose($v_file_compressed);
2869 // ----- Call the header generation
2870 if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
2874 // ----- Add the compressed data
2875 if (($v_file_compressed = @fopen($v_gzip_temp_name, "rb")) == 0)
2877 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
2878 return PclZip::errorCode();
2881 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
2882 fseek($v_file_compressed, 10);
2883 $v_size = $p_header['compressed_size'];
2884 while ($v_size != 0)
2886 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
2887 $v_buffer = @fread($v_file_compressed, $v_read_size);
2888 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
2889 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
2890 $v_size -= $v_read_size;
2893 // ----- Close the file
2894 @fclose($v_file_compressed);
2896 // ----- Unlink the temporary file
2897 @unlink($v_gzip_temp_name);
2902 // --------------------------------------------------------------------------------
2904 // --------------------------------------------------------------------------------
2905 // Function : privCalculateStoredFilename()
2907 // Based on file descriptor properties and global options, this method
2908 // calculate the filename that will be stored in the archive.
2911 // --------------------------------------------------------------------------------
2912 function privCalculateStoredFilename(&$p_filedescr, &$p_options)
2916 // ----- Working variables
2917 $p_filename = $p_filedescr['filename'];
2918 if (isset($p_options[PCLZIP_OPT_ADD_PATH])) {
2919 $p_add_dir = $p_options[PCLZIP_OPT_ADD_PATH];
2924 if (isset($p_options[PCLZIP_OPT_REMOVE_PATH])) {
2925 $p_remove_dir = $p_options[PCLZIP_OPT_REMOVE_PATH];
2930 if (isset($p_options[PCLZIP_OPT_REMOVE_ALL_PATH])) {
2931 $p_remove_all_dir = $p_options[PCLZIP_OPT_REMOVE_ALL_PATH];
2934 $p_remove_all_dir = 0;
2938 // ----- Look for full name change
2939 if (isset($p_filedescr['new_full_name'])) {
2940 // ----- Remove drive letter if any
2941 $v_stored_filename = PclZipUtilTranslateWinPath($p_filedescr['new_full_name']);
2944 // ----- Look for path and/or short name change
2947 // ----- Look for short name change
2948 // Its when we cahnge just the filename but not the path
2949 if (isset($p_filedescr['new_short_name'])) {
2950 $v_path_info = pathinfo($p_filename);
2952 if ($v_path_info['dirname'] != '') {
2953 $v_dir = $v_path_info['dirname'].'/';
2955 $v_stored_filename = $v_dir.$p_filedescr['new_short_name'];
2958 // ----- Calculate the stored filename
2959 $v_stored_filename = $p_filename;
2962 // ----- Look for all path to remove
2963 if ($p_remove_all_dir) {
2964 $v_stored_filename = basename($p_filename);
2966 // ----- Look for partial path remove
2967 else if ($p_remove_dir != "") {
2968 if (substr($p_remove_dir, -1) != '/')
2969 $p_remove_dir .= "/";
2971 if ( (substr($p_filename, 0, 2) == "./")
2972 || (substr($p_remove_dir, 0, 2) == "./")) {
2974 if ( (substr($p_filename, 0, 2) == "./")
2975 && (substr($p_remove_dir, 0, 2) != "./")) {
2976 $p_remove_dir = "./".$p_remove_dir;
2978 if ( (substr($p_filename, 0, 2) != "./")
2979 && (substr($p_remove_dir, 0, 2) == "./")) {
2980 $p_remove_dir = substr($p_remove_dir, 2);
2984 $v_compare = PclZipUtilPathInclusion($p_remove_dir,
2985 $v_stored_filename);
2986 if ($v_compare > 0) {
2987 if ($v_compare == 2) {
2988 $v_stored_filename = "";
2991 $v_stored_filename = substr($v_stored_filename,
2992 strlen($p_remove_dir));
2997 // ----- Remove drive letter if any
2998 $v_stored_filename = PclZipUtilTranslateWinPath($v_stored_filename);
3000 // ----- Look for path to add
3001 if ($p_add_dir != "") {
3002 if (substr($p_add_dir, -1) == "/")
3003 $v_stored_filename = $p_add_dir.$v_stored_filename;
3005 $v_stored_filename = $p_add_dir."/".$v_stored_filename;
3009 // ----- Filename (reduce the path of stored name)
3010 $v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
3011 $p_filedescr['stored_filename'] = $v_stored_filename;
3016 // --------------------------------------------------------------------------------
3018 // --------------------------------------------------------------------------------
3019 // Function : privWriteFileHeader()
3023 // --------------------------------------------------------------------------------
3024 function privWriteFileHeader(&$p_header)
3028 // ----- Store the offset position of the file
3029 $p_header['offset'] = ftell($this->zip_fd);
3031 // ----- Transform UNIX mtime to DOS format mdate/mtime
3032 $v_date = getdate($p_header['mtime']);
3033 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3034 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3036 // ----- Packed data
3037 $v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
3038 $p_header['version_extracted'], $p_header['flag'],
3039 $p_header['compression'], $v_mtime, $v_mdate,
3040 $p_header['crc'], $p_header['compressed_size'],
3042 strlen($p_header['stored_filename']),
3043 $p_header['extra_len']);
3045 // ----- Write the first 148 bytes of the header in the archive
3046 fputs($this->zip_fd, $v_binary_data, 30);
3048 // ----- Write the variable fields
3049 if (strlen($p_header['stored_filename']) != 0)
3051 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3053 if ($p_header['extra_len'] != 0)
3055 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3061 // --------------------------------------------------------------------------------
3063 // --------------------------------------------------------------------------------
3064 // Function : privWriteCentralFileHeader()
3068 // --------------------------------------------------------------------------------
3069 function privWriteCentralFileHeader(&$p_header)
3074 //for(reset($p_header); $key = key($p_header); next($p_header)) {
3077 // ----- Transform UNIX mtime to DOS format mdate/mtime
3078 $v_date = getdate($p_header['mtime']);
3079 $v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
3080 $v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
3083 // ----- Packed data
3084 $v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
3085 $p_header['version'], $p_header['version_extracted'],
3086 $p_header['flag'], $p_header['compression'],
3087 $v_mtime, $v_mdate, $p_header['crc'],
3088 $p_header['compressed_size'], $p_header['size'],
3089 strlen($p_header['stored_filename']),
3090 $p_header['extra_len'], $p_header['comment_len'],
3091 $p_header['disk'], $p_header['internal'],
3092 $p_header['external'], $p_header['offset']);
3094 // ----- Write the 42 bytes of the header in the zip file
3095 fputs($this->zip_fd, $v_binary_data, 46);
3097 // ----- Write the variable fields
3098 if (strlen($p_header['stored_filename']) != 0)
3100 fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
3102 if ($p_header['extra_len'] != 0)
3104 fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
3106 if ($p_header['comment_len'] != 0)
3108 fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
3114 // --------------------------------------------------------------------------------
3116 // --------------------------------------------------------------------------------
3117 // Function : privWriteCentralHeader()
3121 // --------------------------------------------------------------------------------
3122 function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
3126 // ----- Packed data
3127 $v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
3128 $p_nb_entries, $p_size,
3129 $p_offset, strlen($p_comment));
3131 // ----- Write the 22 bytes of the header in the zip file
3132 fputs($this->zip_fd, $v_binary_data, 22);
3134 // ----- Write the variable fields
3135 if (strlen($p_comment) != 0)
3137 fputs($this->zip_fd, $p_comment, strlen($p_comment));
3143 // --------------------------------------------------------------------------------
3145 // --------------------------------------------------------------------------------
3146 // Function : privList()
3150 // --------------------------------------------------------------------------------
3151 function privList(&$p_list)
3155 // ----- Magic quotes trick
3156 $this->privDisableMagicQuotes();
3158 // ----- Open the zip file
3159 if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
3161 // ----- Magic quotes trick
3162 $this->privSwapBackMagicQuotes();
3165 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
3168 return PclZip::errorCode();
3171 // ----- Read the central directory informations
3172 $v_central_dir = array();
3173 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3175 $this->privSwapBackMagicQuotes();
3179 // ----- Go to beginning of Central Dir
3180 @rewind($this->zip_fd);
3181 if (@fseek($this->zip_fd, $v_central_dir['offset']))
3183 $this->privSwapBackMagicQuotes();
3186 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3189 return PclZip::errorCode();
3192 // ----- Read each entry
3193 for ($i=0; $i<$v_central_dir['entries']; $i++)
3195 // ----- Read the file header
3196 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3198 $this->privSwapBackMagicQuotes();
3201 $v_header['index'] = $i;
3203 // ----- Get the only interesting attributes
3204 $this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
3208 // ----- Close the zip file
3209 $this->privCloseFd();
3211 // ----- Magic quotes trick
3212 $this->privSwapBackMagicQuotes();
3217 // --------------------------------------------------------------------------------
3219 // --------------------------------------------------------------------------------
3220 // Function : privConvertHeader2FileInfo()
3222 // This function takes the file informations from the central directory
3223 // entries and extract the interesting parameters that will be given back.
3224 // The resulting file infos are set in the array $p_info
3225 // $p_info['filename'] : Filename with full path. Given by user (add),
3226 // extracted in the filesystem (extract).
3227 // $p_info['stored_filename'] : Stored filename in the archive.
3228 // $p_info['size'] = Size of the file.
3229 // $p_info['compressed_size'] = Compressed size of the file.
3230 // $p_info['mtime'] = Last modification date of the file.
3231 // $p_info['comment'] = Comment associated with the file.
3232 // $p_info['folder'] = true/false : indicates if the entry is a folder or not.
3233 // $p_info['status'] = status of the action on the file.
3234 // $p_info['crc'] = CRC of the file content.
3237 // --------------------------------------------------------------------------------
3238 function privConvertHeader2FileInfo($p_header, &$p_info)
3242 // ----- Get the interesting attributes
3243 $v_temp_path = PclZipUtilPathReduction($p_header['filename']);
3244 $p_info['filename'] = $v_temp_path;
3245 $v_temp_path = PclZipUtilPathReduction($p_header['stored_filename']);
3246 $p_info['stored_filename'] = $v_temp_path;
3247 $p_info['size'] = $p_header['size'];
3248 $p_info['compressed_size'] = $p_header['compressed_size'];
3249 $p_info['mtime'] = $p_header['mtime'];
3250 $p_info['comment'] = $p_header['comment'];
3251 $p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
3252 $p_info['index'] = $p_header['index'];
3253 $p_info['status'] = $p_header['status'];
3254 $p_info['crc'] = $p_header['crc'];
3259 // --------------------------------------------------------------------------------
3261 // --------------------------------------------------------------------------------
3262 // Function : privExtractByRule()
3264 // Extract a file or directory depending of rules (by index, by name, ...)
3266 // $p_file_list : An array where will be placed the properties of each
3268 // $p_path : Path to add while writing the extracted files
3269 // $p_remove_path : Path to remove (from the file memorized path) while writing the
3270 // extracted files. If the path does not match the file path,
3271 // the file is extracted with its memorized path.
3272 // $p_remove_path does not apply to 'list' mode.
3273 // $p_path and $p_remove_path are commulative.
3275 // 1 on success,0 or less on error (see error code list)
3276 // --------------------------------------------------------------------------------
3277 function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3281 // ----- Magic quotes trick
3282 $this->privDisableMagicQuotes();
3284 // ----- Check the path
3285 if ( ($p_path == "")
3286 || ( (substr($p_path, 0, 1) != "/")
3287 && (substr($p_path, 0, 3) != "../")
3288 && (substr($p_path,1,2)!=":/")))
3289 $p_path = "./".$p_path;
3291 // ----- Reduce the path last (and duplicated) '/'
3292 if (($p_path != "./") && ($p_path != "/"))
3294 // ----- Look for the path end '/'
3295 while (substr($p_path, -1) == "/")
3297 $p_path = substr($p_path, 0, strlen($p_path)-1);
3301 // ----- Look for path to remove format (should end by /)
3302 if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
3304 $p_remove_path .= '/';
3306 $p_remove_path_size = strlen($p_remove_path);
3308 // ----- Open the zip file
3309 if (($v_result = $this->privOpenFd('rb')) != 1)
3311 $this->privSwapBackMagicQuotes();
3315 // ----- Read the central directory informations
3316 $v_central_dir = array();
3317 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
3319 // ----- Close the zip file
3320 $this->privCloseFd();
3321 $this->privSwapBackMagicQuotes();
3326 // ----- Start at beginning of Central Dir
3327 $v_pos_entry = $v_central_dir['offset'];
3329 // ----- Read each entry
3331 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
3334 // ----- Read next Central dir entry
3335 @rewind($this->zip_fd);
3336 if (@fseek($this->zip_fd, $v_pos_entry))
3338 // ----- Close the zip file
3339 $this->privCloseFd();
3340 $this->privSwapBackMagicQuotes();
3343 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3346 return PclZip::errorCode();
3349 // ----- Read the file header
3350 $v_header = array();
3351 if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
3353 // ----- Close the zip file
3354 $this->privCloseFd();
3355 $this->privSwapBackMagicQuotes();
3360 // ----- Store the index
3361 $v_header['index'] = $i;
3363 // ----- Store the file position
3364 $v_pos_entry = ftell($this->zip_fd);
3366 // ----- Look for the specific extract rules
3369 // ----- Look for extract by name rule
3370 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
3371 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
3373 // ----- Look if the filename is in the list
3374 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_extract); $j++) {
3376 // ----- Look for a directory
3377 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
3379 // ----- Look if the directory is in the filename path
3380 if ( (strlen($v_header['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
3381 && (substr($v_header['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
3385 // ----- Look for a filename
3386 elseif ($v_header['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
3392 // ----- Look for extract by ereg rule
3393 // ereg() is deprecated with PHP 5.3
3395 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
3396 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
3398 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header['stored_filename'])) {
3404 // ----- Look for extract by preg rule
3405 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
3406 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
3408 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header['stored_filename'])) {
3413 // ----- Look for extract by index rule
3414 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
3415 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
3417 // ----- Look if the index is in the list
3418 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_extract); $j++) {
3420 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
3423 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
3427 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
3433 // ----- Look for no rule, which means extract all the archive
3438 // ----- Check compression method
3440 && ( ($v_header['compression'] != 8)
3441 && ($v_header['compression'] != 0))) {
3442 $v_header['status'] = 'unsupported_compression';
3444 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3445 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3446 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3448 $this->privSwapBackMagicQuotes();
3450 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_COMPRESSION,
3451 "Filename '".$v_header['stored_filename']."' is "
3452 ."compressed by an unsupported compression "
3453 ."method (".$v_header['compression'].") ");
3455 return PclZip::errorCode();
3459 // ----- Check encrypted files
3460 if (($v_extract) && (($v_header['flag'] & 1) == 1)) {
3461 $v_header['status'] = 'unsupported_encryption';
3463 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3464 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3465 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3467 $this->privSwapBackMagicQuotes();
3469 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION,
3470 "Unsupported encryption for "
3471 ." filename '".$v_header['stored_filename']
3474 return PclZip::errorCode();
3478 // ----- Look for real extraction
3479 if (($v_extract) && ($v_header['status'] != 'ok')) {
3480 $v_result = $this->privConvertHeader2FileInfo($v_header,
3481 $p_file_list[$v_nb_extracted++]);
3482 if ($v_result != 1) {
3483 $this->privCloseFd();
3484 $this->privSwapBackMagicQuotes();
3491 // ----- Look for real extraction
3495 // ----- Go to the file position
3496 @rewind($this->zip_fd);
3497 if (@fseek($this->zip_fd, $v_header['offset']))
3499 // ----- Close the zip file
3500 $this->privCloseFd();
3502 $this->privSwapBackMagicQuotes();
3505 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
3508 return PclZip::errorCode();
3511 // ----- Look for extraction as string
3512 if ($p_options[PCLZIP_OPT_EXTRACT_AS_STRING]) {
3516 // ----- Extracting the file
3517 $v_result1 = $this->privExtractFileAsString($v_header, $v_string, $p_options);
3518 if ($v_result1 < 1) {
3519 $this->privCloseFd();
3520 $this->privSwapBackMagicQuotes();
3524 // ----- Get the only interesting attributes
3525 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted])) != 1)
3527 // ----- Close the zip file
3528 $this->privCloseFd();
3529 $this->privSwapBackMagicQuotes();
3534 // ----- Set the file content
3535 $p_file_list[$v_nb_extracted]['content'] = $v_string;
3537 // ----- Next extracted file
3540 // ----- Look for user callback abort
3541 if ($v_result1 == 2) {
3545 // ----- Look for extraction in standard output
3546 elseif ( (isset($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT]))
3547 && ($p_options[PCLZIP_OPT_EXTRACT_IN_OUTPUT])) {
3548 // ----- Extracting the file in standard output
3549 $v_result1 = $this->privExtractFileInOutput($v_header, $p_options);
3550 if ($v_result1 < 1) {
3551 $this->privCloseFd();
3552 $this->privSwapBackMagicQuotes();
3556 // ----- Get the only interesting attributes
3557 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1) {
3558 $this->privCloseFd();
3559 $this->privSwapBackMagicQuotes();
3563 // ----- Look for user callback abort
3564 if ($v_result1 == 2) {
3568 // ----- Look for normal extraction
3570 // ----- Extracting the file
3571 $v_result1 = $this->privExtractFile($v_header,
3572 $p_path, $p_remove_path,
3575 if ($v_result1 < 1) {
3576 $this->privCloseFd();
3577 $this->privSwapBackMagicQuotes();
3581 // ----- Get the only interesting attributes
3582 if (($v_result = $this->privConvertHeader2FileInfo($v_header, $p_file_list[$v_nb_extracted++])) != 1)
3584 // ----- Close the zip file
3585 $this->privCloseFd();
3586 $this->privSwapBackMagicQuotes();
3591 // ----- Look for user callback abort
3592 if ($v_result1 == 2) {
3599 // ----- Close the zip file
3600 $this->privCloseFd();
3601 $this->privSwapBackMagicQuotes();
3606 // --------------------------------------------------------------------------------
3608 // --------------------------------------------------------------------------------
3609 // Function : privExtractFile()
3615 // PCLZIP_ERR_USER_ABORTED(2) : User ask for extraction stop in callback
3616 // --------------------------------------------------------------------------------
3617 function privExtractFile(&$p_entry, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
3621 // ----- Read the file header
3622 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
3629 // ----- Check that the file header is coherent with $p_entry info
3630 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
3634 // ----- Look for all path to remove
3635 if ($p_remove_all_path == true) {
3636 // ----- Look for folder entry that not need to be extracted
3637 if (($p_entry['external']&0x00000010)==0x00000010) {
3639 $p_entry['status'] = "filtered";
3644 // ----- Get the basename of the path
3645 $p_entry['filename'] = basename($p_entry['filename']);
3648 // ----- Look for path to remove
3649 else if ($p_remove_path != "")
3651 if (PclZipUtilPathInclusion($p_remove_path, $p_entry['filename']) == 2)
3654 // ----- Change the file status
3655 $p_entry['status'] = "filtered";
3661 $p_remove_path_size = strlen($p_remove_path);
3662 if (substr($p_entry['filename'], 0, $p_remove_path_size) == $p_remove_path)
3665 // ----- Remove the path
3666 $p_entry['filename'] = substr($p_entry['filename'], $p_remove_path_size);
3671 // ----- Add the path
3672 if ($p_path != '') {
3673 $p_entry['filename'] = $p_path."/".$p_entry['filename'];
3676 // ----- Check a base_dir_restriction
3677 if (isset($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION])) {
3679 = PclZipUtilPathInclusion($p_options[PCLZIP_OPT_EXTRACT_DIR_RESTRICTION],
3680 $p_entry['filename']);
3681 if ($v_inclusion == 0) {
3683 PclZip::privErrorLog(PCLZIP_ERR_DIRECTORY_RESTRICTION,
3684 "Filename '".$p_entry['filename']."' is "
3685 ."outside PCLZIP_OPT_EXTRACT_DIR_RESTRICTION");
3687 return PclZip::errorCode();
3691 // ----- Look for pre-extract callback
3692 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
3694 // ----- Generate a local information
3695 $v_local_header = array();
3696 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3698 // ----- Call the callback
3699 // Here I do not use call_user_func() because I need to send a reference to the
3701 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
3702 if ($v_result == 0) {
3703 // ----- Change the file status
3704 $p_entry['status'] = "skipped";
3708 // ----- Look for abort result
3709 if ($v_result == 2) {
3710 // ----- This status is internal and will be changed in 'skipped'
3711 $p_entry['status'] = "aborted";
3712 $v_result = PCLZIP_ERR_USER_ABORTED;
3715 // ----- Update the informations
3716 // Only some fields can be modified
3717 $p_entry['filename'] = $v_local_header['filename'];
3721 // ----- Look if extraction should be done
3722 if ($p_entry['status'] == 'ok') {
3724 // ----- Look for specific actions while the file exist
3725 if (file_exists($p_entry['filename']))
3728 // ----- Look if file is a directory
3729 if (is_dir($p_entry['filename']))
3732 // ----- Change the file status
3733 $p_entry['status'] = "already_a_directory";
3735 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3736 // For historical reason first PclZip implementation does not stop
3737 // when this kind of error occurs.
3738 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3739 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3741 PclZip::privErrorLog(PCLZIP_ERR_ALREADY_A_DIRECTORY,
3742 "Filename '".$p_entry['filename']."' is "
3743 ."already used by an existing directory");
3745 return PclZip::errorCode();
3748 // ----- Look if file is write protected
3749 else if (!is_writeable($p_entry['filename']))
3752 // ----- Change the file status
3753 $p_entry['status'] = "write_protected";
3755 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3756 // For historical reason first PclZip implementation does not stop
3757 // when this kind of error occurs.
3758 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3759 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3761 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3762 "Filename '".$p_entry['filename']."' exists "
3763 ."and is write protected");
3765 return PclZip::errorCode();
3769 // ----- Look if the extracted file is older
3770 else if (filemtime($p_entry['filename']) > $p_entry['mtime'])
3772 // ----- Change the file status
3773 if ( (isset($p_options[PCLZIP_OPT_REPLACE_NEWER]))
3774 && ($p_options[PCLZIP_OPT_REPLACE_NEWER]===true)) {
3777 $p_entry['status'] = "newer_exist";
3779 // ----- Look for PCLZIP_OPT_STOP_ON_ERROR
3780 // For historical reason first PclZip implementation does not stop
3781 // when this kind of error occurs.
3782 if ( (isset($p_options[PCLZIP_OPT_STOP_ON_ERROR]))
3783 && ($p_options[PCLZIP_OPT_STOP_ON_ERROR]===true)) {
3785 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL,
3786 "Newer version of '".$p_entry['filename']."' exists "
3787 ."and option PCLZIP_OPT_REPLACE_NEWER is not selected");
3789 return PclZip::errorCode();
3797 // ----- Check the directory availability and create it if necessary
3799 if ((($p_entry['external']&0x00000010)==0x00000010) || (substr($p_entry['filename'], -1) == '/'))
3800 $v_dir_to_check = $p_entry['filename'];
3801 else if (!strstr($p_entry['filename'], "/"))
3802 $v_dir_to_check = "";
3804 $v_dir_to_check = dirname($p_entry['filename']);
3806 if (($v_result = $this->privDirCheck($v_dir_to_check, (($p_entry['external']&0x00000010)==0x00000010))) != 1) {
3808 // ----- Change the file status
3809 $p_entry['status'] = "path_creation_fail";
3818 // ----- Look if extraction should be done
3819 if ($p_entry['status'] == 'ok') {
3821 // ----- Do the extraction (if not a folder)
3822 if (!(($p_entry['external']&0x00000010)==0x00000010))
3824 // ----- Look for not compressed file
3825 if ($p_entry['compression'] == 0) {
3827 // ----- Opening destination file
3828 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0)
3831 // ----- Change the file status
3832 $p_entry['status'] = "write_error";
3839 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3840 $v_size = $p_entry['compressed_size'];
3841 while ($v_size != 0)
3843 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3844 $v_buffer = @fread($this->zip_fd, $v_read_size);
3845 /* Try to speed up the code
3846 $v_binary_data = pack('a'.$v_read_size, $v_buffer);
3847 @fwrite($v_dest_file, $v_binary_data, $v_read_size);
3849 @fwrite($v_dest_file, $v_buffer, $v_read_size);
3850 $v_size -= $v_read_size;
3853 // ----- Closing the destination file
3854 fclose($v_dest_file);
3856 // ----- Change the file mtime
3857 touch($p_entry['filename'], $p_entry['mtime']);
3863 // Need to be finished
3864 if (($p_entry['flag'] & 1) == 1) {
3865 PclZip::privErrorLog(PCLZIP_ERR_UNSUPPORTED_ENCRYPTION, 'File \''.$p_entry['filename'].'\' is encrypted. Encrypted files are not supported.');
3866 return PclZip::errorCode();
3870 // ----- Look for using temporary file to unzip
3871 if ( (!isset($p_options[PCLZIP_OPT_TEMP_FILE_OFF]))
3872 && (isset($p_options[PCLZIP_OPT_TEMP_FILE_ON])
3873 || (isset($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD])
3874 && ($p_options[PCLZIP_OPT_TEMP_FILE_THRESHOLD] <= $p_entry['size'])) ) ) {
3875 $v_result = $this->privExtractFileUsingTempFile($p_entry, $p_options);
3876 if ($v_result < PCLZIP_ERR_NO_ERROR) {
3881 // ----- Look for extract in memory
3885 // ----- Read the compressed file in a buffer (one shot)
3886 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
3888 // ----- Decompress the file
3889 $v_file_content = @gzinflate($v_buffer);
3891 if ($v_file_content === FALSE) {
3893 // ----- Change the file status
3895 $p_entry['status'] = "error";
3900 // ----- Opening destination file
3901 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
3903 // ----- Change the file status
3904 $p_entry['status'] = "write_error";
3909 // ----- Write the uncompressed data
3910 @fwrite($v_dest_file, $v_file_content, $p_entry['size']);
3911 unset($v_file_content);
3913 // ----- Closing the destination file
3914 @fclose($v_dest_file);
3918 // ----- Change the file mtime
3919 @touch($p_entry['filename'], $p_entry['mtime']);
3922 // ----- Look for chmod option
3923 if (isset($p_options[PCLZIP_OPT_SET_CHMOD])) {
3925 // ----- Change the mode of the file
3926 @chmod($p_entry['filename'], $p_options[PCLZIP_OPT_SET_CHMOD]);
3932 // ----- Change abort status
3933 if ($p_entry['status'] == "aborted") {
3934 $p_entry['status'] = "skipped";
3937 // ----- Look for post-extract callback
3938 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
3940 // ----- Generate a local information
3941 $v_local_header = array();
3942 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
3944 // ----- Call the callback
3945 // Here I do not use call_user_func() because I need to send a reference to the
3947 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
3949 // ----- Look for abort result
3950 if ($v_result == 2) {
3951 $v_result = PCLZIP_ERR_USER_ABORTED;
3958 // --------------------------------------------------------------------------------
3960 // --------------------------------------------------------------------------------
3961 // Function : privExtractFileUsingTempFile()
3965 // --------------------------------------------------------------------------------
3966 function privExtractFileUsingTempFile(&$p_entry, &$p_options)
3970 // ----- Creates a temporary file
3971 $v_gzip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.gz';
3972 if (($v_dest_file = @fopen($v_gzip_temp_name, "wb")) == 0) {
3974 PclZip::privErrorLog(PCLZIP_ERR_WRITE_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary write mode');
3975 return PclZip::errorCode();
3979 // ----- Write gz file format header
3980 $v_binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($p_entry['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
3981 @fwrite($v_dest_file, $v_binary_data, 10);
3983 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
3984 $v_size = $p_entry['compressed_size'];
3985 while ($v_size != 0)
3987 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
3988 $v_buffer = @fread($this->zip_fd, $v_read_size);
3989 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
3990 @fwrite($v_dest_file, $v_buffer, $v_read_size);
3991 $v_size -= $v_read_size;
3994 // ----- Write gz file format footer
3995 $v_binary_data = pack('VV', $p_entry['crc'], $p_entry['size']);
3996 @fwrite($v_dest_file, $v_binary_data, 8);
3998 // ----- Close the temporary file
3999 @fclose($v_dest_file);
4001 // ----- Opening destination file
4002 if (($v_dest_file = @fopen($p_entry['filename'], 'wb')) == 0) {
4003 $p_entry['status'] = "write_error";
4007 // ----- Open the temporary gz file
4008 if (($v_src_file = @gzopen($v_gzip_temp_name, 'rb')) == 0) {
4009 @fclose($v_dest_file);
4010 $p_entry['status'] = "read_error";
4011 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_gzip_temp_name.'\' in binary read mode');
4012 return PclZip::errorCode();
4016 // ----- Read the file by PCLZIP_READ_BLOCK_SIZE octets blocks
4017 $v_size = $p_entry['size'];
4018 while ($v_size != 0) {
4019 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
4020 $v_buffer = @gzread($v_src_file, $v_read_size);
4021 //$v_binary_data = pack('a'.$v_read_size, $v_buffer);
4022 @fwrite($v_dest_file, $v_buffer, $v_read_size);
4023 $v_size -= $v_read_size;
4025 @fclose($v_dest_file);
4026 @gzclose($v_src_file);
4028 // ----- Delete the temporary file
4029 @unlink($v_gzip_temp_name);
4034 // --------------------------------------------------------------------------------
4036 // --------------------------------------------------------------------------------
4037 // Function : privExtractFileInOutput()
4041 // --------------------------------------------------------------------------------
4042 function privExtractFileInOutput(&$p_entry, &$p_options)
4046 // ----- Read the file header
4047 if (($v_result = $this->privReadFileHeader($v_header)) != 1) {
4052 // ----- Check that the file header is coherent with $p_entry info
4053 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4057 // ----- Look for pre-extract callback
4058 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
4060 // ----- Generate a local information
4061 $v_local_header = array();
4062 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4064 // ----- Call the callback
4065 // Here I do not use call_user_func() because I need to send a reference to the
4067 // eval('$v_result = '.$p_options[PCLZIP_CB_PRE_EXTRACT].'(PCLZIP_CB_PRE_EXTRACT, $v_local_header);');
4068 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
4069 if ($v_result == 0) {
4070 // ----- Change the file status
4071 $p_entry['status'] = "skipped";
4075 // ----- Look for abort result
4076 if ($v_result == 2) {
4077 // ----- This status is internal and will be changed in 'skipped'
4078 $p_entry['status'] = "aborted";
4079 $v_result = PCLZIP_ERR_USER_ABORTED;
4082 // ----- Update the informations
4083 // Only some fields can be modified
4084 $p_entry['filename'] = $v_local_header['filename'];
4089 // ----- Look if extraction should be done
4090 if ($p_entry['status'] == 'ok') {
4092 // ----- Do the extraction (if not a folder)
4093 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
4094 // ----- Look for not compressed file
4095 if ($p_entry['compressed_size'] == $p_entry['size']) {
4097 // ----- Read the file in a buffer (one shot)
4098 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
4100 // ----- Send the file to the output
4106 // ----- Read the compressed file in a buffer (one shot)
4107 $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']);
4109 // ----- Decompress the file
4110 $v_file_content = gzinflate($v_buffer);
4113 // ----- Send the file to the output
4114 echo $v_file_content;
4115 unset($v_file_content);
4120 // ----- Change abort status
4121 if ($p_entry['status'] == "aborted") {
4122 $p_entry['status'] = "skipped";
4125 // ----- Look for post-extract callback
4126 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
4128 // ----- Generate a local information
4129 $v_local_header = array();
4130 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4132 // ----- Call the callback
4133 // Here I do not use call_user_func() because I need to send a reference to the
4135 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
4137 // ----- Look for abort result
4138 if ($v_result == 2) {
4139 $v_result = PCLZIP_ERR_USER_ABORTED;
4145 // --------------------------------------------------------------------------------
4147 // --------------------------------------------------------------------------------
4148 // Function : privExtractFileAsString()
4152 // --------------------------------------------------------------------------------
4153 function privExtractFileAsString(&$p_entry, &$p_string, &$p_options)
4157 // ----- Read the file header
4158 $v_header = array();
4159 if (($v_result = $this->privReadFileHeader($v_header)) != 1)
4166 // ----- Check that the file header is coherent with $p_entry info
4167 if ($this->privCheckFileHeaders($v_header, $p_entry) != 1) {
4171 // ----- Look for pre-extract callback
4172 if (isset($p_options[PCLZIP_CB_PRE_EXTRACT])) {
4174 // ----- Generate a local information
4175 $v_local_header = array();
4176 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4178 // ----- Call the callback
4179 // Here I do not use call_user_func() because I need to send a reference to the
4181 $v_result = $p_options[PCLZIP_CB_PRE_EXTRACT](PCLZIP_CB_PRE_EXTRACT, $v_local_header);
4182 if ($v_result == 0) {
4183 // ----- Change the file status
4184 $p_entry['status'] = "skipped";
4188 // ----- Look for abort result
4189 if ($v_result == 2) {
4190 // ----- This status is internal and will be changed in 'skipped'
4191 $p_entry['status'] = "aborted";
4192 $v_result = PCLZIP_ERR_USER_ABORTED;
4195 // ----- Update the informations
4196 // Only some fields can be modified
4197 $p_entry['filename'] = $v_local_header['filename'];
4201 // ----- Look if extraction should be done
4202 if ($p_entry['status'] == 'ok') {
4204 // ----- Do the extraction (if not a folder)
4205 if (!(($p_entry['external']&0x00000010)==0x00000010)) {
4206 // ----- Look for not compressed file
4207 // if ($p_entry['compressed_size'] == $p_entry['size'])
4208 if ($p_entry['compression'] == 0) {
4210 // ----- Reading the file
4211 $p_string = @fread($this->zip_fd, $p_entry['compressed_size']);
4215 // ----- Reading the file
4216 $v_data = @fread($this->zip_fd, $p_entry['compressed_size']);
4218 // ----- Decompress the file
4219 if (($p_string = @gzinflate($v_data)) === FALSE) {
4227 // TBC : error : can not extract a folder in a string
4232 // ----- Change abort status
4233 if ($p_entry['status'] == "aborted") {
4234 $p_entry['status'] = "skipped";
4237 // ----- Look for post-extract callback
4238 elseif (isset($p_options[PCLZIP_CB_POST_EXTRACT])) {
4240 // ----- Generate a local information
4241 $v_local_header = array();
4242 $this->privConvertHeader2FileInfo($p_entry, $v_local_header);
4244 // ----- Swap the content to header
4245 $v_local_header['content'] = $p_string;
4248 // ----- Call the callback
4249 // Here I do not use call_user_func() because I need to send a reference to the
4251 $v_result = $p_options[PCLZIP_CB_POST_EXTRACT](PCLZIP_CB_POST_EXTRACT, $v_local_header);
4253 // ----- Swap back the content to header
4254 $p_string = $v_local_header['content'];
4255 unset($v_local_header['content']);
4257 // ----- Look for abort result
4258 if ($v_result == 2) {
4259 $v_result = PCLZIP_ERR_USER_ABORTED;
4266 // --------------------------------------------------------------------------------
4268 // --------------------------------------------------------------------------------
4269 // Function : privReadFileHeader()
4273 // --------------------------------------------------------------------------------
4274 function privReadFileHeader(&$p_header)
4278 // ----- Read the 4 bytes signature
4279 $v_binary_data = @fread($this->zip_fd, 4);
4280 $v_data = unpack('Vid', $v_binary_data);
4282 // ----- Check signature
4283 if ($v_data['id'] != 0x04034b50)
4287 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4290 return PclZip::errorCode();
4293 // ----- Read the first 42 bytes of the header
4294 $v_binary_data = fread($this->zip_fd, 26);
4296 // ----- Look for invalid block size
4297 if (strlen($v_binary_data) != 26)
4299 $p_header['filename'] = "";
4300 $p_header['status'] = "invalid_header";
4303 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4306 return PclZip::errorCode();
4309 // ----- Extract the values
4310 $v_data = unpack('vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $v_binary_data);
4312 // ----- Get filename
4313 $p_header['filename'] = fread($this->zip_fd, $v_data['filename_len']);
4315 // ----- Get extra_fields
4316 if ($v_data['extra_len'] != 0) {
4317 $p_header['extra'] = fread($this->zip_fd, $v_data['extra_len']);
4320 $p_header['extra'] = '';
4323 // ----- Extract properties
4324 $p_header['version_extracted'] = $v_data['version'];
4325 $p_header['compression'] = $v_data['compression'];
4326 $p_header['size'] = $v_data['size'];
4327 $p_header['compressed_size'] = $v_data['compressed_size'];
4328 $p_header['crc'] = $v_data['crc'];
4329 $p_header['flag'] = $v_data['flag'];
4330 $p_header['filename_len'] = $v_data['filename_len'];
4332 // ----- Recuperate date in UNIX format
4333 $p_header['mdate'] = $v_data['mdate'];
4334 $p_header['mtime'] = $v_data['mtime'];
4335 if ($p_header['mdate'] && $p_header['mtime'])
4337 // ----- Extract time
4338 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4339 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4340 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4342 // ----- Extract date
4343 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4344 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4345 $v_day = $p_header['mdate'] & 0x001F;
4347 // ----- Get UNIX date format
4348 $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4353 $p_header['mtime'] = time();
4357 //for(reset($v_data); $key = key($v_data); next($v_data)) {
4360 // ----- Set the stored filename
4361 $p_header['stored_filename'] = $p_header['filename'];
4363 // ----- Set the status field
4364 $p_header['status'] = "ok";
4369 // --------------------------------------------------------------------------------
4371 // --------------------------------------------------------------------------------
4372 // Function : privReadCentralFileHeader()
4376 // --------------------------------------------------------------------------------
4377 function privReadCentralFileHeader(&$p_header)
4381 // ----- Read the 4 bytes signature
4382 $v_binary_data = @fread($this->zip_fd, 4);
4383 $v_data = unpack('Vid', $v_binary_data);
4385 // ----- Check signature
4386 if ($v_data['id'] != 0x02014b50)
4390 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Invalid archive structure');
4393 return PclZip::errorCode();
4396 // ----- Read the first 42 bytes of the header
4397 $v_binary_data = fread($this->zip_fd, 42);
4399 // ----- Look for invalid block size
4400 if (strlen($v_binary_data) != 42)
4402 $p_header['filename'] = "";
4403 $p_header['status'] = "invalid_header";
4406 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid block size : ".strlen($v_binary_data));
4409 return PclZip::errorCode();
4412 // ----- Extract the values
4413 $p_header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $v_binary_data);
4415 // ----- Get filename
4416 if ($p_header['filename_len'] != 0)
4417 $p_header['filename'] = fread($this->zip_fd, $p_header['filename_len']);
4419 $p_header['filename'] = '';
4422 if ($p_header['extra_len'] != 0)
4423 $p_header['extra'] = fread($this->zip_fd, $p_header['extra_len']);
4425 $p_header['extra'] = '';
4427 // ----- Get comment
4428 if ($p_header['comment_len'] != 0)
4429 $p_header['comment'] = fread($this->zip_fd, $p_header['comment_len']);
4431 $p_header['comment'] = '';
4433 // ----- Extract properties
4435 // ----- Recuperate date in UNIX format
4436 //if ($p_header['mdate'] && $p_header['mtime'])
4437 // TBC : bug : this was ignoring time with 0/0/0
4440 // ----- Extract time
4441 $v_hour = ($p_header['mtime'] & 0xF800) >> 11;
4442 $v_minute = ($p_header['mtime'] & 0x07E0) >> 5;
4443 $v_seconde = ($p_header['mtime'] & 0x001F)*2;
4445 // ----- Extract date
4446 $v_year = (($p_header['mdate'] & 0xFE00) >> 9) + 1980;
4447 $v_month = ($p_header['mdate'] & 0x01E0) >> 5;
4448 $v_day = $p_header['mdate'] & 0x001F;
4450 // ----- Get UNIX date format
4451 $p_header['mtime'] = @mktime($v_hour, $v_minute, $v_seconde, $v_month, $v_day, $v_year);
4456 $p_header['mtime'] = time();
4459 // ----- Set the stored filename
4460 $p_header['stored_filename'] = $p_header['filename'];
4462 // ----- Set default status to ok
4463 $p_header['status'] = 'ok';
4465 // ----- Look if it is a directory
4466 if (substr($p_header['filename'], -1) == '/') {
4467 //$p_header['external'] = 0x41FF0010;
4468 $p_header['external'] = 0x00000010;
4475 // --------------------------------------------------------------------------------
4477 // --------------------------------------------------------------------------------
4478 // Function : privCheckFileHeaders()
4484 // --------------------------------------------------------------------------------
4485 function privCheckFileHeaders(&$p_local_header, &$p_central_header)
4489 // ----- Check the static values
4491 if ($p_local_header['filename'] != $p_central_header['filename']) {
4493 if ($p_local_header['version_extracted'] != $p_central_header['version_extracted']) {
4495 if ($p_local_header['flag'] != $p_central_header['flag']) {
4497 if ($p_local_header['compression'] != $p_central_header['compression']) {
4499 if ($p_local_header['mtime'] != $p_central_header['mtime']) {
4501 if ($p_local_header['filename_len'] != $p_central_header['filename_len']) {
4504 // ----- Look for flag bit 3
4505 if (($p_local_header['flag'] & 8) == 8) {
4506 $p_local_header['size'] = $p_central_header['size'];
4507 $p_local_header['compressed_size'] = $p_central_header['compressed_size'];
4508 $p_local_header['crc'] = $p_central_header['crc'];
4514 // --------------------------------------------------------------------------------
4516 // --------------------------------------------------------------------------------
4517 // Function : privReadEndCentralDir()
4521 // --------------------------------------------------------------------------------
4522 function privReadEndCentralDir(&$p_central_dir)
4526 // ----- Go to the end of the zip file
4527 $v_size = filesize($this->zipname);
4528 @fseek($this->zip_fd, $v_size);
4529 if (@ftell($this->zip_fd) != $v_size)
4532 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to go to the end of the archive \''.$this->zipname.'\'');
4535 return PclZip::errorCode();
4538 // ----- First try : look if this is an archive with no commentaries (most of the time)
4539 // in this case the end of central dir is at 22 bytes of the file end
4542 @fseek($this->zip_fd, $v_size-22);
4543 if (($v_pos = @ftell($this->zip_fd)) != ($v_size-22))
4546 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4549 return PclZip::errorCode();
4552 // ----- Read for bytes
4553 $v_binary_data = @fread($this->zip_fd, 4);
4554 $v_data = @unpack('Vid', $v_binary_data);
4556 // ----- Check signature
4557 if ($v_data['id'] == 0x06054b50) {
4561 $v_pos = ftell($this->zip_fd);
4564 // ----- Go back to the maximum possible size of the Central Dir End Record
4566 $v_maximum_size = 65557; // 0xFFFF + 22;
4567 if ($v_maximum_size > $v_size)
4568 $v_maximum_size = $v_size;
4569 @fseek($this->zip_fd, $v_size-$v_maximum_size);
4570 if (@ftell($this->zip_fd) != ($v_size-$v_maximum_size))
4573 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, 'Unable to seek back to the middle of the archive \''.$this->zipname.'\'');
4576 return PclZip::errorCode();
4579 // ----- Read byte per byte in order to find the signature
4580 $v_pos = ftell($this->zip_fd);
4581 $v_bytes = 0x00000000;
4582 while ($v_pos < $v_size)
4584 // ----- Read a byte
4585 $v_byte = @fread($this->zip_fd, 1);
4587 // ----- Add the byte
4588 //$v_bytes = ($v_bytes << 8) | Ord($v_byte);
4589 // Note we mask the old value down such that once shifted we can never end up with more than a 32bit number
4590 // Otherwise on systems where we have 64bit integers the check below for the magic number will fail.
4591 $v_bytes = ( ($v_bytes & 0xFFFFFF) << 8) | Ord($v_byte);
4593 // ----- Compare the bytes
4594 if ($v_bytes == 0x504b0506)
4603 // ----- Look if not found end of central dir
4604 if ($v_pos == $v_size)
4608 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Unable to find End of Central Dir Record signature");
4611 return PclZip::errorCode();
4615 // ----- Read the first 18 bytes of the header
4616 $v_binary_data = fread($this->zip_fd, 18);
4618 // ----- Look for invalid block size
4619 if (strlen($v_binary_data) != 18)
4623 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT, "Invalid End of Central Dir Record size : ".strlen($v_binary_data));
4626 return PclZip::errorCode();
4629 // ----- Extract the values
4630 $v_data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $v_binary_data);
4632 // ----- Check the global size
4633 if (($v_pos + $v_data['comment_size'] + 18) != $v_size) {
4635 // ----- Removed in release 2.2 see readme file
4636 // The check of the file size is a little too strict.
4637 // Some bugs where found when a zip is encrypted/decrypted with 'crypt'.
4638 // While decrypted, zip has training 0 bytes
4641 PclZip::privErrorLog(PCLZIP_ERR_BAD_FORMAT,
4642 'The central dir is not at the end of the archive.'
4643 .' Some trailing bytes exists after the archive.');
4646 return PclZip::errorCode();
4650 // ----- Get comment
4651 if ($v_data['comment_size'] != 0) {
4652 $p_central_dir['comment'] = fread($this->zip_fd, $v_data['comment_size']);
4655 $p_central_dir['comment'] = '';
4657 $p_central_dir['entries'] = $v_data['entries'];
4658 $p_central_dir['disk_entries'] = $v_data['disk_entries'];
4659 $p_central_dir['offset'] = $v_data['offset'];
4660 $p_central_dir['size'] = $v_data['size'];
4661 $p_central_dir['disk'] = $v_data['disk'];
4662 $p_central_dir['disk_start'] = $v_data['disk_start'];
4665 //for(reset($p_central_dir); $key = key($p_central_dir); next($p_central_dir)) {
4671 // --------------------------------------------------------------------------------
4673 // --------------------------------------------------------------------------------
4674 // Function : privDeleteByRule()
4678 // --------------------------------------------------------------------------------
4679 function privDeleteByRule(&$p_result_list, &$p_options)
4682 $v_list_detail = array();
4684 // ----- Open the zip file
4685 if (($v_result=$this->privOpenFd('rb')) != 1)
4691 // ----- Read the central directory informations
4692 $v_central_dir = array();
4693 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
4695 $this->privCloseFd();
4699 // ----- Go to beginning of File
4700 @rewind($this->zip_fd);
4702 // ----- Scan all the files
4703 // ----- Start at beginning of Central Dir
4704 $v_pos_entry = $v_central_dir['offset'];
4705 @rewind($this->zip_fd);
4706 if (@fseek($this->zip_fd, $v_pos_entry))
4708 // ----- Close the zip file
4709 $this->privCloseFd();
4712 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4715 return PclZip::errorCode();
4718 // ----- Read each entry
4719 $v_header_list = array();
4721 for ($i=0, $v_nb_extracted=0; $i<$v_central_dir['entries']; $i++)
4724 // ----- Read the file header
4725 $v_header_list[$v_nb_extracted] = array();
4726 if (($v_result = $this->privReadCentralFileHeader($v_header_list[$v_nb_extracted])) != 1)
4728 // ----- Close the zip file
4729 $this->privCloseFd();
4735 // ----- Store the index
4736 $v_header_list[$v_nb_extracted]['index'] = $i;
4738 // ----- Look for the specific extract rules
4741 // ----- Look for extract by name rule
4742 if ( (isset($p_options[PCLZIP_OPT_BY_NAME]))
4743 && ($p_options[PCLZIP_OPT_BY_NAME] != 0)) {
4745 // ----- Look if the filename is in the list
4746 for ($j=0; ($j<sizeof($p_options[PCLZIP_OPT_BY_NAME])) && (!$v_found); $j++) {
4748 // ----- Look for a directory
4749 if (substr($p_options[PCLZIP_OPT_BY_NAME][$j], -1) == "/") {
4751 // ----- Look if the directory is in the filename path
4752 if ( (strlen($v_header_list[$v_nb_extracted]['stored_filename']) > strlen($p_options[PCLZIP_OPT_BY_NAME][$j]))
4753 && (substr($v_header_list[$v_nb_extracted]['stored_filename'], 0, strlen($p_options[PCLZIP_OPT_BY_NAME][$j])) == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4756 elseif ( (($v_header_list[$v_nb_extracted]['external']&0x00000010)==0x00000010) /* Indicates a folder */
4757 && ($v_header_list[$v_nb_extracted]['stored_filename'].'/' == $p_options[PCLZIP_OPT_BY_NAME][$j])) {
4761 // ----- Look for a filename
4762 elseif ($v_header_list[$v_nb_extracted]['stored_filename'] == $p_options[PCLZIP_OPT_BY_NAME][$j]) {
4768 // ----- Look for extract by ereg rule
4769 // ereg() is deprecated with PHP 5.3
4771 else if ( (isset($p_options[PCLZIP_OPT_BY_EREG]))
4772 && ($p_options[PCLZIP_OPT_BY_EREG] != "")) {
4774 if (ereg($p_options[PCLZIP_OPT_BY_EREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4780 // ----- Look for extract by preg rule
4781 else if ( (isset($p_options[PCLZIP_OPT_BY_PREG]))
4782 && ($p_options[PCLZIP_OPT_BY_PREG] != "")) {
4784 if (preg_match($p_options[PCLZIP_OPT_BY_PREG], $v_header_list[$v_nb_extracted]['stored_filename'])) {
4789 // ----- Look for extract by index rule
4790 else if ( (isset($p_options[PCLZIP_OPT_BY_INDEX]))
4791 && ($p_options[PCLZIP_OPT_BY_INDEX] != 0)) {
4793 // ----- Look if the index is in the list
4794 for ($j=$j_start; ($j<sizeof($p_options[PCLZIP_OPT_BY_INDEX])) && (!$v_found); $j++) {
4796 if (($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['start']) && ($i<=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end'])) {
4799 if ($i>=$p_options[PCLZIP_OPT_BY_INDEX][$j]['end']) {
4803 if ($p_options[PCLZIP_OPT_BY_INDEX][$j]['start']>$i) {
4812 // ----- Look for deletion
4815 unset($v_header_list[$v_nb_extracted]);
4823 // ----- Look if something need to be deleted
4824 if ($v_nb_extracted > 0) {
4826 // ----- Creates a temporay file
4827 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
4829 // ----- Creates a temporary zip archive
4830 $v_temp_zip = new PclZip($v_zip_temp_name);
4832 // ----- Open the temporary zip file in write mode
4833 if (($v_result = $v_temp_zip->privOpenFd('wb')) != 1) {
4834 $this->privCloseFd();
4840 // ----- Look which file need to be kept
4841 for ($i=0; $i<sizeof($v_header_list); $i++) {
4843 // ----- Calculate the position of the header
4844 @rewind($this->zip_fd);
4845 if (@fseek($this->zip_fd, $v_header_list[$i]['offset'])) {
4846 // ----- Close the zip file
4847 $this->privCloseFd();
4848 $v_temp_zip->privCloseFd();
4849 @unlink($v_zip_temp_name);
4852 PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
4855 return PclZip::errorCode();
4858 // ----- Read the file header
4859 $v_local_header = array();
4860 if (($v_result = $this->privReadFileHeader($v_local_header)) != 1) {
4861 // ----- Close the zip file
4862 $this->privCloseFd();
4863 $v_temp_zip->privCloseFd();
4864 @unlink($v_zip_temp_name);
4870 // ----- Check that local file header is same as central file header
4871 if ($this->privCheckFileHeaders($v_local_header,
4872 $v_header_list[$i]) != 1) {
4875 unset($v_local_header);
4877 // ----- Write the file header
4878 if (($v_result = $v_temp_zip->privWriteFileHeader($v_header_list[$i])) != 1) {
4879 // ----- Close the zip file
4880 $this->privCloseFd();
4881 $v_temp_zip->privCloseFd();
4882 @unlink($v_zip_temp_name);
4888 // ----- Read/write the data block
4889 if (($v_result = PclZipUtilCopyBlock($this->zip_fd, $v_temp_zip->zip_fd, $v_header_list[$i]['compressed_size'])) != 1) {
4890 // ----- Close the zip file
4891 $this->privCloseFd();
4892 $v_temp_zip->privCloseFd();
4893 @unlink($v_zip_temp_name);
4900 // ----- Store the offset of the central dir
4901 $v_offset = @ftell($v_temp_zip->zip_fd);
4903 // ----- Re-Create the Central Dir files header
4904 for ($i=0; $i<sizeof($v_header_list); $i++) {
4905 // ----- Create the file header
4906 if (($v_result = $v_temp_zip->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
4907 $v_temp_zip->privCloseFd();
4908 $this->privCloseFd();
4909 @unlink($v_zip_temp_name);
4915 // ----- Transform the header to a 'usable' info
4916 $v_temp_zip->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
4920 // ----- Zip file comment
4922 if (isset($p_options[PCLZIP_OPT_COMMENT])) {
4923 $v_comment = $p_options[PCLZIP_OPT_COMMENT];
4926 // ----- Calculate the size of the central header
4927 $v_size = @ftell($v_temp_zip->zip_fd)-$v_offset;
4929 // ----- Create the central dir footer
4930 if (($v_result = $v_temp_zip->privWriteCentralHeader(sizeof($v_header_list), $v_size, $v_offset, $v_comment)) != 1) {
4931 // ----- Reset the file list
4932 unset($v_header_list);
4933 $v_temp_zip->privCloseFd();
4934 $this->privCloseFd();
4935 @unlink($v_zip_temp_name);
4942 $v_temp_zip->privCloseFd();
4943 $this->privCloseFd();
4945 // ----- Delete the zip file
4946 // TBC : I should test the result ...
4947 @unlink($this->zipname);
4949 // ----- Rename the temporary file
4950 // TBC : I should test the result ...
4951 //@rename($v_zip_temp_name, $this->zipname);
4952 PclZipUtilRename($v_zip_temp_name, $this->zipname);
4954 // ----- Destroy the temporary archive
4958 // ----- Remove every files : reset the file
4959 else if ($v_central_dir['entries'] != 0) {
4960 $this->privCloseFd();
4962 if (($v_result = $this->privOpenFd('wb')) != 1) {
4966 if (($v_result = $this->privWriteCentralHeader(0, 0, 0, '')) != 1) {
4970 $this->privCloseFd();
4976 // --------------------------------------------------------------------------------
4978 // --------------------------------------------------------------------------------
4979 // Function : privDirCheck()
4981 // Check if a directory exists, if not it creates it and all the parents directory
4982 // which may be useful.
4984 // $p_dir : Directory path to check.
4987 // -1 : Unable to create directory
4988 // --------------------------------------------------------------------------------
4989 function privDirCheck($p_dir, $p_is_dir=false)
4994 // ----- Remove the final '/'
4995 if (($p_is_dir) && (substr($p_dir, -1)=='/'))
4997 $p_dir = substr($p_dir, 0, strlen($p_dir)-1);
5000 // ----- Check the directory availability
5001 if ((is_dir($p_dir)) || ($p_dir == ""))
5006 // ----- Extract parent directory
5007 $p_parent_dir = dirname($p_dir);
5009 // ----- Just a check
5010 if ($p_parent_dir != $p_dir)
5012 // ----- Look for parent directory
5013 if ($p_parent_dir != "")
5015 if (($v_result = $this->privDirCheck($p_parent_dir)) != 1)
5022 // ----- Create the directory
5023 if (!@mkdir($p_dir, 0777))
5026 PclZip::privErrorLog(PCLZIP_ERR_DIR_CREATE_FAIL, "Unable to create directory '$p_dir'");
5029 return PclZip::errorCode();
5035 // --------------------------------------------------------------------------------
5037 // --------------------------------------------------------------------------------
5038 // Function : privMerge()
5040 // If $p_archive_to_add does not exist, the function exit with a success result.
5043 // --------------------------------------------------------------------------------
5044 function privMerge(&$p_archive_to_add)
5048 // ----- Look if the archive_to_add exists
5049 if (!is_file($p_archive_to_add->zipname))
5052 // ----- Nothing to merge, so merge is a success
5059 // ----- Look if the archive exists
5060 if (!is_file($this->zipname))
5063 // ----- Do a duplicate
5064 $v_result = $this->privDuplicate($p_archive_to_add->zipname);
5070 // ----- Open the zip file
5071 if (($v_result=$this->privOpenFd('rb')) != 1)
5077 // ----- Read the central directory informations
5078 $v_central_dir = array();
5079 if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
5081 $this->privCloseFd();
5085 // ----- Go to beginning of File
5086 @rewind($this->zip_fd);
5088 // ----- Open the archive_to_add file
5089 if (($v_result=$p_archive_to_add->privOpenFd('rb')) != 1)
5091 $this->privCloseFd();
5097 // ----- Read the central directory informations
5098 $v_central_dir_to_add = array();
5099 if (($v_result = $p_archive_to_add->privReadEndCentralDir($v_central_dir_to_add)) != 1)
5101 $this->privCloseFd();
5102 $p_archive_to_add->privCloseFd();
5107 // ----- Go to beginning of File
5108 @rewind($p_archive_to_add->zip_fd);
5110 // ----- Creates a temporay file
5111 $v_zip_temp_name = PCLZIP_TEMPORARY_DIR.uniqid('pclzip-').'.tmp';
5113 // ----- Open the temporary file in write mode
5114 if (($v_zip_temp_fd = @fopen($v_zip_temp_name, 'wb')) == 0)
5116 $this->privCloseFd();
5117 $p_archive_to_add->privCloseFd();
5119 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open temporary file \''.$v_zip_temp_name.'\' in binary write mode');
5122 return PclZip::errorCode();
5125 // ----- Copy the files from the archive to the temporary file
5126 // TBC : Here I should better append the file and go back to erase the central dir
5127 $v_size = $v_central_dir['offset'];
5128 while ($v_size != 0)
5130 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5131 $v_buffer = fread($this->zip_fd, $v_read_size);
5132 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5133 $v_size -= $v_read_size;
5136 // ----- Copy the files from the archive_to_add into the temporary file
5137 $v_size = $v_central_dir_to_add['offset'];
5138 while ($v_size != 0)
5140 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5141 $v_buffer = fread($p_archive_to_add->zip_fd, $v_read_size);
5142 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5143 $v_size -= $v_read_size;
5146 // ----- Store the offset of the central dir
5147 $v_offset = @ftell($v_zip_temp_fd);
5149 // ----- Copy the block of file headers from the old archive
5150 $v_size = $v_central_dir['size'];
5151 while ($v_size != 0)
5153 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5154 $v_buffer = @fread($this->zip_fd, $v_read_size);
5155 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5156 $v_size -= $v_read_size;
5159 // ----- Copy the block of file headers from the archive_to_add
5160 $v_size = $v_central_dir_to_add['size'];
5161 while ($v_size != 0)
5163 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5164 $v_buffer = @fread($p_archive_to_add->zip_fd, $v_read_size);
5165 @fwrite($v_zip_temp_fd, $v_buffer, $v_read_size);
5166 $v_size -= $v_read_size;
5169 // ----- Merge the file comments
5170 $v_comment = $v_central_dir['comment'].' '.$v_central_dir_to_add['comment'];
5172 // ----- Calculate the size of the (new) central header
5173 $v_size = @ftell($v_zip_temp_fd)-$v_offset;
5175 // ----- Swap the file descriptor
5176 // Here is a trick : I swap the temporary fd with the zip fd, in order to use
5177 // the following methods on the temporary fil and not the real archive fd
5178 $v_swap = $this->zip_fd;
5179 $this->zip_fd = $v_zip_temp_fd;
5180 $v_zip_temp_fd = $v_swap;
5182 // ----- Create the central dir footer
5183 if (($v_result = $this->privWriteCentralHeader($v_central_dir['entries']+$v_central_dir_to_add['entries'], $v_size, $v_offset, $v_comment)) != 1)
5185 $this->privCloseFd();
5186 $p_archive_to_add->privCloseFd();
5187 @fclose($v_zip_temp_fd);
5188 $this->zip_fd = null;
5190 // ----- Reset the file list
5191 unset($v_header_list);
5197 // ----- Swap back the file descriptor
5198 $v_swap = $this->zip_fd;
5199 $this->zip_fd = $v_zip_temp_fd;
5200 $v_zip_temp_fd = $v_swap;
5203 $this->privCloseFd();
5204 $p_archive_to_add->privCloseFd();
5206 // ----- Close the temporary file
5207 @fclose($v_zip_temp_fd);
5209 // ----- Delete the zip file
5210 // TBC : I should test the result ...
5211 @unlink($this->zipname);
5213 // ----- Rename the temporary file
5214 // TBC : I should test the result ...
5215 //@rename($v_zip_temp_name, $this->zipname);
5216 PclZipUtilRename($v_zip_temp_name, $this->zipname);
5221 // --------------------------------------------------------------------------------
5223 // --------------------------------------------------------------------------------
5224 // Function : privDuplicate()
5228 // --------------------------------------------------------------------------------
5229 function privDuplicate($p_archive_filename)
5233 // ----- Look if the $p_archive_filename exists
5234 if (!is_file($p_archive_filename))
5237 // ----- Nothing to duplicate, so duplicate is a success.
5244 // ----- Open the zip file
5245 if (($v_result=$this->privOpenFd('wb')) != 1)
5251 // ----- Open the temporary file in write mode
5252 if (($v_zip_temp_fd = @fopen($p_archive_filename, 'rb')) == 0)
5254 $this->privCloseFd();
5256 PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive file \''.$p_archive_filename.'\' in binary write mode');
5259 return PclZip::errorCode();
5262 // ----- Copy the files from the archive to the temporary file
5263 // TBC : Here I should better append the file and go back to erase the central dir
5264 $v_size = filesize($p_archive_filename);
5265 while ($v_size != 0)
5267 $v_read_size = ($v_size < PCLZIP_READ_BLOCK_SIZE ? $v_size : PCLZIP_READ_BLOCK_SIZE);
5268 $v_buffer = fread($v_zip_temp_fd, $v_read_size);
5269 @fwrite($this->zip_fd, $v_buffer, $v_read_size);
5270 $v_size -= $v_read_size;
5274 $this->privCloseFd();
5276 // ----- Close the temporary file
5277 @fclose($v_zip_temp_fd);
5282 // --------------------------------------------------------------------------------
5284 // --------------------------------------------------------------------------------
5285 // Function : privErrorLog()
5288 // --------------------------------------------------------------------------------
5289 function privErrorLog($p_error_code=0, $p_error_string='')
5291 if (PCLZIP_ERROR_EXTERNAL == 1) {
5292 PclError($p_error_code, $p_error_string);
5295 $this->error_code = $p_error_code;
5296 $this->error_string = $p_error_string;
5299 // --------------------------------------------------------------------------------
5301 // --------------------------------------------------------------------------------
5302 // Function : privErrorReset()
5305 // --------------------------------------------------------------------------------
5306 function privErrorReset()
5308 if (PCLZIP_ERROR_EXTERNAL == 1) {
5312 $this->error_code = 0;
5313 $this->error_string = '';
5316 // --------------------------------------------------------------------------------
5318 // --------------------------------------------------------------------------------
5319 // Function : privDisableMagicQuotes()
5323 // --------------------------------------------------------------------------------
5324 function privDisableMagicQuotes()
5328 // ----- Look if function exists
5329 if ( (!function_exists("get_magic_quotes_runtime"))
5330 || (!function_exists("set_magic_quotes_runtime"))) {
5334 // ----- Look if already done
5335 if ($this->magic_quotes_status != -1) {
5339 // ----- Get and memorize the magic_quote value
5340 $this->magic_quotes_status = @get_magic_quotes_runtime();
5342 // ----- Disable magic_quotes
5343 if ($this->magic_quotes_status == 1) {
5344 @set_magic_quotes_runtime(0);
5350 // --------------------------------------------------------------------------------
5352 // --------------------------------------------------------------------------------
5353 // Function : privSwapBackMagicQuotes()
5357 // --------------------------------------------------------------------------------
5358 function privSwapBackMagicQuotes()
5362 // ----- Look if function exists
5363 if ( (!function_exists("get_magic_quotes_runtime"))
5364 || (!function_exists("set_magic_quotes_runtime"))) {
5368 // ----- Look if something to do
5369 if ($this->magic_quotes_status != -1) {
5373 // ----- Swap back magic_quotes
5374 if ($this->magic_quotes_status == 1) {
5375 @set_magic_quotes_runtime($this->magic_quotes_status);
5381 // --------------------------------------------------------------------------------
5385 // --------------------------------------------------------------------------------
5387 // --------------------------------------------------------------------------------
5388 // Function : PclZipUtilPathReduction()
5392 // --------------------------------------------------------------------------------
5393 function PclZipUtilPathReduction($p_dir)
5397 // ----- Look for not empty path
5399 // ----- Explode path by directory names
5400 $v_list = explode("/", $p_dir);
5402 // ----- Study directories from last to first
5404 for ($i=sizeof($v_list)-1; $i>=0; $i--) {
5405 // ----- Look for current path
5406 if ($v_list[$i] == ".") {
5407 // ----- Ignore this directory
5408 // Should be the first $i=0, but no check is done
5410 else if ($v_list[$i] == "..") {
5413 else if ($v_list[$i] == "") {
5414 // ----- First '/' i.e. root slash
5416 $v_result = "/".$v_result;
5418 // ----- It is an invalid path, so the path is not modified
5424 // ----- Last '/' i.e. indicates a directory
5425 else if ($i == (sizeof($v_list)-1)) {
5426 $v_result = $v_list[$i];
5428 // ----- Double '/' inside the path
5430 // ----- Ignore only the double '//' in path,
5431 // but not the first and last '/'
5435 // ----- Look for item to skip
5440 $v_result = $v_list[$i].($i!=(sizeof($v_list)-1)?"/".$v_result:"");
5445 // ----- Look for skip
5447 while ($v_skip > 0) {
5448 $v_result = '../'.$v_result;
5457 // --------------------------------------------------------------------------------
5459 // --------------------------------------------------------------------------------
5460 // Function : PclZipUtilPathInclusion()
5462 // This function indicates if the path $p_path is under the $p_dir tree. Or,
5463 // said in an other way, if the file or sub-dir $p_path is inside the dir
5465 // The function indicates also if the path is exactly the same as the dir.
5466 // This function supports path with duplicated '/' like '//', but does not
5467 // support '.' or '..' statements.
5470 // 0 if $p_path is not inside directory $p_dir
5471 // 1 if $p_path is inside directory $p_dir
5472 // 2 if $p_path is exactly the same as $p_dir
5473 // --------------------------------------------------------------------------------
5474 function PclZipUtilPathInclusion($p_dir, $p_path)
5478 // ----- Look for path beginning by ./
5479 if ( ($p_dir == '.')
5480 || ((strlen($p_dir) >=2) && (substr($p_dir, 0, 2) == './'))) {
5481 $p_dir = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_dir, 1);
5483 if ( ($p_path == '.')
5484 || ((strlen($p_path) >=2) && (substr($p_path, 0, 2) == './'))) {
5485 $p_path = PclZipUtilTranslateWinPath(getcwd(), FALSE).'/'.substr($p_path, 1);
5488 // ----- Explode dir and path by directory separator
5489 $v_list_dir = explode("/", $p_dir);
5490 $v_list_dir_size = sizeof($v_list_dir);
5491 $v_list_path = explode("/", $p_path);
5492 $v_list_path_size = sizeof($v_list_path);
5494 // ----- Study directories paths
5497 while (($i < $v_list_dir_size) && ($j < $v_list_path_size) && ($v_result)) {
5499 // ----- Look for empty dir (path reduction)
5500 if ($v_list_dir[$i] == '') {
5504 if ($v_list_path[$j] == '') {
5509 // ----- Compare the items
5510 if (($v_list_dir[$i] != $v_list_path[$j]) && ($v_list_dir[$i] != '') && ( $v_list_path[$j] != '')) {
5519 // ----- Look if everything seems to be the same
5521 // ----- Skip all the empty items
5522 while (($j < $v_list_path_size) && ($v_list_path[$j] == '')) $j++;
5523 while (($i < $v_list_dir_size) && ($v_list_dir[$i] == '')) $i++;
5525 if (($i >= $v_list_dir_size) && ($j >= $v_list_path_size)) {
5526 // ----- There are exactly the same
5529 else if ($i < $v_list_dir_size) {
5530 // ----- The path is shorter than the dir
5538 // --------------------------------------------------------------------------------
5540 // --------------------------------------------------------------------------------
5541 // Function : PclZipUtilCopyBlock()
5544 // $p_mode : read/write compression mode
5545 // 0 : src & dest normal
5546 // 1 : src gzip, dest normal
5547 // 2 : src normal, dest gzip
5548 // 3 : src & dest gzip
5550 // --------------------------------------------------------------------------------
5551 function PclZipUtilCopyBlock($p_src, $p_dest, $p_size, $p_mode=0)
5557 while ($p_size != 0)
5559 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5560 $v_buffer = @fread($p_src, $v_read_size);
5561 @fwrite($p_dest, $v_buffer, $v_read_size);
5562 $p_size -= $v_read_size;
5565 else if ($p_mode==1)
5567 while ($p_size != 0)
5569 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5570 $v_buffer = @gzread($p_src, $v_read_size);
5571 @fwrite($p_dest, $v_buffer, $v_read_size);
5572 $p_size -= $v_read_size;
5575 else if ($p_mode==2)
5577 while ($p_size != 0)
5579 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5580 $v_buffer = @fread($p_src, $v_read_size);
5581 @gzwrite($p_dest, $v_buffer, $v_read_size);
5582 $p_size -= $v_read_size;
5585 else if ($p_mode==3)
5587 while ($p_size != 0)
5589 $v_read_size = ($p_size < PCLZIP_READ_BLOCK_SIZE ? $p_size : PCLZIP_READ_BLOCK_SIZE);
5590 $v_buffer = @gzread($p_src, $v_read_size);
5591 @gzwrite($p_dest, $v_buffer, $v_read_size);
5592 $p_size -= $v_read_size;
5599 // --------------------------------------------------------------------------------
5601 // --------------------------------------------------------------------------------
5602 // Function : PclZipUtilRename()
5604 // This function tries to do a simple rename() function. If it fails, it
5605 // tries to copy the $p_src file in a new $p_dest file and then unlink the
5608 // $p_src : Old filename
5609 // $p_dest : New filename
5611 // 1 on success, 0 on failure.
5612 // --------------------------------------------------------------------------------
5613 function PclZipUtilRename($p_src, $p_dest)
5617 // ----- Try to rename the files
5618 if (!@rename($p_src, $p_dest)) {
5620 // ----- Try to copy & unlink the src
5621 if (!@copy($p_src, $p_dest)) {
5624 else if (!@unlink($p_src)) {
5632 // --------------------------------------------------------------------------------
5634 // --------------------------------------------------------------------------------
5635 // Function : PclZipUtilOptionText()
5637 // Translate option value in text. Mainly for debug purpose.
5639 // $p_option : the option value.
5641 // The option text value.
5642 // --------------------------------------------------------------------------------
5643 function PclZipUtilOptionText($p_option)
5646 $v_list = get_defined_constants();
5647 for (reset($v_list); $v_key = key($v_list); next($v_list)) {
5648 $v_prefix = substr($v_key, 0, 10);
5649 if (( ($v_prefix == 'PCLZIP_OPT')
5650 || ($v_prefix == 'PCLZIP_CB_')
5651 || ($v_prefix == 'PCLZIP_ATT'))
5652 && ($v_list[$v_key] == $p_option)) {
5657 $v_result = 'Unknown';
5661 // --------------------------------------------------------------------------------
5663 // --------------------------------------------------------------------------------
5664 // Function : PclZipUtilTranslateWinPath()
5666 // Translate windows path by replacing '\' by '/' and optionally removing
5669 // $p_path : path to translate.
5670 // $p_remove_disk_letter : true | false
5672 // The path translated.
5673 // --------------------------------------------------------------------------------
5674 function PclZipUtilTranslateWinPath($p_path, $p_remove_disk_letter=true)
5676 if (stristr(php_uname(), 'windows')) {
5677 // ----- Look for potential disk letter
5678 if (($p_remove_disk_letter) && (($v_position = strpos($p_path, ':')) != false)) {
5679 $p_path = substr($p_path, $v_position+1);
5681 // ----- Change potential windows directory separator
5682 if ((strpos($p_path, '\\') > 0) || (substr($p_path, 0,1) == '\\')) {
5683 $p_path = strtr($p_path, '\\', '/');
5688 // --------------------------------------------------------------------------------