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,如需转载请自行联系原作者


相关文章
|
2小时前
|
存储 数据库
LabVIEW如何修复或重置NI MAX数据库文件
LabVIEW如何修复或重置NI MAX数据库文件
10 0
|
2小时前
|
SQL 存储 监控
关系型数据库做好备份
【5月更文挑战第4天】关系型数据库做好备份
30 6
关系型数据库做好备份
|
2小时前
|
存储 JSON 关系型数据库
Percona XtraBackup是否支持Elasticsearch数据库备份?
【5月更文挑战第13天】Percona XtraBackup是否支持Elasticsearch数据库备份?
7 1
|
2小时前
|
NoSQL 关系型数据库 MySQL
Percona XtraBackup是否支持Redis数据库备份?
【5月更文挑战第13天】Percona XtraBackup是否支持Redis数据库备份?
9 2
|
2小时前
|
存储 NoSQL 关系型数据库
Percona XtraBackup是否支持MongoDB数据库备份?
【5月更文挑战第13天】Percona XtraBackup是否支持MongoDB数据库备份?
14 1
|
2小时前
|
存储 关系型数据库 MySQL
Percona XtraBackup是否支持PostgreSQL数据库备份?
【5月更文挑战第13天】Percona XtraBackup是否支持PostgreSQL数据库备份?
30 1
|
2小时前
|
关系型数据库 MySQL Linux
服务器Linux系统配置mysql数据库主从自动备份
这是一个基本的配置主从复制和设置自动备份的指南。具体的配置细节和命令可能因您的环境和需求而有所不同,因此建议在操作前详细阅读MySQL文档和相关资源,并谨慎操作以避免数据丢失或不一致。
12 3
|
2小时前
|
关系型数据库 MySQL 数据库连接
用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
用Navicat备份Mysql演示系统数据库的时候出:Too Many Connections
18 0
|
2小时前
|
关系型数据库 MySQL 数据库
docker MySQL删除数据库时的错误(errno: 39)
docker MySQL删除数据库时的错误(errno: 39)
14 0
|
2小时前
|
关系型数据库 MySQL 数据库
mysql 设置环境变量与未设置环境变量连接数据库的区别
设置与未设置MySQL环境变量在连接数据库时主要区别在于命令输入方式和系统便捷性。设置环境变量后,可直接使用`mysql -u 用户名 -p`命令连接,而无需指定完整路径,提升便利性和灵活性。未设置时,需输入完整路径如`C:\Program Files\MySQL\...`,操作繁琐且易错。为提高效率和减少错误,推荐安装后设置环境变量。[查看视频讲解](https://www.bilibili.com/video/BV1vH4y137HC/)。
21 3
mysql 设置环境变量与未设置环境变量连接数据库的区别