Rumah > php教程 > PHP源码 > teks badan

PHP在线快速压缩解压ZIP文件使用pclzip类

PHP中文网
Lepaskan: 2016-05-25 17:10:17
asal
2015 orang telah melayarinya

1. [图片] zip.JPG    

PHP在线快速压缩解压ZIP文件使用pclzip类                

2. [代码][PHP]代码  

<?php
header("content-Type: text/html; charset=utf-8");include "pclzip.lib.php";@set_time_limit(0);
$password = "123456";//验证密码
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>在线解压ZIP文件程序|www.ibtf.net</title>
<style type="text/css">
body,td{font-size: 14px;color: #000000;}a {color: #000066;text-decoration: none;}a:hover {color: #FF6600;text-decoration: underline;}</style>
</head>
<body>
<? if(!$_REQUEST["myaction"]):?>
<form name="myform" method="post" action="<?=$_SERVER[PHP_SELF];?>" onSubmit="return check_uploadObject(this);">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr>
      <td height="40" colspan="2" style="color:#FF9900"><p><font color="#FF0000">在线解压ZIP文件程序 --欢迎浏览:<a href="http://www.ibtf.net">爱美丽女装</a></font></p>
      <p>使用方法:把zip文件通过FTP上传到本文件相同的目录下,选择zip文件;</p>
<p> </p>
 </td>
    </tr>
    <tr>
      <td width="11%">选择ZIP文件: </td>
      <td width="89%"><select name="zipfile">
<option value="" selected>- 请选择 -</option>
<?
  	$fdir = opendir(&#39;./&#39;);
	while($file=readdir($fdir)){
		if(!is_file($file)) continue;
		if(preg_match(&#39;/\.zip$/mis&#39;,$file)){
			echo "<option value=&#39;$file&#39;>$file</option>\r\n";
		}
	}
?></select></td>
    </tr>
	<tr>
      <td>解压到目录: </td>
      <td><input name="todir" type="text" id="todir" value="" size="15">
      (留空为本目录,必须有写入权限,其它目录如:bitefu)</td>
    </tr>
	<tr>
      <td>验证密码: </td>
      <td><input name="password" type="password" id="password" size="15">
        (源文件中设定的密码)</td>
    </tr>
    <tr>
      <td><input name="myaction" type="hidden" id="myaction" value="dounzip"></td>
      <td><input type="submit" name="Submit" value=" 解 压 "></td>
    </tr>
  </table>
</form>
  <hr>
<form name="myform" method="post" action="<?=$_SERVER[PHP_SELF];?>" onSubmit="return check_uploadObject(this);">
<table width="100%" border="0" cellspacing="0" cellpadding="4">
    <tr>
      <td height="40" colspan="2" style="color:#FF9900"><p><font color="#FF0000">在线压缩ZIP文件程序</font></p>
      <p>使用方法:填写压缩目录,填写压缩后文件名;</p></td>
</tr>
<tr>
      <td width="11%">压缩目录: </td>
      <td width="89%"><input name="todir" type="text" id="todir" value="" size="15">
      (留空为当前目录,必须有写入权限,其它目录如:bitefu)</td>
    </tr>
	<tr>
      <td>压缩后文件名: </td>
      <td><input name="toname" type="text" id="toname" value="<?=date("Ymdhis").".zip"?>" size="30">
      (压缩后文件名,如<a href="http://www.ibtf.net">www.ibtf.net</a>.zip)</td>
    </tr>
	<tr>
      <td>验证密码: </td>
      <td><input name="password" type="password" id="password" size="15">
        (源文件中设定的密码)</td>
    </tr>
    <tr>
      <td><input name="myaction" type="hidden" id="myaction" value="dozip"></td>
      <td><input type="submit" name="Submit" value=" 压 缩 "></td>
    </tr>
  </table></form>
<?
elseif($_REQUEST["myaction"]=="dounzip"):
	if ($_POST[&#39;password&#39;] != $password) die("输入的密码不正确,请重新输入。");
	$path    = &#39;&#39;;$remove  = &#39;&#39;;
	$name    = u2g(trim($_REQUEST[&#39;zipfile&#39;]));
	$unzippath = $_POST["todir"]?u2g(trim($_REQUEST[&#39;todir&#39;],&#39;/&#39;)).&#39;/&#39;:&#39;./&#39;;
	if(file_exists($path.$name) && is_file($path.$name)){
		$zip = new PclZip($path.$name);
		$result = $zip->extract($path.((&#39;./&#39; == $unzippath)?&#39;&#39;:$unzippath), $remove);
		$data = array();
		if($result){
			$data[&#39;statusCode&#39;] = 200;
			$list = $zip->listContent();
			$fold = 0; $fil = 0; $tot_comp = 0; $tot_uncomp = 0;
			foreach($list as $key=>$val){if ($val[&#39;folder&#39;]==&#39;1&#39;) {++$fold;}else{++$fil;$tot_comp += $val[&#39;compressed_size&#39;];$tot_uncomp += $val[&#39;size&#39;];}}
			G(&#39;_unzip_end&#39;);
			$data[&#39;message&#39;]  = &#39;<font color="green">解压目标文件:</font><font color="red"> &#39;.g2u($name).&#39;</font><br />&#39;;
			$data[&#39;message&#39;] .= &#39;<font color="green">解压文件详情:</font><font color="red">共&#39;.$fold.&#39; 个目录,&#39;.$fil.&#39; 个文件</font><br />&#39;;
			$data[&#39;message&#39;] .= &#39;<font color="green">压缩文档大小:</font><font color="red">&#39;.dealsize($tot_comp).&#39;</font><br />&#39;;
			$data[&#39;message&#39;] .= &#39;<font color="green">解压文档大小:</font><font color="red">&#39;.dealsize($tot_uncomp).&#39;</font><br />&#39;;
			$data[&#39;message&#39;] .= &#39;<font color="green">解压总计耗时:</font><font color="red">&#39;.G(&#39;_run_start&#39;,&#39;_run_end&#39;,6).&#39; 秒</font><br />&#39;;
		}else{
			$data[&#39;statusCode&#39;] = 300;
			$data[&#39;message&#39;]   .= &#39;<font color="blue">解压失败:</font><font color="red">&#39;.$zip->errorInfo(true).&#39;</font><br />&#39;;
			$data[&#39;message&#39;]   .= &#39;<font color="green">执行耗时:</font><font color="red">&#39;.G(&#39;_run_start&#39;,&#39;_run_end&#39;,6).&#39; 秒</font><br />&#39;;
		}
		//exit(json_encode($data));

	}else{
		$data = array();
		$data[&#39;statusCode&#39;] = 300;
		$data[&#39;message&#39;] = &#39;Sorry,未知错误,无法解压:&#39;.$path.$name.&#39;文件!&#39;;
		//exit(json_encode($data));
	}echo $data[&#39;message&#39;];
elseif($_REQUEST["myaction"]=="dozip"):
if ($_POST[&#39;password&#39;] != $password) die("输入的密码不正确,请重新输入。");
		$data   = array();$_REQUEST[&#39;type&#39;]=2;
		$endpath=explode("/",$_SERVER[&#39;PHP_SELF&#39;]);$endpath=$endpath[count($endpath)-2];
		$file  = $_REQUEST[&#39;todir&#39;]?u2g(trim($_REQUEST[&#39;todir&#39;])):"../".$endpath;
		$fileend  = $_REQUEST[&#39;todir&#39;]?&#39;zip&#39;:$file;
		$zipname  = u2g(trim($_REQUEST[&#39;toname&#39;]));

		$path = dirname($file);

		$Zip = new PclZip($zipname);
		if(!file_exists($zipname) && $Zip->create($file,PCLZIP_OPT_REMOVE_PATH,$fileend)){
			$data[&#39;statusCode&#39;] = 200;
		    $list = $Zip->listContent();
		    if($list){
				$fold = 0; $fil = 0; $tot_comp = 0; $tot_uncomp = 0;
				foreach($list as $key=>$val){if ($val[&#39;folder&#39;]==&#39;1&#39;) {++$fold;}else{++$fil;$tot_comp += $val[&#39;compressed_size&#39;];$tot_uncomp += $val[&#39;size&#39;];}}
				$data[&#39;message&#39;]  = &#39;<font color="green">压缩目标文件:</font><font color="red"> &#39;.g2u($zipname).&#39;</font><br />&#39;;
				$data[&#39;message&#39;] .= &#39;<font color="green">压缩文件详情:</font><font color="red">共&#39;.$fold.&#39; 个目录,&#39;.$fil.&#39; 个文件</font><br />&#39;;
				$data[&#39;message&#39;] .= &#39;<font color="green">压缩文档大小:</font><font color="red">&#39;.dealsize($tot_comp).&#39;</font><br />&#39;;
				$data[&#39;message&#39;] .= &#39;<font color="green">解压文档大小:</font><font color="red">&#39;.dealsize($tot_uncomp).&#39;</font><br />&#39;;
				$data[&#39;message&#39;] .= &#39;<font color="green">压缩执行耗时:</font><font color="red">&#39;.G(&#39;_run_start&#39;,&#39;_run_end&#39;,6).&#39; 秒</font><br />&#39;;
				$data[&#39;message&#39;] .= &#39;<font color="green">压缩文件列表:</font><br />&#39;;
				foreach($list as $key => $val){
				    if(10 > $key){$key = &#39;00&#39;.$key;}elseif(100 > $key){$key = &#39;0&#39;.$key;}
					$data[&#39;message&#39;] .= &#39;<font color="red">&#39;.$key.&#39;=>&#39;.($val[&#39;folder&#39;]?&#39;目录:&#39;:&#39;文件:&#39;).g2u($val[&#39;filename&#39;]).&#39; </font><br />&#39;;
				}
			}
		}else{
			$data[&#39;statusCode&#39;] = 300;
			if(file_exists($zipname)){$error = $zipname.&#39;已经存在!&#39;;}else{$error = $Zip->errorInfo(true);}
			$data[&#39;message&#39;]    = &#39;<font color="blue">压缩失败:</font><font color="red">&#39;.$error.&#39;</font><br />&#39;;
			$data[&#39;message&#39;]   .= &#39;<font color="green">执行耗时:</font><font color="red">&#39;.G(&#39;_run_start&#39;,&#39;_run_end&#39;,6).&#39; 秒</font><br />&#39;;
		}
		echo $data[&#39;message&#39;];

endif;
?></body>
</html><script language="javascript">
function check_uploadObject(form){
	if(form.password.value==&#39;&#39;){
		alert(&#39;请输入密码.&#39;);
		return false;
	}
	return true;
}
</script>
<?php
function u2g($str){return @iconv(&#39;UTF-8&#39;, &#39;GB2312//IGNORE&#39;, $str);}
function g2u($str){return @iconv(&#39;GB2312&#39;, &#39;UTF-8//IGNORE&#39;, $str);}
function G($start,$end=&#39;&#39;,$dec=3) {// 记录和统计时间(微秒)
    static $_info = array();
    if(!empty($end)) { // 统计时间
        if(!isset($_info[$end])) {$_info[$end]   =  microtime(TRUE);}
        return number_format(($_info[$end]-$_info[$start]),$dec);
    }else{$_info[$start]  =  microtime(TRUE); // 记录时间
    }
}
function dealsize($size){//文件大小格式化
	$danwei = array( &#39;Byte&#39;,&#39;KB&#39;,&#39;MB&#39;,&#39;GB&#39; );
	$d = 0;
	while ( $size >= 900 ){
		$size = round($size*100/1024)/100;
		$d++;
	}return $size.&#39; &#39;.$danwei[$d];}
?>
Salin selepas log masuk

                   


                   

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!