小弟我有这么一个压缩文件夹的类,求更快的压缩方法

WBOY
Release: 2016-06-13 11:58:43
Original
832 people have browsed it

我有这么一个压缩文件夹的类,求更快的压缩方法

<br /><?php<br /><br />class PHPZip<br />{<br /><br />	var $datasec		 = array();<br />	var $ctrl_dir		 = array();<br />	var $eof_ctrl_dir	 = "\x50\x4b\x05\x06\x00\x00\x00\x00";<br />	var $old_offset		 = 0;<br />	<br />	function Zip($dir, $zipfilename)<br />	{<br />		if(@function_exists('gzcompress'))<br />		{<br />			@set_time_limit("0");<br />			$this->openFile($dir, $dir);<br />			$out = $this->filezip();<br />			$fp	 = fopen($zipfilename, "w");<br />			$fwrite = fwrite($fp, $out, strlen($out));<br />			fclose($fp);<br />			if($fwrite == true)<br />			{<br />				return true;<br />			}<br />		}<br />		return false;<br />	}<br /><br />	function openFile($path, $zipName)<br />	{<br />		$temp_path		 = $path;<br />		$temp_zip_path	 = $zipName;<br />		$zipDir			 = $zipName;<br />		<br />		if($handle			 = @opendir($path))<br />		{<br />			while(($file = readdir($handle))!==false)<br />			{<br />				$sub_dir = $path . DIRECTORY_SEPARATOR . $file;    //构建子目录路径<br />				if($file == '.' || $file == '..') {<br />					continue;<br />				}else if(is_dir($sub_dir)) {    //如果是目录,进行递归<br />					$this->openFile($path . '/' . $file, $zipName . '/' . $file);<br />				}<br />				else{<br />					$fd			 = fopen($path . '/' . $file, "r");<br />					$fileValue	 = @fread($fd, 1024000);<br />					if($fd)<br />						fclose($fd);<br />					$this->addFile($fileValue, $zipName . '/' . $file);<br />				}<br />			}<br />			$zipName = $temp_zip_path;<br />			$path	 = $temp_path;<br />			closedir($handle);<br />		}<br />	}<br /><br />	function unix2DosTime($unixtime = 0)<br />	{<br />		$timearray = ($unixtime==0) ? getdate() : getdate($unixtime);<br />		if($timearray['year']<1980)<br />		{<br />			$timearray['year']		 = 1980;<br />			$timearray['mon']		 = 1;<br />			$timearray['mday']		 = 1;<br />			$timearray['hours']		 = 0;<br />			$timearray['minutes']	 = 0;<br />			$timearray['seconds']	 = 0;<br />		}<br />		return (($timearray['year']-1980)<<25)|($timearray['mon']<<21)|($timearray['mday']<<16)|<br />				($timearray['hours']<<11)|($timearray['minutes']<<5)|($timearray['seconds']>>1);<br />	}<br /><br />	function addFile($data, $name, $time = 0)<br />	{<br />		$name				 = str_replace('\\', '/', $name);<br />		$dtime				 = dechex($this->unix2DosTime($time));<br />		$hexdtime			 = '\x' . $dtime[6] . $dtime[7]<br />				. '\x' . $dtime[4] . $dtime[5]<br />				. '\x' . $dtime[2] . $dtime[3]<br />				. '\x' . $dtime[0] . $dtime[1];<br />		eval('$hexdtime = "' . $hexdtime . '";');<br />		$fr					 = "\x50\x4b\x03\x04";<br />		$fr .= "\x14\x00";<br />		$fr .= "\x00\x00";<br />		$fr .= "\x08\x00";<br />		$fr .= $hexdtime;<br />		$unc_len			 = strlen($data);<br />		$crc				 = crc32($data);<br />		$zdata				 = gzcompress($data);<br />		$c_len				 = strlen($zdata);<br />		$zdata				 = substr(substr($zdata, 0, strlen($zdata)-4), 2);<br />		$fr .= pack('V', $crc);<br />		$fr .= pack('V', $c_len);<br />		$fr .= pack('V', $unc_len);<br />		$fr .= pack('v', strlen($name));<br />		$fr .= pack('v', 0);<br />		$fr .= $name;<br />		$fr .= $zdata;<br />		$fr .= pack('V', $crc);<br />		$fr .= pack('V', $c_len);<br />		$fr .= pack('V', $unc_len);<br />		$this->datasec[]	 = $fr;<br />		$new_offset			 = strlen(implode('', $this->datasec));<br />		$cdrec				 = "\x50\x4b\x01\x02";<br />		$cdrec .= "\x00\x00";<br />		$cdrec .= "\x14\x00";<br />		$cdrec .= "\x00\x00";<br />		$cdrec .= "\x08\x00";<br />		$cdrec .= $hexdtime;<br />		$cdrec .= pack('V', $crc);<br />		$cdrec .= pack('V', $c_len);<br />		$cdrec .= pack('V', $unc_len);<br />		$cdrec .= pack('v', strlen($name));<br />		$cdrec .= pack('v', 0);<br />		$cdrec .= pack('v', 0);<br />		$cdrec .= pack('v', 0);<br />		$cdrec .= pack('v', 0);<br />		$cdrec .= pack('V', 32);<br />		$cdrec .= pack('V', $this->old_offset);<br />		$this->old_offset	 = $new_offset;<br />		$cdrec .= $name;<br />		$this->ctrl_dir[]	 = $cdrec;<br />	}<br /><br />	function filezip()<br />	{<br />		$data	 = implode('', $this->datasec);<br />		$ctrldir = implode('', $this->ctrl_dir);<br />		return<br />				$data .<br />				$ctrldir .<br />				$this->eof_ctrl_dir .<br />				pack('v', sizeof($this->ctrl_dir)) .<br />				pack('v', sizeof($this->ctrl_dir)) .<br />				pack('V', strlen($ctrldir)) .<br />				pack('V', strlen($data)) .<br />				"\x00\x00";<br />	}<br /><br />}<br /><br />//我有个文件夹,大概30M,600个文件,打包时间超过半个小时候以上,谁有更好更快的打包方法啊,不胜感谢了。<br /><br />
Copy after login

------解决方案--------------------
php 提供有 zip 扩展,为什么不用?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template