这一天无事,我看论坛有人问了php生成压缩文件的问题,记得以前做项目,我们做的项目中就有一个将整个程序打包成zip的功能,但是这个功能不是我写的,那个时候因为忙也是简单的看了一下,现在有时间,又遇到了这个问题,于是就又仔细的看了下。把这个类拿出来,大家分享一下。
phpZip.class.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
set_time_limit(0); class PhpZip { var $file_count = 0 ; var $datastr_len = 0; var $dirstr_len = 0; var $filedata = ''; //该变量只被类外部程序访问 var $gzfilename; var $fp; var $dirstr=''; var $filefilters = array(); function SetFileFilter($filetype) { $this->filefilters = explode('|',$filetype); } //返回文件的修改时间格式. //只为本类内部函数调用. 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'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); } //初始化文件,建立文件目录, //并返回文件的写入权限. function startfile($path = 'dodo.zip'){ $this->gzfilename=$path; $mypathdir=array(); do { $mypathdir[] = $path = dirname($path); } while($path != '.'); @end($mypathdir); do { $path = @current($mypathdir); @mkdir($path); } while(@prev($mypathdir)); if($this->fp=@fopen($this->gzfilename,"w")){ return true; } return false; } //添加一个文件到 zip 压缩包中. function addfile($data, $name) { $name = str_replace('\\', '/', $name); if(strrchr($name,'/')=='/') return $this->adddir($name); if(!empty($this->filefilters)) { if (!in_array(end(explode(".",$name)), $this->filefilters)) { return; } } $dtime = dechex($this->unix2DosTime()); $hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1]; eval('$hexdtime = "' . $hexdtime . '";'); $unc_len = strlen($data); $crc = crc32($data); $zdata = gzcompress($data); $c_len = strlen($zdata); $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); //新添文件内容格式化: $datastr = "\x50\x4b\x03\x04"; $datastr .= "\x14\x00"; // ver needed to extract $datastr .= "\x00\x00"; // gen purpose bit flag $datastr .= "\x08\x00"; // compression method $datastr .= $hexdtime; // last mod time and date $datastr .= pack('V', $crc); // crc32 $datastr .= pack('V', $c_len); // compressed filesize $datastr .= pack('V', $unc_len); // uncompressed filesize $datastr .= pack('v', strlen($name)); // length of filename $datastr .= pack('v', 0); // extra field length $datastr .= $name; $datastr .= $zdata; $datastr .= pack('V', $crc); // crc32 $datastr .= pack('V', $c_len); // compressed filesize $datastr .= pack('V', $unc_len); // uncompressed filesize fwrite($this->fp,$datastr); //写入新的文件内容 $my_datastr_len = strlen($datastr); unset($datastr); //新添文件目录信息 $dirstr = "\x50\x4b\x01\x02"; $dirstr .= "\x00\x00"; // version made by $dirstr .= "\x14\x00"; // version needed to extract $dirstr .= "\x00\x00"; // gen purpose bit flag $dirstr .= "\x08\x00"; // compression method $dirstr .= $hexdtime; // last mod time & date $dirstr .= pack('V', $crc); // crc32 $dirstr .= pack('V', $c_len); // compressed filesize $dirstr .= pack('V', $unc_len); // uncompressed filesize $dirstr .= pack('v', strlen($name) ); // length of filename $dirstr .= pack('v', 0 ); // extra field length $dirstr .= pack('v', 0 ); // file comment length $dirstr .= pack('v', 0 ); // disk number start $dirstr .= pack('v', 0 ); // internal file attributes $dirstr .= pack('V', 32 ); // external file attributes - 'archive' bit set $dirstr .= pack('V',$this->datastr_len ); // relative offset of local header $dirstr .= $name; $this->dirstr .= $dirstr; //目录信息 $this -> file_count ++; $this -> dirstr_len += strlen($dirstr); $this -> datastr_len += $my_datastr_len; } function adddir($name) { $name = str_replace("\\", "/", $name); $datastr = "\x50\x4b\x03\x04\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"; $datastr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); $datastr .= pack("v", 0 ).$name.pack("V", 0).pack("V", 0).pack("V", 0); fwrite($this->fp,$datastr); //写入新的文件内容 $my_datastr_len = strlen($datastr); unset($datastr); $dirstr = "\x50\x4b\x01\x02\x00\x00\x0a\x00\x00\x00\x00\x00\x00\x00\x00\x00"; $dirstr .= pack("V",0).pack("V",0).pack("V",0).pack("v", strlen($name) ); $dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 ); $dirstr .= pack("V", 16 ).pack("V",$this->datastr_len).$name; $this->dirstr .= $dirstr; //目录信息 $this -> file_count ++; $this -> dirstr_len += strlen($dirstr); $this -> datastr_len += $my_datastr_len; } function createfile() { //压缩包结束信息,包括文件总数,目录信息读取指针位置等信息 $endstr = "\x50\x4b\x05\x06\x00\x00\x00\x00" . pack('v', $this -> file_count) . pack('v', $this -> file_count) . pack('V', $this -> dirstr_len) . pack('V', $this -> datastr_len) . "\x00\x00"; fwrite($this->fp,$this->dirstr.$endstr); fclose($this->fp); } } |
本文链接:http://www.521php.com/archives/341/
程序本天成,妙手偶得之!我们只是代码的搬运工!
转载请注明:http://www.521php.com/archives/341/