给你一个类以下是一个完整的文件,用的时候你只要传入路径就可以了比如:
if (is_uploaded_file($_FILES["fileId0"]['tmp_name']))
{
$readzip = new readzip($_FILES"fileId0"]'tmp_name'],"/x/test/htdocs/manage/photo");//],"/x/test/htdocs/manage/photo这个是临时存放路径;
$readzip->unzip($orderPath);//$orderPath 是解压后文件的存放路径。你上传得zip文件是什么格式内容,解压后也是什么格式内容
}下面的类上传得一定是.zip的文件,.rar的不行。
if (is_uploaded_file($_FILES["fileId0"]['tmp_name']))
{
$readzip = new readzip($_FILES"fileId0"]'tmp_name'],"/x/test/htdocs/manage/photo");//],"/x/test/htdocs/manage/photo这个是临时存放路径;
$readzip->unzip($orderPath);//$orderPath 是解压后文件的存放路径。你上传得zip文件是什么格式内容,解压后也是什么格式内容
}下面的类上传得一定是.zip的文件,.rar的不行。
class readzip{
var $ctrl_dir = array();
var $contentlist = array();
var $filename = "";
var $read = 0;
var $filehandle = "";
var $tmpbasedir = "";
function readzip($filename,$tempdir){
if(!file_exists($filename)) $this->error("file not found");
$this->filename=$filename;
//$this->tmpbasedir = "c:";
$this->tmpbasedir = $tempdir;//"/x/test/htdocs/manage/photo";
$this->open();
$this->readcontent();
$this->close();
}
function readcontent(){
$this->contentlist=array();
$this->read_ctrl_dir();
@fseek($this->filehandle,$this->ctrl_dir['offset']); for($i=0;$i<$this->ctrl_dir['entries'];$i++){
$this->contentlist[$i]=$this->read_centralfileheader();
$this->contentlist[$i]['index']=$i;
}
$this->read=1;
}
function unzip($destpath=""){
if($this->read==0){
$this->open();
$this->readcontent();
}
$count = $this->ctrl_dir['entries'];
for($i=0;$i<$count;$i++){
$tempfile = $this->writetmpfile($i);
$dest = "";
$filename = $this->contentlist[$i]['filename'];
if($destpath=="") $dest = "./".$filename;
else $dest = $destpath."/".$filename;
$dest = str_replace("\\","/",$dest);
$dir = dirname($dest);
if(!is_dir($dir)){
mkdir($dir);
}
$this->unziptmp($i,$tempfile,$dest);
$tmpdir = dirname($tempfile);
if($tmpdir!="." && $tmpdir!="..") rmdir($tmpdir);
}
}
function unziptmp($index,$tmpfile,$dest){
$this->open();
if($this->read==0) $this->readcontent();
if(!isset($this->contentlist[$index])) $this->error("cannot find file in archive");
if($this->contentlist[$index]['type']=="folder") return 0;//$this->error("cannot unzip a folder, only files");
if(!file_exists($tmpfile)) $this->error("cannot find temporary file $tmpfile for read");
if($this->contentlist[$index]['compression']==0){ //uncompressed
$copy=@copy($tmpfile,$dest);
if(!$copy) $this->error("cannot copy temorary file $tmpfile to $dest");
}
else{ //compressed
if(!extension_loaded("zlib") || !function_exists("gzopen")) $this->error("zlib library required");
$tmp_fh=@gzopen($tmpfile,"rb");
if(!$tmp_fh) $this->error("cannot open temporary file $tmpfile for read");
$fh=@fopen($dest,"wb");
if(!$fh) $this->error("cannot open file $dest for write");
$size = $this->contentlist[$index]['size'];
while($size){
$read_size = ($size < 2048 ? $size : 2048);
@fwrite($fh,pack('a'.$read_size,@gzread($tmp_fh, $read_size)),$read_size);
$size -= $read_size;
}
@gzclose($tmp_fh);
@fclose($fh);
}
chmod($dest,0777);
unlink($tmpfile);
$this->close();
}
function writetmpfile($index){
$this->open();
if($this->read==0) $this->readcontent();
if(!isset($this->contentlist[$index])) $this->error("cannot find file in archive");
if($this->contentlist[$index]['type']=="folder") return 0;
fseek($this->filehandle,($this->contentlist[$index]['offset']+$this->contentlist[$index]['filename_len']+$this->contentlist[$index]['extra_len']+30));
if($this->contentlist[$index]['compression']==0){
$tmpfile=$this->tmpbasedir."/temp/uncompress_".$index."_".md5(uniqid(rand()));
$tmpfile=str_replace("\\","/",$tmpfile);
while(file_exists($tmpfile)){
$tmpfile=$this->tmpbasedir."/temp/uncompress_".$index."_".md5(uniqid(rand()));
$tmpfile=str_replace("\\","/",$tmpfile);
}
}
else{
$tmpfile=$this->tmpbasedir."/temp/uncompress_".$index."_".md5(uniqid(rand())).".gz";
$tmpfile=str_replace("\\","/",$tmpfile);
while(file_exists($tmpfile)){
$tmpfile=$this->tmpbasedir."/temp/uncompress_".$index."_".md5(uniqid(rand())).".gz";
$tmpfile=str_replace("\\","/",$tmpfile);
}
}
$dir = dirname($tmpfile);
if(!is_dir($dir)) mkdir($dir);
$tmp_fh=@fopen($tmpfile,"wb");
if(!$tmp_fh) $this->error("cannot open temporary file $tmpfile for write");
if($this->contentlist[$index]['compression']==0){ // uncompressed
$size = $this->contentlist[$index]['compressed_size'];
while($size){
$read_size = ($size < 2048 ? $size : 2048);
@fwrite($tmp_fh,pack('a'.$read_size,@fread($this->filehandle, $read_size)),$read_size);
$size -= $read_size;
}
}
else{ // compressed
$binary_data = pack('va1a1Va1a1', 0x8b1f, Chr($this->contentlist[$index]['compression']), Chr(0x00), time(), Chr(0x00), Chr(3));
@fwrite($tmp_fh, $binary_data, 10);
$size = $this->contentlist[$index]['compressed_size'];
while($size){
$read_size = ($size < 2048 ? $size : 2048);
@fwrite($tmp_fh,pack('a'.$read_size,@fread($this->filehandle, $read_size)),$read_size);
$size -= $read_size;
}
@fwrite($tmp_fh,pack('VV', $this->contentlist[$index]['crc'], $this->contentlist[$index]['size']), 8);
} @fclose($tmp_fh);
chmod($tmpfile,0777);
$this->close();
return $tmpfile;
}
function read_ctrl_dir(){
$this->ctrl_dir=array();
$size=@filesize($this->filename);
$maximum_size=277;
if($maximum_size>$size) $maximum_size=$size;
@fseek($this->filehandle,($size-$maximum_size));
$pos = @ftell($this->filehandle);
$bytes = 0x00000000;
while ($pos < $size){
$pos++;
$byte = fread($this->filehandle, 1);
$bytes = ($bytes << 8) | Ord($byte);
if($bytes==0x504b0506) break;
}
$binary_data = @fread($this->filehandle,18);
$data = unpack('vdisk/vdisk_start/vdisk_entries/ventries/Vsize/Voffset/vcomment_size', $binary_data); if($data['comment_size'] != 0) $this->ctrl_dir['comment'] = @fread($this->filehandle, $data['comment_size']);
else $this->ctrl_dir['comment'] = '';
$this->ctrl_dir['entries'] = $data['entries'];
$this->ctrl_dir['disk_entries'] = $data['disk_entries'];
$this->ctrl_dir['offset'] = $data['offset'];
$this->ctrl_dir['size'] = $data['size'];
$this->ctrl_dir['disk'] = $data['disk'];
$this->ctrl_dir['disk_start'] = $data['disk_start'];
}
function read_centralfileheader(){
$binary_data = @fread($this->filehandle, 4);
$data = unpack('Vid', $binary_data);
$binary_data = @fread($this->filehandle,42);
$header = unpack('vversion/vversion_extracted/vflag/vcompression/vmtime/vmdate/Vcrc/Vcompressed_size/Vsize/vfilename_len/vextra_len/vcomment_len/vdisk/vinternal/Vexternal/Voffset', $binary_data);
if($header['filename_len']) $header['filename'] = fread($this->filehandle,$header['filename_len']);
else $header['filename'] = "";
if($header['extra_len']) $header['extra'] = fread($this->filehandle,$header['extra_len']);
else $header['extra'] = "";
if($header['comment_len']) $header['comment'] = fread($this->filehandle,$header['comment_len']);
else $header['comment'] = "";
if ($header['mdate'] && $header['mtime'])
{
$hour = ($header['mtime'] & 0xF800) >> 11;
$minute = ($header['mtime'] & 0x07E0) >> 5;
$second = ($header['mtime'] & 0x001F)*2;
$year = (($header['mdate'] & 0xFE00) >> 9) + 1980;
$month = ($header['mdate'] & 0x01E0) >> 5;
$day = $header['mdate'] & 0x001F;
$header['mtime'] = mktime($hour, $minute, $second, $month, $day, $year);
}
else $header['mtime'] = time();
if(substr($header['filename'], -1) == '/') $header['external'] = 0x41FF0010;
if($header['external']==0x41FF0010) $header['type']="folder";
else $header['type']="file"; return $header;
}
function open(){
$this->filehandle = @fopen($this->filename,"rb");
if(!$this->filehandle) $this->error("cannot open archive ".$this->filename);
} function close(){
@fclose($this->filehandle);
unset($this->filehandle);
}
function error($msg){
$this->close();
die("<b>Fatal error:</b> ".$msg);
}
}
?>
$zip = zip_open("e:\\phpwww\\3dep\\abc.zip");
if ($zip){
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Actual Filesize: " . zip_entry_filesize($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Compression Method: " . zip_entry_compressionmethod($zip_entry) . "\n";
$name=zip_entry_name($zip_entry);
$fileSize=zip_entry_filesize($zip_entry);
echo $name."<br>";
if (zip_entry_open($zip, $zip_entry, "r")) {
echo "\nFile Contents:\n";
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
zip_entry_close($zip_entry);
}
$fp=fopen($name,"w");
fwrite($fp,$buf,$fileSize);
fclose($fp);
echo "\n";
}
zip_close($zip);
}
?>
我这样解决的,很简单的