php打包备份好的数据库文件(也可打包别的文件)

 
 
  1. <?php 
  2. $z = new PHPZip(); 
  3. $file = array('1.jpg'); 
  4. $zipname = time() . '.zip'
  5. $z->Zip($file$zipname); 
  6.  
  7. /** 
  8. 以下是其他调用这个类的方法: 
  9. $z = new PHPZip(); //新建立一个zip的类 
  10.  
  11. 方法一: 
  12. $z -> Zip("", "out1.zip"); //添加当前目录和子目录下的所有档案 
  13.  
  14. 方法二: 
  15. $files=array('1.txt','gb.txt'); 
  16. $files[]='5.txt'; 
  17. $z -> Zip($files, "out2.zip"); //添加文件列表 
  18.  
  19. 方法三: 
  20. $z -> Zip("/usr/local/sext/", "out3.zip"); //添加指定目录 
  21. */ 
  22.  
  23. class PHPZip 
  24. function Zip($dir$zipfilename
  25. if (@function_exists('gzcompress')) 
  26. $curdir = getcwd(); 
  27. if (is_array($dir)) 
  28. $filelist = $dir
  29. else 
  30. $filelist = $this -> GetFileList($dir); 
  31.  
  32. if ((!emptyempty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir); 
  33. else chdir($curdir); 
  34.  
  35. if (count($filelist)>0) 
  36. foreach($filelist as $filename
  37. if (is_file($filename)) 
  38. $fd = fopen ($filename"r"); 
  39. $content = fread ($fdfilesize ($filename)); 
  40. fclose ($fd); 
  41.  
  42. if (is_array($dir)) $filename = basename($filename); 
  43. $this -> addFile($content$filename); 
  44. $out = $this -> file(); 
  45.  
  46. chdir($curdir); 
  47. $fp = fopen($zipfilename"w"); 
  48. fwrite($fp$outstrlen($out)); 
  49. fclose($fp); 
  50. return 1; 
  51. else return 0; 
  52.  
  53. function GetFileList($dir
  54. if (file_exists($dir)) 
  55. $args = func_get_args(); 
  56. $pref = $args[1]; 
  57.  
  58. $dh = opendir($dir); 
  59. while($files = readdir($dh)) 
  60. if (($files!=".")&&($files!="..")) 
  61. if (is_dir($dir.$files)) 
  62. $curdir = getcwd(); 
  63. chdir($dir.$files); 
  64. $file = array_merge($file$this -> GetFileList("""$pref$files/")); 
  65. chdir($curdir); 
  66. else $file[]=$pref.$files
  67. closedir($dh); 
  68. return $file
  69.  
  70. var $datasec      = array(); 
  71. var $ctrl_dir     = array(); 
  72. var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"
  73. var $old_offset   = 0; 
  74.  
  75. /** 
  76. * Converts an Unix timestamp to a four byte DOS date and time format (date 
  77. * in high two bytes, time in low two bytes allowing magnitude comparison). 
  78. * 
  79. * @param integer the current Unix timestamp 
  80. * 
  81. * @return integer the current date in a four byte DOS format 
  82. * 
  83. * @access private 
  84. */ 
  85. function unix2DosTime($unixtime = 0) { 
  86. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); 
  87.  
  88. if ($timearray['year'] < 1980) { 
  89. $timearray['year']    = 1980; 
  90. $timearray['mon']     = 1; 
  91. $timearray['mday']    = 1; 
  92. $timearray['hours']   = 0; 
  93. $timearray['minutes'] = 0; 
  94. $timearray['seconds'] = 0; 
  95. // end if 
  96.  
  97. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | 
  98. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); 
  99. // end of the 'unix2DosTime()' method 
  100.  
  101.  
  102. /** 
  103. * Adds "file" to archive 
  104. * 
  105. * @param string   file contents 
  106. * @param string   name of the file in the archive (may contains the path) 
  107. * @param integer the current timestamp 
  108. * 
  109. * @access public 
  110. */ 
  111. function addFile($data$name$time = 0) 
  112. $name     = str_replace('\\', '/', $name); 
  113.  
  114. $dtime    = dechex($this->unix2DosTime($time)); 
  115. $hexdtime = '\x' . $dtime[6] . $dtime[7] 
  116. '\x' . $dtime[4] . $dtime[5] 
  117. '\x' . $dtime[2] . $dtime[3] 
  118. '\x' . $dtime[0] . $dtime[1]; 
  119. eval('$hexdtime = "' . $hexdtime . '";'); 
  120.  
  121. $fr   = "\x50\x4b\x03\x04"
  122. $fr   .= "\x14\x00";            // ver needed to extract 
  123. $fr   .= "\x00\x00";            // gen purpose bit flag 
  124. $fr   .= "\x08\x00";            // compression method 
  125. $fr   .= $hexdtime;             // last mod time and date 
  126.  
  127. // "local file header" segment 
  128. $unc_len = strlen($data); 
  129. $crc     = crc32($data); 
  130. $zdata   = gzcompress($data); 
  131. $c_len   = strlen($zdata); 
  132. $zdata   = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug 
  133. $fr      .= pack('V'$crc);             // crc32 
  134. $fr      .= pack('V'$c_len);           // compressed filesize 
  135. $fr      .= pack('V'$unc_len);         // uncompressed filesize 
  136. $fr      .= pack('v'strlen($name));    // length of filename 
  137. $fr      .= pack('v', 0);                // extra field length 
  138. $fr      .= $name
  139.  
  140. // "file data" segment 
  141. $fr .= $zdata
  142.  
  143. // "data descriptor" segment (optional but necessary if archive is not 
  144. // served as file) 
  145. $fr .= pack('V'$crc);                 // crc32 
  146. $fr .= pack('V'$c_len);               // compressed filesize 
  147. $fr .= pack('V'$unc_len);             // uncompressed filesize 
  148.  
  149. // add this entry to array 
  150. $this -> datasec[] = $fr
  151. $new_offset        = strlen(implode(''$this->datasec)); 
  152.  
  153. // now add to central directory record 
  154. $cdrec = "\x50\x4b\x01\x02"
  155. $cdrec .= "\x00\x00";                // version made by 
  156. $cdrec .= "\x14\x00";                // version needed to extract 
  157. $cdrec .= "\x00\x00";                // gen purpose bit flag 
  158. $cdrec .= "\x08\x00";                // compression method 
  159. $cdrec .= $hexdtime;                 // last mod time & date 
  160. $cdrec .= pack('V'$crc);           // crc32 
  161. $cdrec .= pack('V'$c_len);         // compressed filesize 
  162. $cdrec .= pack('V'$unc_len);       // uncompressed filesize 
  163. $cdrec .= pack('v'strlen($name) ); // length of filename 
  164. $cdrec .= pack('v', 0 );             // extra field length 
  165. $cdrec .= pack('v', 0 );             // file comment length 
  166. $cdrec .= pack('v', 0 );             // disk number start 
  167. $cdrec .= pack('v', 0 );             // internal file attributes 
  168. $cdrec .= pack('V', 32 );            // external file attributes - 'archive' bit set 
  169.  
  170. $cdrec .= pack('V'$this -> old_offset ); // relative offset of local header 
  171. $this -> old_offset = $new_offset
  172.  
  173. $cdrec .= $name
  174.  
  175. // optional extra field, file comment goes here 
  176. // save to central directory 
  177. $this -> ctrl_dir[] = $cdrec
  178. // end of the 'addFile()' method 
  179.  
  180.  
  181. /** 
  182. * Dumps out file 
  183. * 
  184. * @return string the zipped file 
  185. * 
  186. * @access public 
  187. */ 
  188. function file() 
  189. $data    = implode(''$this -> datasec); 
  190. $ctrldir = implode(''$this -> ctrl_dir); 
  191.  
  192. return 
  193. $data . 
  194. $ctrldir . 
  195. $this -> eof_ctrl_dir . 
  196. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk" 
  197. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall 
  198. pack('V'strlen($ctrldir)) .           // size of central dir 
  199. pack('V'strlen($data)) .              // offset to start of central dir 
  200. "\x00\x00";                             // .zip file comment length 
  201. // end of the 'file()' method 
  202.  
  203.  
  204. // end of the 'PHPZip' class 
  205.  
  206. ?> 

 

      本文转自许琴 51CTO博客,原文链接:http://blog.51cto.com/xuqin/882336,如需转载请自行联系原作者


php版mysql大数据库备份和恢复工具,这是亮仔修改的无乱码版 在原faisunSQL 4.0的基础上,针对数据备份过程中出现乱码的问题,做了优化. 增强的功能: 1.自动识别数据库版本,对于MySQL 4.1以上,备份数据时提示选择字符集. 2.导入数据时,提示原数据库编码,并自动识别. 3.增加导入目标数据库字符集选项. 4.支持GBK、BIG5、UTF8之间的编码转换(见特别说明4). 特别说明: 1.乱码问题一般仅出现在MySQL 4.1/MySQL 5 版本以后,如果你的数据库低于这个版本,基本可以不用考虑这个问题. 2.确保原始数据的完整是至关重要的.就算导出时出现乱码,但只要原始数据完整,总有解决的办法.所以,导出时数据库字符集的选择必须正确,保证导出数据无乱码.一般为GBK,UTF8或Latin1.导出后,可以用文本编辑器先查看一下,看是否出现问号(?)等乱码. 3.导出和导入数据编码要保持一致(见特别说明4). 4.虽然程序目前支持GBK、BIG5、UTF8之间的编码转换,但这种转换不是安全的.首先你的目标导入服务器要支持iconv,即在导入时如果"编码转换功能"提示为支持,则可以使用此功能.反之则不可以.其次,转换时的数据必须是"干净"的.即GBK、BIG5、UTF8不能混合.如果你想将原来备份出的GBK数据导入到编码为UTF8数据库,则你的GBK数据中仅能含有GBK或GB2312的简体中文字符.不可以出现BIG5等繁体字符,否则转换将失败.基本上,一般的博客/论坛数据都不能保证这种纯净性,谁也不能保证你的文章中不会混合使用简体和繁体文字,所以这种跨字符集的导入导出数据难度很大.绝对不要轻易尝试这种游戏.目前的主流论坛如Discuz、PHPWind等都提供支持GBK、BIG5和UTF8的不同程序.你在最初安装时,一定先想好自己需要那种字符的程序,一旦选定,以后不是迫不得已,不要更改.以上仅针对 5.鉴于上面特别说明4,如果你是从MySQL 4.0.X/MySQL 3的老数据版本导入到MySQL 4.1/MySQL 5的高数据库版本,导入时请选择GBK编码.如果是UTF8编码的数据,如我的博客(http://www.zhouliang.name)采用WordPress程序,默认使用UTF8编码,则只能在MySQL 4.1/MySQL 5以上的数据库中导入导出,因为低版本的MySQL不支持UTF8. 6.以上说明仅针对本程序而言,在编码转换方面,不排除通过其他手段实现的可能性. 程序使用中出现任何问题(编码转换方面),欢迎与我联络: 我的E-Mail: php@zhouliang.name 本程序讨论主页:http://www.zhouliang.name/archives/198.htm 我的博客:http://www.zhouliang.name 此程序只是针对"乱码"问题做了改进,faisunSQL 4.0其他方面的故有功能效率与本增强版无关,如有问题请联系原作者.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值