在帝国cms系统文件中,e/class/phpzip.inc.php是专门处理文件压缩功能的,阅读代码你会发现,它没有解压缩功能。
网站有个模板版块提供各种网页模板、手机模板下载功能。系统除了录入模板的介绍信息外,还需要上传模板截图,模板附件等。可是有些模板页面实在太多了,不能一一截图展示,再说如果截图过多,1是费力,2是费网络资源,3是用户体验也不好。所以就必须要有一些解压文件包的功能,把上传的附件解压到某个指定目录,形成内部静态网页,让用户可以预览模板,切实体验模板详情。
功能代码制作
查了一下相关资料,发现写个php解压缩文件功能还是比较复杂的,所以就从网上找到别人写好的代码,融合到帝国CMS里,大家可以把以下代码复制到e/class/phpzip.inc.php文件第213行下:
//解压文件部分 function unZip($zipfile, $to, $index = Array(-1)) { $ok = 0; $zip = @fopen($zipfile, 'rb'); if(!$zip){ return(-1); } $cdir = $this->ReadCentralDir($zip, $zipfile); $pos_entry = $cdir['offset']; if(!is_array($index)){ $index = array($index); } for($i=0; $index[$i]; $i++) { if(intval($index[$i]) != $index[$i] || $index[$i] > $cdir['entries']) { return(-1); } } for($i=0; $i<$cdir['entries']; $i++) { @fseek($zip, $pos_entry); $header = $this->ReadCentralFileHeaders($zip); $header['index'] = $i; $pos_entry = ftell($zip); @rewind($zip); fseek($zip, $header['offset']); if(in_array("-1", $index) || in_array($i, $index)) { $stat[$header['filename']] = $this->ExtractFile($header, $to, $zip); } } fclose($zip); return $stat; } function ReadCentralDir($zip, $zipfile) { $size = filesize($zipfile); $max_size = ($size < 277) ? $size : 277; @fseek($zip, $size - $max_size); $pos = ftell($zip); $bytes = 0x00000000; while($pos < $size) { $byte = @fread($zip, 1); $bytes = ($bytes << 8) | Ord($byte); $pos++; if($bytes == 0x504b0506){ break; } } $data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', fread($zip, 18)); $centd['comment'] = ($data['comment_size'] != 0) ? fread($zip, $data['comment_size']) : ''; // 注释 $centd['entries'] = $data['entries']; $centd['disk_entries'] = $data['disk_entries']; $centd['offset'] = $data['offset']; $centd['disk_start'] = $data['disk_start']; $centd['size'] = $data['size']; $centd['disk'] = $data['disk']; return $centd; } function ReadCentralFileHeaders($zip) { $binary_data = fread($zip, 46); $header = unpack('vchkid/vid/vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data); $header['filename'] = ($header['filename_len'] != 0) ? fread($zip, $header['filename_len']) : ''; $header['extra'] = ($header['extra_len'] != 0) ? fread($zip, $header['extra_len']) : ''; $header['comment'] = ($header['comment_len'] != 0) ? fread($zip, $header['comment_len']) : ''; if($header['mdate'] && $header['mtime']) { $hour = ($header['mtime'] & 0xF800) >> 11; $minute = ($header['mtime'] & 0x07E0) >> 5; $seconde = ($header['mtime'] & 0x001F) * 2; $year = (($header['mdate'] & 0xFE00) >> 9) + 1980; $month = ($header['mdate'] & 0x01E0) >> 5; $day = $header['mdate'] & 0x001F; $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year); } else { $header['mtime'] = time(); } $header['stored_filename'] = $header['filename']; $header['status'] = 'ok'; if(substr($header['filename'], -1) == '/'){ $header['external'] = 0x41FF0010; } // 判断是否文件夹 return $header; } function ExtractFile($header, $to, $zip) { $header = $this->readfileheader($zip); $pthss = ""; if(substr($to, -1) != "/"){ $to .= "/"; } if(!@is_dir($to)){ @mkdir($to, 0777); } $pth = explode("/", dirname($header['filename'])); for($i=0; isset($pth[$i]); $i++){ if(!$pth[$i]){ continue; } $pthss .= $pth[$i]."/"; if(!is_dir($to.$pthss)){ @mkdir($to.$pthss, 0777); } } if(!($header['external'] == 0x41FF0010) && !($header['external'] == 16)) { if($header['compression'] == 0) { $fp = @fopen($to.$header['filename'], 'wb'); if(!$fp){ return(-1); } $size = $header['compressed_size']; while($size != 0) { $read_size = ($size < 2048 ? $size : 2048); $buffer = fread($zip, $read_size); $binary_data = pack('a'.$read_size, $buffer); @fwrite($fp, $binary_data, $read_size); $size -= $read_size; } fclose($fp); touch($to.$header['filename'], $header['mtime']); }else{ $fp = @fopen($to.$header['filename'].'.gz', 'wb'); if(!$fp){ return(-1); } $binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($header['compression']), Chr(0x00), time(), Chr(0x00), Chr(3)); fwrite($fp, $binary_data, 10); $size = $header['compressed_size']; while($size != 0) { $read_size = ($size < 1024 ? $size : 1024); $buffer = fread($zip, $read_size); $binary_data = pack('a'.$read_size, $buffer); @fwrite($fp, $binary_data, $read_size); $size -= $read_size; } $binary_data = pack('VV', $header['crc'], $header['size']); fwrite($fp, $binary_data, 8); fclose($fp); $gzp = @gzopen($to.$header['filename'].'.gz', 'rb') or die("Cette archive est compress!"); if(!$gzp){ return(-2); } $fp = @fopen($to.$header['filename'], 'wb'); if(!$fp){ return(-1); } $size = $header['size']; while($size != 0) { $read_size = ($size < 2048 ? $size : 2048); $buffer = gzread($gzp, $read_size); $binary_data = pack('a'.$read_size, $buffer); @fwrite($fp, $binary_data, $read_size); $size -= $read_size; } fclose($fp); gzclose($gzp); touch($to.$header['filename'], $header['mtime']); @unlink($to.$header['filename'].'.gz'); } } return true; } function ReadFileHeader($zip) { $binary_data = fread($zip, 30); $data = unpack('vchk/vid/vversion/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len', $binary_data); $header['filename'] = fread($zip, $data['filename_len']); $header['extra'] = ($data['extra_len'] != 0) ? fread($zip, $data['extra_len']) : ''; $header['compression'] = $data['compression']; $header['size'] = $data['size']; $header['compressed_size'] = $data['compressed_size']; $header['crc'] = $data['crc']; $header['flag'] = $data['flag']; $header['mdate'] = $data['mdate']; $header['mtime'] = $data['mtime']; if($header['mdate'] && $header['mtime']){ $hour = ($header['mtime'] & 0xF800) >> 11; $minute = ($header['mtime'] & 0x07E0) >> 5; $seconde = ($header['mtime'] & 0x001F) * 2; $year = (($header['mdate'] & 0xFE00) >> 9) + 1980; $month = ($header['mdate'] & 0x01E0) >> 5; $day = $header['mdate'] & 0x001F; $header['mtime'] = mktime($hour, $minute, $seconde, $month, $day, $year); }else{ $header['mtime'] = time(); } $header['stored_filename'] = $header['filename']; $header['status'] = "ok"; return $header; } function GetOneFileName($zipfile){ $zip = @fopen($zipfile, 'rb'); $v=$this->ReadFileHeader($zip); if(substr($v['filename'], -1) == '/') { return $v['filename']; }else{ return ''; } }
如何使用解压缩功能
在完成上一步代码复制粘贴后,在你要使用的页面中引入:
require ECMS_PATH.'e/class/phpzip.inc.php'; $z=new PHPZip(); //新建立一个zip的类 $res=$z->unZip('你要解压的文件','解压到那个文件夹下'); //两个地址一定要是绝对目录 /* * 举例 * $res=$z->unZip(ECMS_PATH.'https://www.hhymw.comhttps://www.freexyz.cn/d/file/mb/qianduanmb/20190426/abc.rar',ECMS_PATH.'/demo/html/abc'); */
在实际的使用中,要解压附件并不是上面几行代码就能完成的,下面说是对此问题的解压思路及代码上的实现
1、获取真实地址。想要得到某条信息附件的真实地址,我们需要拿到信息的ID,通过步骤查询,然后解析获取。
2、定义想要解压到目录的规则
3、创建目录(如果有就跳过,没有就新创建)
4、判断压缩包里第一层是文件还是文件夹
5、执行解压缩
6、更新数据
代码:
$z=new PHPZip(); //新建立一个zip的类 $r=$empire->fetch1("select downpath from {$dbtbpre}ecms_download_data_1 WHERE id=$id limit 1"); //查询附件 $path_r=explode("rn",$r[downpath]); //解析下载地址 $showdown_r=explode("::::::",$path_r[0]);//拆分第一行下载地址 $downurl=$showdown_r[1];//将第一行下载地址赋值给$downurl变量 if($downurl) { //如果存在下载地址 $zipfile=ECMS_PATH.$downurl; //组合服务器绝对附件地址,如: D:wwwrootwwwhttps://www.wdzzz.comhttps://www.freexyz.cn/d/file/mb/qianduanmb/20190426/abc.rar $tofile=ECMS_PATH.'/demo/'.$id.'/'; //定义想要解压到目录的规则 if(!is_dir($tofile)) { //创建目录 @mkdir($tofile,0777,ture); @chmod($tofile,0777); } $mulu=$z->GetOneFileName($zipfile);//判断压缩包里第一层是文件还是文件夹 $mulu = iconv('gbk','utf-8',$mulu);//将中文目录转码 $res=$z->unZip($zipfile,$tofile);//执行解压缩操作 if($res) { $ysurl='/demo/'.$id.'/'.$mulu; $empire->query("update {$dbtbpre}ecms_download set ysurl='$ysurl' WHERE id=$id");//更新数据 return json_encode(array('error'=>0,'ysurl'=>$ysurl,'msg'=>'生成演示目录成功')); } }
以上是解压缩的核心代码,想要集成到信息里可以把代码封装成函数,以便后期调用。如果不会的话,可以联系本站站长做付费操作。下面说说帝国CMS的压缩功能
帝国CMS压缩文件夹功能:
require ECMS_PATH.'e/class/phpzip.inc.php'; //引入类文件 $z=new PHPZip(); //新建立一个zip的类 $z->Zip('要压缩的文件夹','压缩到那个文件夹下'); //举例: $z->Zip('D:wampwwwdgcmsaa','D:wampwwwdgcmsbbaa.zip'); //添加指定目录