- /*
- ?? ??: /include/zip.php
- ???: Horace 2009/04/15
- */
- class PHPZip{
- var $dirInfo = array("0","0");
- var $rootDir = '';
- var $datasec = ??();
- var $ctrl_dir = ??();
- var $eof_ctrl_dir = "/x50/x4b/x05/x06/x00/x00/x00/x00";
- var $old_offset = 0;
- function downloadZip(){
- createZip($dir, $zipfilename, true);
- }
- function createZip($dir, $zipfilename, $autoDownload = false){
- if (@function_exists('gzcompress')){
- @set_time_limit("0");
- if (is_array($dir)){
- $fd = fopen ($dir, "r");
-
- $fileValue = fread($fd, ?? ??($filename));
- fclose($fd);
- if (is_array($dir)) $filename = basename($dir);
- $?? -> addFile($fileValue, "$filename");
- }else{
- $this->dirTree($dir,$dir);
- }
- $zipfilenametemp = time().$zipfilename;
- $out = $this -> ??zip();
- $fp = fopen($zipfilenametemp, "w");
- fwrite($fp, $out, strlen($out));
- fclose($fp);
- $filesize = ?? ??($zipfilenametemp);
- if ($filesize < 104857600) {
- if($autoDownload){
- header("??? ??: ??????/??-???");
- header("Content-disposition: attachment; filename=".$zipfilename);
- }
- echo $this -> ??zip();
- }else{
- echo "zip ?? ??!";
- }
- unlink($zipfilenametemp);
- }
- }
- //dir ?? ????..
- function dirTree($directory,$rootDir){
- global $_SERVER,$dirInfo,$rootDir;
- $fileDir=$rootDir;
- $myDir=dir($directory);
- while($file=$myDir->read()){
-
-
- if(is_dir("$directory/$file") ? $file!="." ? $file !=".."){
- $dirInfo[0] ;
- $rootDir ="$fileDir$file/";
- $?? -> addFile('', "$rootDir");
- //n?? ??? ??
- $this->dirTree("$directory/$file",$rootDir);
- }else{
- if($file!="." and $file!=".."){
- $dirInfo[1] ;
- //$fd = fopen ("$directory/$file", "r");
- $fileValue = file_get_contents("$directory/$file");
- //fclose($fd);
- $?? -> addFile($fileValue, "$fileDir$file");
- }
- }
- }
- $myDir->close();
- }
- function unix2DosTime($unixtime = 0) {
- $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
- if ($timearray['??'] < 1980) {
- $timearray['??'] = 1980;
- $timearray['mon'] = 1;
- $timearray['mday'] = 1;
- $timearray['??'] = 0;
- $timearray['?'] = 0;
- $timearray['?'] = 0;
-
-
- } //
- ?? ?? return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
- ($timearray['hours'] << 11) | ($timearray['?'] << 5) | ($timearray['?'] >> 1);
- }
- function addFile($data, $name, $time = 0){
- $name = str_replace('//', '/', $name);
- $dtime = decex($this->unix2DosTime($time));
- $hexdtime = '/x' . $dtime[6] . $dtime[7]
- . '/x' . $dtime[4] . $dtime[5]
- . '/x' . $dtime[2] . $dtime[3]
- . '/x' . $dtime[0] . $dtime[1];
- eval('$hexdtime = "' . $hexdtime . '";');
- $fr = "/x50/x4b/x03/x04";
- $fr .= "/x14/x00"; //
- $fr .= "/x00/x00";? ???? ? ??? ?????. // ?? ?? ?? ???
- $fr .= "/x08/x00"; // ?? ??
- $fr .= $hexdtime; // ??? ?? ?? ? ??
- // "?? ?? ??" ????
- $unc_len = strlen($data);
- $crc = crc32($data);
- $zdata = gzcompress($data);
-
-
- $c_len = strlen($zdata);
- $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // crc ?? ??
- $fr .= pack('V', $crc); // crc32
- $fr .= pack('V', $c_len); // ??? ?? ??
- $fr .= pack('V', $unc_len); // ???? ?? ?? ??
- $fr .= pack('v', strlen($name)); // ?? ?? ??
- $fr .= pack('v', 0); // ?? ?? ??
- $fr .= $name;
- // "?? ???" ????
- $fr .= $zdata;
- // "??? ???" ????(?? ????? ????? ?? ?? ???
- // ??? ???)
- $fr .= pack('V', $crc); // crc32
- $fr .= pack('V', $c_len); // ??? ?? ??
- $fr .= pack('V', $unc_len); // ???? ?? ?? ??
- // ? ??? ??? ?????
- $this -> ????[] = $fr;
- $new_offset = strlen(implode('', $this->datasec));
- // ?? ?? ???? ???? ?????.
- $cdrec = "/x50/x4b/x01/x02";
- $cdrec .= "/x00/x00"; //
- ? ?? ???? ?? $cdrec .= "/x14/x00"; //
-
-
- ? ???? ? ??? ?? $cdrec .= "/x00/x00"; // ?? ?? ?? ???
- $cdrec .= "/x08/x00"; // ?? ??
- $cdrec .= $hexdtime; // ??? ?? ?? ? ??
- $cdrec .= pack('V', $crc); // crc32
- $cdrec .= pack('V', $c_len); // ??? ?? ??
- $cdrec .= pack('V', $unc_len); // ???? ?? ?? ??
- $cdrec .= pack('v', strlen($name) ); // ?? ?? ??
- $cdrec .= pack('v', 0 ); // ?? ?? ??
- $cdrec .= pack('v', 0 ); // ?? ?? ??
- $cdrec .= pack('v', 0 ); // ??? ?? ??
- $cdrec .= pack('v', 0 ); // ?? ?? ??
- $cdrec .= pack('V', 32 ); // ?? ?? ?? - '????' ?? ??
- $cdrec .= pack('V', $this -> old_offset ); // ?? ??? ?? ???
- $this -> old_offset = $new_offset;
- $cdrec .= $??;
- // ??? ?? ??, ?? ??? ??? ?????.
- // ?? ????? ??
- $this -> ctrl_dir[] = $cdrec;
- }
- function filezip(){
- $data = implode('', $this -> datasec);
- $ctrldir = implode('', $this -> ctrl_dir);
-
- $data? ?????.
- $ctrldir .
- $?? -> eof_ctrl_dir .
- pack('v', sizeof($this -> ctrl_dir)) . // "? ???? ??" ??? ? ??
- pack('v', sizeof($this -> ctrl_dir)) . // ?? ??? ? ??
- pack('V', strlen($ctrldir)) . // ?? ????? ??
- pack('V', strlen($data)) . // ?? ???? ?? ???
- "/x00/x00"; // .zip ?? ?? ??
- }
- }
- ?>
-
-
-
- 還有另??個(gè)比較簡(jiǎn)單?
-
- [php] ?? ?? ??
-
-
- ??? createzip
- {
- /* @??? zip ??? ?????.
- * @destination 目標(biāo)文件的路徑 如"c:/androidyue.zip"
- * @$overwrite 是否為覆蓋與目標(biāo)文件???件
- * @Recorded By Androidyue
- * @Blog: http://thinkblog.sinaapp.com
- */
- function create_zip($files = array(),$destination = '',$overwrite = false)
- {
- //如果zip文件已經(jīng)存在并且設(shè)置為不返回false
- if(file_exists($destination) && !$overwrite) { return false; }
- $valid_files = array();
- //??? ??? ??...
- //?? ??? ?? ?? ????
- if(is_array($files)) {
- //? ??? ??
- foreach($files as $file) {
- //??? ????? ??
- if(file_exists($file)) {
- $valid_files[] = $file ;
- }
- }
- }
- //?? ??? ??? ?? ??
- if(count($valid_files))
- {
- //???? ??
- $zip = new ZipArchive();
- //??? ???. ??? ?? ??? ?????.
- if($zip->open($destination,$overwrite) ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE ::CREATE) !== true) {
- return false;
- }
- //?? ??? ?? ??
- foreach($valid_files as $file) {
- $zip-> ;addFile($file,$file);
- }
- //?? ??
- $zip->close()
- //??? ??? ?? exists
- return file_exists($destination );
- }
- else{
- //?? ??? ??? ??? false? ?????.
- return false;
-
- }
- /* ***
- //??? ??
- $files=array('temp.php','test.php')
- create_zip($files, 'myzipfile.zip', true); ****/
- ?>
-
-
- ?? ??
|
????, php