<?php
/* vim: set ts=4 sw=4: */
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Vincent Blavet <vincent@blavet.net> |
// +----------------------------------------------------------------------+
//
// $Id: Zip.php 3076 2006-12-18 08:52:12Z fabien $
// ----- Constants
define( 'ARCHIVE_ZIP_READ_BLOCK_SIZE', 2048 );
// ----- File list separator
define( 'ARCHIVE_ZIP_SEPARATOR', ',' );
// ----- Optional static temporary directory
// By default temporary files are generated in the script current
// path.
// If defined :
// - MUST BE terminated by a '/'.
// - MUST be a valid, already created directory
// Samples :
// define( 'ARCHIVE_ZIP_TEMPORARY_DIR', '/temp/' );
// define( 'ARCHIVE_ZIP_TEMPORARY_DIR', 'C:/Temp/' );
define( 'ARCHIVE_ZIP_TEMPORARY_DIR', '' );
// ----- Error codes
define( 'ARCHIVE_ZIP_ERR_NO_ERROR', 0 );
define( 'ARCHIVE_ZIP_ERR_WRITE_OPEN_FAIL', -1 );
define( 'ARCHIVE_ZIP_ERR_READ_OPEN_FAIL', -2 );
define( 'ARCHIVE_ZIP_ERR_INVALID_PARAMETER', -3 );
define( 'ARCHIVE_ZIP_ERR_MISSING_FILE', -4 );
define( 'ARCHIVE_ZIP_ERR_FILENAME_TOO_LONG', -5 );
define( 'ARCHIVE_ZIP_ERR_INVALID_ZIP', -6 );
define( 'ARCHIVE_ZIP_ERR_BAD_EXTRACTED_FILE', -7 );
define( 'ARCHIVE_ZIP_ERR_DIR_CREATE_FAIL', -8 );
define( 'ARCHIVE_ZIP_ERR_BAD_EXTENSION', -9 );
define( 'ARCHIVE_ZIP_ERR_BAD_FORMAT', -10 );
define( 'ARCHIVE_ZIP_ERR_DELETE_FILE_FAIL', -11 );
define( 'ARCHIVE_ZIP_ERR_RENAME_FILE_FAIL', -12 );
define( 'ARCHIVE_ZIP_ERR_BAD_CHECKSUM', -13 );
define( 'ARCHIVE_ZIP_ERR_INVALID_ARCHIVE_ZIP', -14 );
define( 'ARCHIVE_ZIP_ERR_MISSING_OPTION_VALUE', -15 );
define( 'ARCHIVE_ZIP_ERR_INVALID_PARAM_VALUE', -16 );
// ----- Warning codes
define( 'ARCHIVE_ZIP_WARN_NO_WARNING', 0 );
define( 'ARCHIVE_ZIP_WARN_FILE_EXIST', 1 );
// ----- Methods parameters
define( 'ARCHIVE_ZIP_PARAM_PATH', 'path' );
define( 'ARCHIVE_ZIP_PARAM_ADD_PATH', 'add_path' );
define( 'ARCHIVE_ZIP_PARAM_REMOVE_PATH', 'remove_path' );
define( 'ARCHIVE_ZIP_PARAM_REMOVE_ALL_PATH', 'remove_all_path' );
define( 'ARCHIVE_ZIP_PARAM_SET_CHMOD', 'set_chmod' );
define( 'ARCHIVE_ZIP_PARAM_EXTRACT_AS_STRING', 'extract_as_string' );
define( 'ARCHIVE_ZIP_PARAM_NO_COMPRESSION', 'no_compression' );
define( 'ARCHIVE_ZIP_PARAM_BY_NAME', 'by_name' );
define( 'ARCHIVE_ZIP_PARAM_BY_INDEX', 'by_index' );
define( 'ARCHIVE_ZIP_PARAM_BY_EREG', 'by_ereg' );
define( 'ARCHIVE_ZIP_PARAM_BY_PREG', 'by_preg' );
define( 'ARCHIVE_ZIP_PARAM_PRE_EXTRACT', 'callback_pre_extract' );
define( 'ARCHIVE_ZIP_PARAM_POST_EXTRACT', 'callback_post_extract' );
define( 'ARCHIVE_ZIP_PARAM_PRE_ADD', 'callback_pre_add' );
define( 'ARCHIVE_ZIP_PARAM_POST_ADD', 'callback_post_add' );
/**
* Class for manipulating zip archive files
*
* A class which provided common methods to manipulate ZIP formatted
* archive files.
* It provides creation, extraction, deletion and add features.
*
* @author Vincent Blavet <vincent@blavet.net>
* @version $Revision: 1.3 $
* @package phing.lib
*/
class Archive_Zip
{
/**
* The filename of the zip archive.
*
* @var string Name of the Zip file
*/
var $_zipname='';
/**
* File descriptor of the opened Zip file.
*
* @var int Internal zip file descriptor
*/
var $_zip_fd=0;
/**
* @var int last error code
*/
var $_error_code=1;
/**
* @var string Last error description
*/
var $_error_string='';
// {{{ constructor
/**
* Archive_Zip Class constructor. This flavour of the constructor only
* declare a new Archive_Zip object, identifying it by the name of the
* zip file.
*
* @param string $p_zipname The name of the zip archive to create
* @access public
*/
function __construct($p_zipname)
{
if (!extension_loaded('zlib')) {
throw new Exception("The extension 'zlib' couldn't be found.\n".
"Please make sure your version of PHP was built ".
"with 'zlib' support.");
}
// ----- Set the attributes
$this->_zipname = $p_zipname;
$this->_zip_fd = 0;
}
// }}}
// {{{ create()
/**
* This method creates a Zip Archive with the filename set with
* the constructor.
* The files and directories indicated in $p_filelist
* are added in the archive.
* When a directory is in the list, the directory and its content is added
* in the archive.
* The methods takes a variable list of parameters in $p_params.
* The supported parameters for this method are :
* 'add_path' : Add a path to the archived files.
* 'remove_path' : Remove the specified 'root' path of the archived files.
* 'remove_all_path' : Remove all the path of the archived files.
* 'no_compression' : The archived files will not be compressed.
*
* @access public
* @param mixed $p_filelist The list of the files or folders to add.
* It can be a string with filenames separated
* by a comma, or an array of filenames.
* @param mixed $p_params An array of variable parameters and values.
* @return mixed An array of file description on success,
* an error code on error
*/
function create($p_filelist, $p_params=0)
{
$this->_errorReset();
// ----- Set default values
if ($p_params === 0) {
$p_params = array();
}
if ($this->_check_parameters($p_params,
array('no_compression' => false,
'add_path' => "",
'remove_path' => "",
'remove_all_path' => false)) != 1) {
return 0;
}
// ----- Look if the $p_filelist is really an array
$p_result_list = array();
if (is_array($p_filelist)) {
$v_result = $this->_create($p_filelist, $p_result_list, $p_params);
}
// ----- Look if the $p_filelist is a string
else if (is_string($p_filelist)) {
// ----- Create a list with the elements from the string
$v_list = explode(ARCHIVE_ZIP_SEPARATOR, $p_filelist);
$v_result = $this->_create($v_list, $p_result_list, $p_params);
}
// ----- Invalid variable
else {
$this->_errorLog(ARCHIVE_ZIP_ERR_INVALID_PARAMETER,
'Invalid variable type p_filelist');
$v_result = ARCHIVE_ZIP_ERR_INVALID_PARAMETER;
}
if ($v_result != 1) {
return 0;
}
return $p_result_list;
}
// }}}
// {{{ add()
/**
* This method add files or directory in an existing Zip Archive.
* If the Zip Archive does not exist it is created.
* The files and directories to add are indicated in $p_filelist.
* When a directory is in the list, the directory and its cont
评论0