class phpzip{ private $ctrl_dir = array(); private $datasec = array(); private $old_offset = 0; private $eof_ctrl_dir = \x50\x4b\x05\x06\x00\x00\x00\x00; /** * 压缩部分--遍历指定文件夹 * @param string $path--文件夹路径 * @return array--文件夹内容列表 ****/ function visitfile($path){ $filelist = array(); $path = str_replace(\\,/,$path); $fdir = dir($path); while(($file = $fdir->read()) !== false){ if($file == '.' || $file == '..'){ continue; } $pathsub = preg_replace(*/{2,}*, /, $path./.$file); // 替换多个反斜杠 $filelist[] = is_dir($pathsub) ? $pathsub./ : $pathsub; if(is_dir($pathsub)){ $this->visitfile($pathsub); } } $fdir->close(); return $filelist; } /** * 压缩到服务器 * @param string $dir--需压缩的文件所在目录 * @param string $savename--zip压缩文件名 * @return boolean--是否压缩成功 * */ public function zip($dir, $savename){ if(@!function_exists('gzcompress')){ return false; } ob_end_clean(); $filelist = $this->visitfile($dir); if(count($filelist) == 0){ return false; } foreach($filelist as $file){ if(!file_exists($file) || !is_file($file)){ continue; } $fd = fopen($file, rb); $content = @fread($fd, filesize($file)); fclose($fd); // 1.删除$dir的字符(./folder/file.txt删除./folder/) // 2.如果存在/就删除(/file.txt删除/) $file = substr($file, strlen($dir)); if(substr($file, 0, 1) == \\ || substr($file, 0, 1) == /){ $file = substr($file, 1); } $this->addfile($content, $file); } $out = $this->file(); $fp = fopen($savename, wb); fwrite($fp,$out,strlen($out)); fclose($fp); return true; } /** * 压缩并直接下载 * @param string $dir--需压缩的文件所在目录 */ public function zipanddownload($dir){ if(@!function_exists('gzcompress')){ return; } ob_end_clean(); $filelist = $this->visitfile($dir); if(count($filelist) == 0){ return; } foreach($filelist as $file){ if(!file_exists($file) || !is_file($file)){ continue; } $fd = fopen($file, rb); $content = @fread($fd, filesize($file)); fclose($fd); // 1.删除$dir的字符(./folder/file.txt删除./folder/) // 2.如果存在/就删除(/file.txt删除/) $file = substr($file, strlen($dir)); if(substr($file, 0, 1) == \\ || substr($file, 0, 1) == /){ $file = substr($file, 1); } $this->addfile($content, $file); } $out = $this->file(); @header('content-encoding: none'); @header('content-type: application/zip'); @header('content-disposition: attachment ; filename=farticle'.date(ymdhis, time()).'.zip'); @header('pragma: no-cache'); @header('expires: 0'); print($out); } // ------------------------------------------------------ // // $archive = new phpzip(); // $zipfile = zip压缩文件名; // $savepath = 解压缩目录名; // $zipfile = $unzipfile; // $savepath = $unziptarget; // $array = $archive->getzipinnerfilesinfo($zipfile); // $filecount = 0; // $dircount = 0; // $failfiles = array(); // set_time_limit(0); // 修改为不限制超时时间(默认为30秒) // for($i=0; $iunzip($zipfile, $savepath, $i) > 0){ // $filecount++; // }else{ // $failfiles[] = $array[$i][filename]; // } // }else{ // $dircount++; // } // } // set_time_limit(30); // printf(文件夹:%d 解压文件:%d 失败:%d
\r\n, $dircount, $filecount, count($failfiles)); // if(count($failfiles) > 0){ // foreach($failfiles as $file){ // printf(·%s
\r\n, $file); // } // } // ------------------------------------------------------// public 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; $ireadcentralfileheaders($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; } // ------------------------------------------------------ // // #获取被压缩文件的信息 // // $archive = new phpzip(); // $array = $archive->getzipinnerfilesinfo(zip压缩文件名); // for($i=0; $i $value) // printf(%s => %s
\r\n, $key, $value); // print \r\n------------------------------------\r\n\r\n; // } // ------------------------------------------------------ // public function getzipinnerfilesinfo($zipfile){ $zip = @fopen($zipfile, 'rb'); if(!$zip){ return(0); } $centd = $this->readcentraldir($zip, $zipfile); @rewind($zip); @fseek($zip, $centd['offset']); $ret = array(); for($i=0; $ireadcentralfileheaders($zip); $header['index'] = $i; $info = array('filename' => $header['filename'],// 文件名 'stored_filename' => $header['stored_filename'], // 压缩后文件名 'size' => $header['size'], // 大小 'compressed_size' => $header['compressed_size'], // 压缩后大小 'crc' => strtoupper(dechex($header['crc'])), // crc32 'mtime' => date(y-m-d h:i:s,$header['mtime']), // 文件修改时间 'comment' => $header['comment'], // 注释 'folder' => ($header['external'] == 0x41ff0010 || $header['external'] == 16) ? 1 : 0, // 是否为文件夹 'index' => $header['index'], // 文件索引 'status' => $header['status'] // 状态 ); $ret[] = $info; unset($header); } fclose($zip); return $ret; } // ------------------------------------------------------ // // #获取压缩文件的注释 // // $archive = new phpzip(); // echo $archive->getzipcomment(zip压缩文件名); // ------------------------------------------------------ // public function getzipcomment($zipfile){ $zip = @fopen($zipfile, 'rb'); if(!$zip){ return(0); } $centd = $this->readcentraldir($zip, $zipfile); fclose($zip); return $centd[comment]; } private function unix2dostime($unixtime = 0){ $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); if($timearray['year'] < 1980){ $timearray['year'] = 1980; $timearray['mon'] = 1; $timearray['mday'] = 1; $timearray['hours'] = 0; $timearray['minutes'] = 0; $timearray['seconds'] = 0; } return (($timearray['year'] - 1980) << 25)| ($timearray['mon'] << 21)| ($timearray['mday'] << 16)| ($timearray['hours']
?
