功能:将文件压缩成zip,或者rar的压缩包。后缀名可以自定义。
使用方法:首先实例化,然后传参。两个参数。第一个关于你文件地址的一个Array。第二个是要你要保存的压缩包文件的绝对地址。
使用例子:
$zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt");<BR> $z = new PHPZip();<BR> //$randomstr = random(8);<BR> $zipfile = TEMP."/photocome_".$groupid.".zip";<BR> $z->Zip($zipfiles, $zipfile); //添加文件列表<BR>
PHP的ZIP压缩类如下:
<?php<BR># <BR># PHPZip v1.2 by Sext ([email protected]) 2002-11-18<BR># (Changed: 2003-03-01)<BR># <BR># Makes zip archive<BR>#<BR># Based on "Zip file creation class", uses zLib<BR>#<BR>#<BR>class PHPZip<BR>{<BR> function Zip($dir, $zipfilename)<BR> {<BR> if (@function_exists('gzcompress'))<BR> { <BR> $curdir = getcwd();<BR> if (is_array($dir)) <BR> {<BR> $filelist = $dir;<BR> }<BR> else<BR> {<BR> $filelist = $this -> GetFileList($dir);<BR> }<br><br> if ((!empty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir);<BR> else chdir($curdir);<br><br> if (count($filelist)>0)<BR> {<BR> foreach($filelist as $filename)<BR> {<BR> if (is_file($filename))<BR> {<BR> $fd = fopen ($filename, "r");<BR> $content = fread ($fd, filesize ($filename));<BR> fclose ($fd);<br><br> if (is_array($dir)) $filename = basename($filename);<BR> $this -> addFile($content, $filename);<BR> }<BR> }<BR> $out = $this -> fil<p>+本文来源gao!%daima.com搞$代*!码9网(</p><strong>搞gaodaima代码</strong>e();<br><br> chdir($curdir);<BR> $fp = fopen($zipfilename, "w");<BR> fwrite($fp, $out, strlen($out));<BR> fclose($fp);<BR> }<BR> return 1;<BR> } <BR> else return 0;<BR> }<br><br> function GetFileList($dir)<BR> {<BR> if (file_exists($dir))<BR> {<BR> $args = func_get_args();<BR> $pref = $args[1];<br><br> $dh = opendir($dir);<BR> while($files = readdir($dh))<BR> {<BR> if (($files!=".")&&($files!="..")) <BR> {<BR> if (is_dir($dir.$files)) <BR> {<BR> $curdir = getcwd();<BR> chdir($dir.$files);<BR> $file = array_merge($file, $this -> GetFileList("", "$pref$files/"));<BR> chdir($curdir);<BR> }<BR> else $file[]=$pref.$files;<BR> }<BR> }<BR> closedir($dh);<BR> }<BR> return $file;<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> /**<BR> * Converts an Unix timestamp to a four byte DOS date and time format (date<BR> * in high two bytes, time in low two bytes allowing magnitude comparison).<BR> *<BR> * @param integer the current Unix timestamp<BR> *<BR> * @return integer the current date in a four byte DOS format<BR> *<BR> * @access private<BR> */<BR> function unix2DosTime($unixtime = 0) {<BR> $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);<br><br> if ($timearray['year'] < 1980) {<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> } // end if<br><br> return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |<BR> ($timearray['hours'] << 11) | ($timearray['minutes'] <> 1);<BR> } // end of the 'unix2DosTime()' method<br><br> /**<BR> * Adds "file" to archive<BR> *<BR> * @param string file contents<BR> * @param string name of the file in the archive (may contains the path)<BR> * @param integer the current timestamp<BR> *<BR> * @access public<BR> */<BR> function addFile($data, $name, $time = 0)<BR> {<BR> $name = str_replace('\\', '/', $name);<br><br> $dtime = dechex($this->unix2DosTime($time));<BR> $hexdtime = '\x' . $dtime[6] . $dtime[7]<BR> Western union point . '\x' . $dtime[4] . $dtime[5]<BR> . '\x' . $dtime[2] . $dtime[3]<BR> . '\x' . $dtime[0] . $dtime[1];<BR> eval('$hexdtime = "' . $hexdtime . '";');<br><br> $fr = "\x50\x4b\x03\x04";<BR> $fr .= "\x14\x00"; // ver needed to extract<BR> $fr .= "\x00\x00"; // gen purpose bit flag<BR> $fr .= "\x08\x00"; // compression method<BR> $fr .= $hexdtime; // last mod time and date<br><br> // "local file header" segment<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); // fix crc bug<BR> $fr .= pack('V', $crc); // crc32<BR> $fr .= pack('V', $c_len); // compressed filesize<BR> $fr .= pack('V', $unc_len); // uncompressed filesize<BR> $fr .= pack('v', strlen($name)); // length of filename<BR> $fr .= pack('v', 0); // extra field length<BR> $fr .= $name;<br><br> // "file data" segment<BR> $fr .= $zdata;<br><br> // "data descriptor" segment (optional but necessary if archive is not<BR> // served as file)<BR> $fr .= pack('V', $crc); // crc32<BR> $fr .= pack('V', $c_len); // compressed filesize<BR> $fr .= pack('V', $unc_len); // uncompressed filesize<br><br> // add this entry to array<BR> $this -> datasec[] = $fr;<BR> $new_offset = strlen(implode('', $this->datasec));<br><br> // now add to central directory record<BR> $cdrec = "\x50\x4b\x01\x02";<BR> $cdrec .= "\x00\x00"; // version made by<BR> $cdrec .= "\x14\x00"; // version needed to extract<BR> $cdrec .= "\x00\x00"; // gen purpose bit flag<BR> $cdrec .= "\x08\x00"; // compression method<BR> $cdrec .= $hexdtime; // last mod time & date<BR> $cdrec .= pack('V', $crc); // crc32<BR> $cdrec .= pack('V', $c_len); // compressed filesize<BR> $cdrec .= pack('V', $unc_len); // uncompressed filesize<BR> $cdrec .= pack('v', strlen($name) ); // length of filename<BR> $cdrec .= pack('v', 0 ); // extra field length<BR> $cdrec .= pack('v', 0 ); // file comment length<BR> $cdrec .= pack('v', 0 ); // disk number start<BR> $cdrec .= pack('v', 0 ); // internal file attributes<BR> $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set<br><br> $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header<BR> $this -> old_offset = $new_offset;<br><br> $cdrec .= $name;<br><br> // optional extra field, file comment goes here<BR> // save to central directory<BR> $this -> ctrl_dir[] = $cdrec;<BR> } // end of the 'addFile()' method<br><br> /**<BR> * Dumps out file<BR> *<BR> * @return string the zipped file<BR> *<BR> * @access public<BR> */<BR> function file()<BR> {<BR> $data = implode('', $this -> datasec);<BR> $ctrldir = implode('', $this -> ctrl_dir);<br><br> return<BR> $data .<BR> $ctrldir .<BR> $this -> eof_ctrl_dir .<BR> pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"<BR> pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall<BR> pack('V', strlen($ctrldir)) . // size of central dir<BR> pack('V', strlen($data)) . // offset to start of central dir<BR> "\x00\x00"; // .zip file comment length<BR> } // end of the 'file()' method<br><br>} // end of the 'PHPZip' class<BR>?><BR>