<?php
/*
TbsZip version 2.15
Date : 2013-10-16
Author : Skrol29 (email: http://www.tinybutstrong.com/onlyyou.html)
Licence : LGPL
This class is independent from any other classes and has been originally created for the OpenTbs plug-in
for TinyButStrong Template Engine (TBS). OpenTbs makes TBS able to merge OpenOffice and Ms Office documents.
Visit http://www.tinybutstrong.com
*/
define('TBSZIP_DOWNLOAD',1); // download (default)
define('TBSZIP_NOHEADER',4); // option to use with DOWNLOAD: no header is sent
define('TBSZIP_FILE',8); // output to file , or add from file
define('TBSZIP_STRING',32); // output to string, or add from string
class clsTbsZip {
function __construct() {
$this->Meth8Ok = extension_loaded('zlib'); // check if Zlib extension is available. This is need for compress and uncompress with method 8.
$this->DisplayError = true;
$this->ArchFile = '';
$this->Error = false;
}
function CreateNew($ArchName='new.zip') {
// Create a new virtual empty archive, the name will be the default name when the archive is flushed.
if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility
$this->Close(); // note that $this->ArchHnd is set to false here
$this->Error = false;
$this->ArchFile = $ArchName;
$this->ArchIsNew = true;
$bin = 'PK'.chr(05).chr(06).str_repeat(chr(0), 18);
$this->CdEndPos = strlen($bin) - 4;
$this->CdInfo = array('disk_num_curr'=>0, 'disk_num_cd'=>0, 'file_nbr_curr'=>0, 'file_nbr_tot'=>0, 'l_cd'=>0, 'p_cd'=>0, 'l_comm'=>0, 'v_comm'=>'', 'bin'=>$bin);
$this->CdPos = $this->CdInfo['p_cd'];
}
function Open($ArchFile, $UseIncludePath=false) {
// Open the zip archive
if (!isset($this->Meth8Ok)) $this->__construct(); // for PHP 4 compatibility
$this->Close(); // close handle and init info
$this->Error = false;
$this->ArchIsNew = false;
$this->ArchIsStream = (is_resource($ArchFile) && (get_resource_type($ArchFile)=='stream'));
if ($this->ArchIsStream) {
$this->ArchFile = 'from_stream.zip';
$this->ArchHnd = $ArchFile;
} else {
// open the file
$this->ArchFile = $ArchFile;
$this->ArchHnd = fopen($ArchFile, 'rb', $UseIncludePath);
}
$ok = !($this->ArchHnd===false);
if ($ok) $ok = $this->CentralDirRead();
return $ok;
}
function Close() {
if (isset($this->ArchHnd) and ($this->ArchHnd!==false)) fclose($this->ArchHnd);
$this->ArchFile = '';
$this->ArchHnd = false;
$this->CdInfo = array();
$this->CdFileLst = array();
$this->CdFileNbr = 0;
$this->CdFileByName = array();
$this->VisFileLst = array();
$this->ArchCancelModif();
}
function ArchCancelModif() {
$this->LastReadComp = false; // compression of the last read file (1=compressed, 0=stored not compressed, -1= stored compressed but read uncompressed)
$this->LastReadIdx = false; // index of the last file read
$this->ReplInfo = array();
$this->ReplByPos = array();
$this->AddInfo = array();
}
function FileAdd($Name, $Data, $DataType=TBSZIP_STRING, $Compress=true) {
if ($Data===false) return $this->FileCancelModif($Name, false); // Cancel a previously added file
// Save information for adding a new file into the archive
$Diff = 30 + 46 + 2*strlen($Name); // size of the header + cd info
$Ref = $this->_DataCreateNewRef($Data, $DataType, $Compress, $Diff, $Name);
if ($Ref===false) return false;
$Ref['name'] = $Name;
$this->AddInfo[] = $Ref;
return $Ref['res'];
}
function CentralDirRead() {
$cd_info = 'PK'.chr(05).chr(06); // signature of the Central Directory
$cd_pos = -22;
$this->_MoveTo($cd_pos, SEEK_END);
$b = $this->_ReadData(4);
if ($b===$cd_info) {
$this->CdEndPos = ftell($this->ArchHnd) - 4;
} else {
$p = $this->_FindCDEnd($cd_info);
//echo 'p='.var_export($p,true); exit;
if ($p===false) {
return $this->RaiseError('The End of Central Directory Record is not found.');
} else {
$this->CdEndPos = $p;
$this->_MoveTo($p+4);
}
}
$this->CdInfo = $this->CentralDirRead_End($cd_info);
$this->CdFileLst = array();
$this->CdFileNbr = $this->CdInfo['file_nbr_curr'];
$this->CdPos = $this->CdInfo['p_cd'];
if ($this->CdFileNbr<=0) return $this->RaiseError('No header found in the Central Directory.');
if ($this->CdPos<=0) return $this->RaiseError('No position found for the Central Directory.');
$this->_MoveTo($this->CdPos);
for ($i=0;$i<$this->CdFileNbr;$i++) {
$x = $this->CentralDirRead_File($i);
if ($x!==false) {
$this->CdFileLst[$i] = $x;
$this->CdFileByName[$x['v_name']] = $i;
}
}
return true;
}
function CentralDirRead_End($cd_info) {
$b = $cd_info.$this->_ReadData(18);
$x = array();
$x['disk_num_curr'] = $this->_GetDec($b,4,2); // number of this disk
$x['disk_num_cd'] = $this->_GetDec($b,6,2); // number of the disk with the start of the central directory
$x['file_nbr_curr'] = $this->_GetDec($b,8,2); // total number of entries in the central directory on this disk
$x['file_nbr_tot'] = $this->_GetDec($b,10,2); // total number of entries in the central directory
$x['l_cd'] = $this->_GetDec($b,12,4); // size of the central directory
$x['p_cd'] = $this->_GetDec($b,16,4); // position of start of central directory with respect to the starting disk number
$x['l_comm'] = $this->_GetDec($b,20,2); // .ZIP file comment length
$x['v_comm'] = $this->_ReadData($x['l_comm']); // .ZIP file comment
$x['bin'] = $b.$x['v_comm'];
return $x;
}
function CentralDirRead_File($idx) {
$b = $this->_ReadData(46);
$x = $this->_GetHex($b,0,4);
if ($x!=='h:02014b50') return $this->RaiseError("Signature of Central Directory Header #".$idx." (file information) expected but not found at position ".$this->_TxtPos(ftell($this->ArchHnd) - 46).".");
$x = array();
$x['vers_used'] = $this->_GetDec($b,4,2);
$x['vers_necess'] = $this->_GetDec($b,6,2);
$x['purp'] = $this->_GetBin($b,8,2);
$x['meth'] = $this->_GetDec($b,10,2);
$x['time'] = $this->_GetDec($b,12,2);
$x['date'] = $this->_GetDec($b,14,2);
$x['crc32'] = $this->_GetDec($b,16,4);
$x['l_data_c'] = $this->_GetDec($b,20,4);
$x['l_data_u'] = $this->_GetDec($b,24,4);
$x['l_name'] = $this->_GetDec($b,28,2);
$x['l_fields'] = $this->_GetDec($b,30,2);
$x['l_comm'] = $this->_GetDec($b,32,2);
$x['disk_num'] = $this->_GetDec($b,34,2);
$x['int_file_att'] = $this->_GetDec($b,36,2);
$x['ext_file_att'] = $this->_GetDec($b,38,4);
$x['p_loc'] = $this->_GetDec($b,42,4);
$x['v_name'] = $this->_ReadData($x['l_name']);
$x['v_fields'] = $this->_ReadData($x['l_fields']);
$x['v_comm'] = $this->_ReadData($x['l_comm']);
$x['bin'] = $b.$x['v_name'].$x['v_fields'].$x['v_comm'];
return $x;
}
function RaiseError($Msg) {
if ($this->DisplayError) {
if (PHP_SAPI==='cli') {
echo get_class($this).' ERROR with the zip archive: '.$Msg."\r\n";
} else {
echo '<strong>'.get_class($this).' ERROR with the zip archive:</strong> '.$Msg.'<br>'."\r\n";
}
}
$this->Error = $Msg;
return false;
}
function Debug($FileHeaders=false) {
$this->DisplayError = true;
if ($FileHeaders) {
// Calculations first in order to have error messages before other information
$idx = 0;
$pos = 0;
$pos_stop = $this->CdInfo['p_cd'];
$this->_MoveTo($pos);
while ( ($pos<$pos_stop) && ($ok = $this->_ReadFile($idx,false)) ) {
$this->VisFileLst[$idx]['p_this_header (debug_mode only)'] = $pos;
$pos = ftell($this->ArchHnd);
$idx++;
}
}
$nl = "\r\n";
echo "<pre>";
echo "-------------------------------".$nl;
echo "End of Central Directory record".$nl;
echo "-------------------------------".$nl;
print_r($this->DebugArray($this->CdInfo));
echo $nl;
echo "-------------------------".$nl;
echo "Central Directory headers".$nl;
echo "-------------------------".$nl;
print_r($this->DebugArray($this->CdFileLst));
if ($FileHeaders) {
echo $nl;
echo "------------------".$nl;
echo "Local File headers".$nl;
ech
没有合适的资源?快使用搜索试试~ 我知道了~
PHP实例开发源码-TbsZip(Zip处理类) php版 v2.1.5.zip
共3个文件
php:2个
html:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 46 浏览量
2022-10-16
04:08:29
上传
评论
收藏 15KB ZIP 举报
温馨提示
PHP实例开发源码—TbsZip(Zip处理类) php版 v2.1.5.zip PHP实例开发源码—TbsZip(Zip处理类) php版 v2.1.5.zip PHP实例开发源码—TbsZip(Zip处理类) php版 v2.1.5.zip
资源详情
资源评论
资源推荐
收起资源包目录
PHP实例开发源码—TbsZip(Zip处理类) php版 v2.1.5.zip (3个子文件)
132707302409353911
tbszip_help.html 18KB
tbszip_code_examples.php 3KB
tbszip.php 31KB
共 3 条
- 1
易小侠
- 粉丝: 6451
- 资源: 9万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0