📄 pclzip.php
字号:
}
$this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
}
$v_comment = $v_central_dir['comment'];
if (isset($p_options[PCLZIP_OPT_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_COMMENT];
}
if (isset($p_options[PCLZIP_OPT_ADD_COMMENT])) {
$v_comment = $v_comment.$p_options[PCLZIP_OPT_ADD_COMMENT];
}
if (isset($p_options[PCLZIP_OPT_PREPEND_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_PREPEND_COMMENT].$v_comment;
}
$v_size = @ftell($this->zip_fd)-$v_offset;
if (($v_result = $this->privWriteCentralHeader($v_count+$v_central_dir['entries'], $v_size, $v_offset, $v_comment)) != 1)
{
unset($v_header_list);
return $v_result;
}
$v_swap = $this->zip_fd;
$this->zip_fd = $v_zip_temp_fd;
$v_zip_temp_fd = $v_swap;
$this->privCloseFd();
@fclose($v_zip_temp_fd);
@unlink($this->zipname);
PclZipUtilRename($v_zip_temp_name, $this->zipname);
return $v_result;
}
function privOpenFd($p_mode)
{
$v_result=1;
if ($this->zip_fd != 0)
{
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Zip file \''.$this->zipname.'\' already open');
return PclZip::errorCode();
}
if (($this->zip_fd = @fopen($this->zipname, $p_mode)) == 0)
{
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in '.$p_mode.' mode');
return PclZip::errorCode();
}
return $v_result;
}
function privCloseFd()
{
$v_result=1;
if ($this->zip_fd != 0)
@fclose($this->zip_fd);
$this->zip_fd = 0;
return $v_result;
}
function privAddList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
{
$v_result=1;
$v_header_list = array();
if (($v_result = $this->privAddFileList($p_list, $v_header_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
{
return $v_result;
}
$v_offset = @ftell($this->zip_fd);
for ($i=0,$v_count=0; $i<sizeof($v_header_list); $i++)
{
if ($v_header_list[$i]['status'] == 'ok') {
if (($v_result = $this->privWriteCentralFileHeader($v_header_list[$i])) != 1) {
return $v_result;
}
$v_count++;
}
$this->privConvertHeader2FileInfo($v_header_list[$i], $p_result_list[$i]);
}
$v_comment = '';
if (isset($p_options[PCLZIP_OPT_COMMENT])) {
$v_comment = $p_options[PCLZIP_OPT_COMMENT];
}
$v_size = @ftell($this->zip_fd)-$v_offset;
if (($v_result = $this->privWriteCentralHeader($v_count, $v_size, $v_offset, $v_comment)) != 1)
{
unset($v_header_list);
return $v_result;
}
return $v_result;
}
function privAddFileList($p_list, &$p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
{
$v_result=1;
$v_header = array();
$v_nb = sizeof($p_result_list);
for ($j=0; ($j<count($p_list)) && ($v_result==1); $j++)
{
$p_filename = PclZipUtilTranslateWinPath($p_list[$j], false);
if ($p_filename == "")
{
continue;
}
if (!file_exists($p_filename))
{
PclZip::privErrorLog(PCLZIP_ERR_MISSING_FILE, "File '$p_filename' does not exists");
return PclZip::errorCode();
}
if ((is_file($p_filename)) || ((is_dir($p_filename)) && !$p_remove_all_dir)) {
if (($v_result = $this->privAddFile($p_filename, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1)
{
return $v_result;
}
$p_result_list[$v_nb++] = $v_header;
}
if (@is_dir($p_filename))
{
if ($p_filename != ".")
$v_path = $p_filename."/";
else
$v_path = "";
if ($p_hdir = @opendir($p_filename)) {
$p_hitem = @readdir($p_hdir);
$p_hitem = @readdir($p_hdir);
while (($p_hitem = @readdir($p_hdir)) !== false) {
if (is_file($v_path.$p_hitem)) {
if (($v_result = $this->privAddFile($v_path.$p_hitem, $v_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options)) != 1) {
return $v_result;
}
$p_result_list[$v_nb++] = $v_header;
}
else {
$p_temp_list[0] = $v_path.$p_hitem;
$v_result = $this->privAddFileList($p_temp_list, $p_result_list, $p_add_dir, $p_remove_dir, $p_remove_all_dir, $p_options);
$v_nb = sizeof($p_result_list);
}
}
}
unset($p_temp_list);
unset($p_hdir);
unset($p_hitem);
}
}
return $v_result;
}
function privAddFile($p_filename, &$p_header, $p_add_dir, $p_remove_dir, $p_remove_all_dir, &$p_options)
{
$v_result=1;
if ($p_filename == "")
{
PclZip::privErrorLog(PCLZIP_ERR_INVALID_PARAMETER, "Invalid file list parameter (invalid or empty list)");
return PclZip::errorCode();
}
$v_stored_filename = $p_filename;
if ($p_remove_all_dir) {
$v_stored_filename = basename($p_filename);
}
else if ($p_remove_dir != "")
{
if (substr($p_remove_dir, -1) != '/')
$p_remove_dir .= "/";
if ((substr($p_filename, 0, 2) == "./") || (substr($p_remove_dir, 0, 2) == "./"))
{
if ((substr($p_filename, 0, 2) == "./") && (substr($p_remove_dir, 0, 2) != "./"))
$p_remove_dir = "./".$p_remove_dir;
if ((substr($p_filename, 0, 2) != "./") && (substr($p_remove_dir, 0, 2) == "./"))
$p_remove_dir = substr($p_remove_dir, 2);
}
$v_compare = PclZipUtilPathInclusion($p_remove_dir, $p_filename);
if ($v_compare > 0)
{
if ($v_compare == 2) {
$v_stored_filename = "";
}
else {
$v_stored_filename = substr($p_filename, strlen($p_remove_dir));
}
}
}
if ($p_add_dir != "")
{
if (substr($p_add_dir, -1) == "/")
$v_stored_filename = $p_add_dir.$v_stored_filename;
else
$v_stored_filename = $p_add_dir."/".$v_stored_filename;
}
$v_stored_filename = PclZipUtilPathReduction($v_stored_filename);
clearstatcache();
$p_header['version'] = 20;
$p_header['version_extracted'] = 10;
$p_header['flag'] = 0;
$p_header['compression'] = 0;
$p_header['mtime'] = filemtime($p_filename);
$p_header['crc'] = 0;
$p_header['compressed_size'] = 0;
$p_header['size'] = filesize($p_filename);
$p_header['filename_len'] = strlen($p_filename);
$p_header['extra_len'] = 0;
$p_header['comment_len'] = 0;
$p_header['disk'] = 0;
$p_header['internal'] = 0;
$p_header['external'] = (is_file($p_filename)?0x00000000:0x00000010);
$p_header['offset'] = 0;
$p_header['filename'] = $p_filename;
$p_header['stored_filename'] = $v_stored_filename;
$p_header['extra'] = '';
$p_header['comment'] = '';
$p_header['status'] = 'ok';
$p_header['index'] = -1;
if (isset($p_options[PCLZIP_CB_PRE_ADD])) {
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_header, $v_local_header);
eval('$v_result = '.$p_options[PCLZIP_CB_PRE_ADD].'(PCLZIP_CB_PRE_ADD, $v_local_header);');
if ($v_result == 0) {
$p_header['status'] = "skipped";
$v_result = 1;
}
if ($p_header['stored_filename'] != $v_local_header['stored_filename']) {
$p_header['stored_filename'] = PclZipUtilPathReduction($v_local_header['stored_filename']);
}
}
if ($p_header['stored_filename'] == "") {
$p_header['status'] = "filtered";
}
if (strlen($p_header['stored_filename']) > 0xFF) {
$p_header['status'] = 'filename_too_long';
}
if ($p_header['status'] == 'ok') {
if (is_file($p_filename))
{
if (($v_file = @fopen($p_filename, "rb")) == 0) {
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, "Unable to open file '$p_filename' in binary read mode");
return PclZip::errorCode();
}
if ($p_options[PCLZIP_OPT_NO_COMPRESSION]) {
$v_content_compressed = @fread($v_file, $p_header['size']);
$p_header['crc'] = @crc32($v_content_compressed);
$p_header['compressed_size'] = $p_header['size'];
$p_header['compression'] = 0;
}
else {
$v_content = @fread($v_file, $p_header['size']);
$p_header['crc'] = @crc32($v_content);
$v_content_compressed = @gzdeflate($v_content);
$p_header['compressed_size'] = strlen($v_content_compressed);
$p_header['compression'] = 8;
}
if (($v_result = $this->privWriteFileHeader($p_header)) != 1) {
@fclose($v_file);
return $v_result;
}
$v_binary_data = pack('a'.$p_header['compressed_size'],
$v_content_compressed);
@fwrite($this->zip_fd, $v_binary_data, $p_header['compressed_size']);
@fclose($v_file);
}
else {
if (@substr($p_header['stored_filename'], -1) != '/') {
$p_header['stored_filename'] .= '/';
}
$p_header['size'] = 0;
$p_header['external'] = 0x00000010;
if (($v_result = $this->privWriteFileHeader($p_header)) != 1)
{
return $v_result;
}
}
}
if (isset($p_options[PCLZIP_CB_POST_ADD])) {
$v_local_header = array();
$this->privConvertHeader2FileInfo($p_header, $v_local_header);
eval('$v_result = '.$p_options[PCLZIP_CB_POST_ADD].'(PCLZIP_CB_POST_ADD, $v_local_header);');
if ($v_result == 0) {
$v_result = 1;
}
}
return $v_result;
}
function privWriteFileHeader(&$p_header)
{
$v_result=1;
$p_header['offset'] = ftell($this->zip_fd);
$v_date = getdate($p_header['mtime']);
$v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
$v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
$v_binary_data = pack("VvvvvvVVVvv", 0x04034b50,
$p_header['version_extracted'], $p_header['flag'],
$p_header['compression'], $v_mtime, $v_mdate,
$p_header['crc'], $p_header['compressed_size'],
$p_header['size'],
strlen($p_header['stored_filename']),
$p_header['extra_len']);
fputs($this->zip_fd, $v_binary_data, 30);
if (strlen($p_header['stored_filename']) != 0)
{
fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
}
if ($p_header['extra_len'] != 0)
{
fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
}
return $v_result;
}
function privWriteCentralFileHeader(&$p_header)
{
$v_result=1;
$v_date = getdate($p_header['mtime']);
$v_mtime = ($v_date['hours']<<11) + ($v_date['minutes']<<5) + $v_date['seconds']/2;
$v_mdate = (($v_date['year']-1980)<<9) + ($v_date['mon']<<5) + $v_date['mday'];
$v_binary_data = pack("VvvvvvvVVVvvvvvVV", 0x02014b50,
$p_header['version'], $p_header['version_extracted'],
$p_header['flag'], $p_header['compression'],
$v_mtime, $v_mdate, $p_header['crc'],
$p_header['compressed_size'], $p_header['size'],
strlen($p_header['stored_filename']),
$p_header['extra_len'], $p_header['comment_len'],
$p_header['disk'], $p_header['internal'],
$p_header['external'], $p_header['offset']);
fputs($this->zip_fd, $v_binary_data, 46);
if (strlen($p_header['stored_filename']) != 0)
{
fputs($this->zip_fd, $p_header['stored_filename'], strlen($p_header['stored_filename']));
}
if ($p_header['extra_len'] != 0)
{
fputs($this->zip_fd, $p_header['extra'], $p_header['extra_len']);
}
if ($p_header['comment_len'] != 0)
{
fputs($this->zip_fd, $p_header['comment'], $p_header['comment_len']);
}
return $v_result;
}
function privWriteCentralHeader($p_nb_entries, $p_size, $p_offset, $p_comment)
{
$v_result=1;
$v_binary_data = pack("VvvvvVVv", 0x06054b50, 0, 0, $p_nb_entries,
$p_nb_entries, $p_size,
$p_offset, strlen($p_comment));
fputs($this->zip_fd, $v_binary_data, 22);
if (strlen($p_comment) != 0)
{
fputs($this->zip_fd, $p_comment, strlen($p_comment));
}
return $v_result;
}
function privList(&$p_list)
{
$v_result=1;
if (($this->zip_fd = @fopen($this->zipname, 'rb')) == 0)
{
PclZip::privErrorLog(PCLZIP_ERR_READ_OPEN_FAIL, 'Unable to open archive \''.$this->zipname.'\' in binary read mode');
return PclZip::errorCode();
}
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
{
return $v_result;
}
@rewind($this->zip_fd);
if (@fseek($this->zip_fd, $v_central_dir['offset']))
{
PclZip::privErrorLog(PCLZIP_ERR_INVALID_ARCHIVE_ZIP, 'Invalid archive size');
return PclZip::errorCode();
}
for ($i=0; $i<$v_central_dir['entries']; $i++)
{
if (($v_result = $this->privReadCentralFileHeader($v_header)) != 1)
{
return $v_result;
}
$v_header['index'] = $i;
$this->privConvertHeader2FileInfo($v_header, $p_list[$i]);
unset($v_header);
}
$this->privCloseFd();
return $v_result;
}
function privConvertHeader2FileInfo($p_header, &$p_info)
{
$v_result=1;
$p_info['filename'] = $p_header['filename'];
$p_info['stored_filename'] = $p_header['stored_filename'];
$p_info['size'] = $p_header['size'];
$p_info['compressed_size'] = $p_header['compressed_size'];
$p_info['mtime'] = $p_header['mtime'];
$p_info['comment'] = $p_header['comment'];
$p_info['folder'] = (($p_header['external']&0x00000010)==0x00000010);
$p_info['index'] = $p_header['index'];
$p_info['status'] = $p_header['status'];
return $v_result;
}
function privExtractByRule(&$p_file_list, $p_path, $p_remove_path, $p_remove_all_path, &$p_options)
{
$v_result=1;
if (($p_path == "") || ((substr($p_path, 0, 1) != "/") && (substr($p_path, 0, 3) != "../") && (substr($p_path,1,2)!=":/")))
$p_path = "./".$p_path;
if (($p_path != "./") && ($p_path != "/"))
{
while (substr($p_path, -1) == "/")
{
$p_path = substr($p_path, 0, strlen($p_path)-1);
}
}
if (($p_remove_path != "") && (substr($p_remove_path, -1) != '/'))
{
$p_remove_path .= '/';
}
$p_remove_path_size = strlen($p_remove_path);
if (($v_result = $this->privOpenFd('rb')) != 1)
{
return $v_result;
}
$v_central_dir = array();
if (($v_result = $this->privReadEndCentralDir($v_central_dir)) != 1)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -