PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/add-ons/archive/archive.php

https://github.com/jcplat/console-seolan
PHP | 778 lines | 247 code | 20 blank | 511 comment | 49 complexity | efdf5acb1743de54687600b97e3d1dbf MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /*--------------------------------------------------
  3. | TAR/GZIP/BZIP2/ZIP ARCHIVE CLASSES 2.0
  4. | By Devin Doucette
  5. | Copyright (c) 2004 Devin Doucette
  6. | Email: darksnoopy@shaw.ca
  7. +--------------------------------------------------
  8. | Email bugs/suggestions to darksnoopy@shaw.ca
  9. +--------------------------------------------------
  10. | This script has been created and released under
  11. | the GNU GPL and is free to use and redistribute
  12. | only if this copyright statement is not removed
  13. +--------------------------------------------------*/
  14. class archive
  15. {
  16. function archive($name)
  17. {
  18. $this->options = array(
  19. 'basedir'=>".",
  20. 'name'=>$name,
  21. 'prepend'=>"",
  22. 'inmemory'=>0,
  23. 'overwrite'=>0,
  24. 'recurse'=>1,
  25. 'storepaths'=>1,
  26. 'level'=>3,
  27. 'method'=>1,
  28. 'sfx'=>"",
  29. 'type'=>"",
  30. 'comment'=>""
  31. );
  32. $this->files = array();
  33. $this->exclude = array();
  34. $this->storeonly = array();
  35. $this->error = array();
  36. }
  37. function set_options($options)
  38. {
  39. foreach($options as $key => $value)
  40. {
  41. $this->options[$key] = $value;
  42. }
  43. if(!empty($this->options['basedir']))
  44. {
  45. $this->options['basedir'] = str_replace("\\","/",$this->options['basedir']);
  46. $this->options['basedir'] = preg_replace("/\/+/","/",$this->options['basedir']);
  47. $this->options['basedir'] = preg_replace("/\/$/","",$this->options['basedir']);
  48. }
  49. if(!empty($this->options['name']))
  50. {
  51. $this->options['name'] = str_replace("\\","/",$this->options['name']);
  52. $this->options['name'] = preg_replace("/\/+/","/",$this->options['name']);
  53. }
  54. if(!empty($this->options['prepend']))
  55. {
  56. $this->options['prepend'] = str_replace("\\","/",$this->options['prepend']);
  57. $this->options['prepend'] = preg_replace("/^(\.*\/+)+/","",$this->options['prepend']);
  58. $this->options['prepend'] = preg_replace("/\/+/","/",$this->options['prepend']);
  59. $this->options['prepend'] = preg_replace("/\/$/","",$this->options['prepend']) . "/";
  60. }
  61. }
  62. function create_archive()
  63. {
  64. $this->make_list();
  65. if($this->options['inmemory'] == 0)
  66. {
  67. $pwd = getcwd();
  68. chdir($this->options['basedir']);
  69. if($this->options['overwrite'] == 0 && file_exists($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip"? ".tmp" : "")))
  70. {
  71. $this->error[] = "File {$this->options['name']} already exists.";
  72. chdir($pwd);
  73. return 0;
  74. }
  75. else if($this->archive = @fopen($this->options['name'] . ($this->options['type'] == "gzip" || $this->options['type'] == "bzip"? ".tmp" : ""),"wb+"))
  76. {
  77. chdir($pwd);
  78. }
  79. else
  80. {
  81. $this->error[] = "Could not open {$this->options['name']} for writing.";
  82. chdir($pwd);
  83. return 0;
  84. }
  85. }
  86. else
  87. {
  88. $this->archive = "";
  89. }
  90. switch($this->options['type'])
  91. {
  92. case "zip":
  93. if(!$this->create_zip())
  94. {
  95. $this->error[] = "Could not create zip file.";
  96. return 0;
  97. }
  98. break;
  99. case "bzip":
  100. if(!$this->create_tar())
  101. {
  102. $this->error[] = "Could not create tar file.";
  103. return 0;
  104. }
  105. if(!$this->create_bzip())
  106. {
  107. $this->error[] = "Could not create bzip2 file.";
  108. return 0;
  109. }
  110. break;
  111. case "gzip":
  112. if(!$this->create_tar())
  113. {
  114. $this->error[] = "Could not create tar file.";
  115. return 0;
  116. }
  117. if(!$this->create_gzip())
  118. {
  119. $this->error[] = "Could not create gzip file.";
  120. return 0;
  121. }
  122. break;
  123. case "tar":
  124. if(!$this->create_tar())
  125. {
  126. $this->error[] = "Could not create tar file.";
  127. return 0;
  128. }
  129. }
  130. if($this->options['inmemory'] == 0)
  131. {
  132. fclose($this->archive);
  133. if($this->options['type'] == "gzip" || $this->options['type'] == "bzip")
  134. {
  135. unlink($this->options['basedir'] . "/" . $this->options['name'] . ".tmp");
  136. }
  137. }
  138. }
  139. function add_data($data)
  140. {
  141. if($this->options['inmemory'] == 0)
  142. {
  143. fwrite($this->archive,$data);
  144. }
  145. else
  146. {
  147. $this->archive .= $data;
  148. }
  149. }
  150. function make_list()
  151. {
  152. if(!empty($this->exclude))
  153. {
  154. foreach($this->files as $key => $value)
  155. {
  156. foreach($this->exclude as $current)
  157. {
  158. if($value['name'] == $current['name'])
  159. {
  160. unset($this->files[$key]);
  161. }
  162. }
  163. }
  164. }
  165. if(!empty($this->storeonly))
  166. {
  167. foreach($this->files as $key => $value)
  168. {
  169. foreach($this->storeonly as $current)
  170. {
  171. if($value['name'] == $current['name'])
  172. {
  173. $this->files[$key]['method'] = 0;
  174. }
  175. }
  176. }
  177. }
  178. unset($this->exclude,$this->storeonly);
  179. }
  180. function add_files($list)
  181. {
  182. $temp = $this->list_files($list);
  183. foreach($temp as $current)
  184. {
  185. $this->files[] = $current;
  186. }
  187. }
  188. function exclude_files($list)
  189. {
  190. $temp = $this->list_files($list);
  191. foreach($temp as $current)
  192. {
  193. $this->exclude[] = $current;
  194. }
  195. }
  196. function store_files($list)
  197. {
  198. $temp = $this->list_files($list);
  199. foreach($temp as $current)
  200. {
  201. $this->storeonly[] = $current;
  202. }
  203. }
  204. function list_files($list)
  205. {
  206. if(!is_array($list))
  207. {
  208. $temp = $list;
  209. $list = array($temp);
  210. unset($temp);
  211. }
  212. $files = array();
  213. $pwd = getcwd();
  214. chdir($this->options['basedir']);
  215. foreach($list as $current)
  216. {
  217. $current = str_replace("\\","/",$current);
  218. $current = preg_replace("/\/+/","/",$current);
  219. $current = preg_replace("/\/$/","",$current);
  220. if(strstr($current,"*"))
  221. {
  222. $regex = preg_replace("/([\\\^\$\.\[\]\|\(\)\?\+\{\}\/])/","\\\\\\1",$current);
  223. $regex = str_replace("*",".*",$regex);
  224. $dir = strstr($current,"/")? substr($current,0,strrpos($current,"/")) : ".";
  225. $temp = $this->parse_dir($dir);
  226. foreach($temp as $current2)
  227. {
  228. if(preg_match("/^{$regex}$/i",$current2['name']))
  229. {
  230. $files[] = $current2;
  231. }
  232. }
  233. unset($regex,$dir,$temp,$current);
  234. }
  235. else if(@is_dir($current))
  236. {
  237. $temp = $this->parse_dir($current);
  238. foreach($temp as $file)
  239. {
  240. $files[] = $file;
  241. }
  242. unset($temp,$file);
  243. }
  244. else if(@file_exists($current))
  245. {
  246. $files[] = array('name'=>$current,'name2'=>$this->options['prepend'] .
  247. preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($current,"/"))?
  248. substr($current,strrpos($current,"/") + 1) : $current),'type'=>0,
  249. 'ext'=>substr($current,strrpos($current,".")),'stat'=>stat($current));
  250. }
  251. }
  252. chdir($pwd);
  253. unset($current,$pwd);
  254. usort($files,array("archive","sort_files"));
  255. return $files;
  256. }
  257. function parse_dir($dirname)
  258. {
  259. if($this->options['storepaths'] == 1 && !preg_match("/^(\.+\/*)+$/",$dirname))
  260. {
  261. $files = array(array('name'=>$dirname,'name2'=>$this->options['prepend'] .
  262. preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($dirname,"/"))?
  263. substr($dirname,strrpos($dirname,"/") + 1) : $dirname),'type'=>5,'stat'=>stat($dirname)));
  264. }
  265. else
  266. {
  267. $files = array();
  268. }
  269. $dir = @opendir($dirname);
  270. while($file = @readdir($dir))
  271. {
  272. if($file == "." || $file == "..")
  273. {
  274. continue;
  275. }
  276. else if(@is_dir($dirname."/".$file))
  277. {
  278. if(empty($this->options['recurse']))
  279. {
  280. continue;
  281. }
  282. $temp = $this->parse_dir($dirname."/".$file);
  283. foreach($temp as $file2)
  284. {
  285. $files[] = $file2;
  286. }
  287. }
  288. else if(@file_exists($dirname."/".$file))
  289. {
  290. $files[] = array('name'=>$dirname."/".$file,'name2'=>$this->options['prepend'] .
  291. preg_replace("/(\.+\/+)+/","",($this->options['storepaths'] == 0 && strstr($dirname."/".$file,"/"))?
  292. substr($dirname."/".$file,strrpos($dirname."/".$file,"/") + 1) : $dirname."/".$file),'type'=>0,
  293. 'ext'=>substr($file,strrpos($file,".")),'stat'=>stat($dirname."/".$file));
  294. }
  295. }
  296. @closedir($dir);
  297. return $files;
  298. }
  299. function sort_files($a,$b)
  300. {
  301. if($a['type'] != $b['type'])
  302. {
  303. return $a['type'] > $b['type']? -1 : 1;
  304. }
  305. else if($a['type'] == 5)
  306. {
  307. return strcmp(strtolower($a['name']),strtolower($b['name']));
  308. }
  309. else
  310. {
  311. if($a['ext'] != $b['ext'])
  312. {
  313. return strcmp($a['ext'],$b['ext']);
  314. }
  315. else if($a['stat'][7] != $b['stat'][7])
  316. {
  317. return $a['stat'][7] > $b['stat'][7]? -1 : 1;
  318. }
  319. else
  320. {
  321. return strcmp(strtolower($a['name']),strtolower($b['name']));
  322. }
  323. }
  324. return 0;
  325. }
  326. function download_file()
  327. {
  328. if($this->options['inmemory'] == 0)
  329. {
  330. $this->error[] = "Can only use download_file() if archive is in memory. Redirect to file otherwise, it is faster.";
  331. return;
  332. }
  333. switch($this->options['type'])
  334. {
  335. case "zip":
  336. header("Content-type:application/zip");
  337. break;
  338. case "bzip":
  339. header("Content-type:application/x-compressed");
  340. break;
  341. case "gzip":
  342. header("Content-type:application/x-compressed");
  343. break;
  344. case "tar":
  345. header("Content-type:application/x-tar");
  346. }
  347. $header = "Content-disposition: attachment; filename=\"";
  348. $header .= strstr($this->options['name'],"/")? substr($this->options['name'],strrpos($this->options['name'],"/") + 1) : $this->options['name'];
  349. $header .= "\"";
  350. header($header);
  351. header("Content-length: " . strlen($this->archive));
  352. header("Content-transfer-encoding: binary");
  353. header("Pragma: no-cache");
  354. header("Expires: 0");
  355. print($this->archive);
  356. }
  357. }
  358. class tar_file extends archive
  359. {
  360. function tar_file($name)
  361. {
  362. $this->archive($name);
  363. $this->options['type'] = "tar";
  364. }
  365. function create_tar()
  366. {
  367. $pwd = getcwd();
  368. chdir($this->options['basedir']);
  369. foreach($this->files as $current)
  370. {
  371. if($current['name'] == $this->options['name'])
  372. {
  373. continue;
  374. }
  375. if(strlen($current['name2']) > 99)
  376. {
  377. $path = substr($current['name2'],0,strpos($current['name2'],"/",strlen($current['name2']) - 100) + 1);
  378. $current['name2'] = substr($current['name2'],strlen($path));
  379. if(strlen($path) > 154 || strlen($current['name2']) > 99)
  380. {
  381. $this->error[] = "Could not add {$path}{$current['name2']} to archive because the filename is too long.";
  382. continue;
  383. }
  384. }
  385. $block = pack("a100a8a8a8a12a12a8a1a100a6a2a32a32a8a8a155a12",$current['name2'],decoct($current['stat'][2]),
  386. sprintf("%6s ",decoct($current['stat'][4])),sprintf("%6s ",decoct($current['stat'][5])),
  387. sprintf("%11s ",decoct($current['stat'][7])),sprintf("%11s ",decoct($current['stat'][9])),
  388. " ",$current['type'],"","ustar","00","Unknown","Unknown","","",!empty($path)? $path : "","");
  389. $checksum = 0;
  390. for($i = 0; $i < 512; $i++)
  391. {
  392. $checksum += ord(substr($block,$i,1));
  393. }
  394. $checksum = pack("a8",sprintf("%6s ",decoct($checksum)));
  395. $block = substr_replace($block,$checksum,148,8);
  396. if($current['stat'][7] == 0)
  397. {
  398. $this->add_data($block);
  399. }
  400. else if($fp = @fopen($current['name'],"rb"))
  401. {
  402. $this->add_data($block);
  403. while($temp = fread($fp,1048576))
  404. {
  405. $this->add_data($temp);
  406. }
  407. if($current['stat'][7] % 512 > 0)
  408. {
  409. $temp = "";
  410. for($i = 0; $i < 512 - $current['stat'][7] % 512; $i++)
  411. {
  412. $temp .= "\0";
  413. }
  414. $this->add_data($temp);
  415. }
  416. fclose($fp);
  417. }
  418. else
  419. {
  420. $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
  421. }
  422. }
  423. $this->add_data(pack("a512",""));
  424. chdir($pwd);
  425. return 1;
  426. }
  427. function extract_files()
  428. {
  429. $pwd = getcwd();
  430. chdir($this->options['basedir']);
  431. if($fp = $this->open_archive())
  432. {
  433. if($this->options['inmemory'] == 1)
  434. {
  435. $this->files = array();
  436. }
  437. while($block = fread($fp,512))
  438. {
  439. $temp = unpack("a100name/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1type/a100temp/a6magic/a2temp/a32temp/a32temp/a8temp/a8temp/a155prefix/a12temp",$block);
  440. $file = array(
  441. 'name'=>$temp['prefix'] . $temp['name'],
  442. 'stat'=>array(
  443. 2=>$temp['mode'],
  444. 4=>octdec($temp['uid']),
  445. 5=>octdec($temp['gid']),
  446. 7=>octdec($temp['size']),
  447. 9=>octdec($temp['mtime']),
  448. ),
  449. 'checksum'=>octdec($temp['checksum']),
  450. 'type'=>$temp['type'],
  451. 'magic'=>$temp['magic'],
  452. );
  453. if($file['checksum'] == 0x00000000)
  454. {
  455. break;
  456. }
  457. else if($file['magic'] != "ustar")
  458. {
  459. $this->error[] = "This script does not support extracting this type of tar file.";
  460. break;
  461. }
  462. $block = substr_replace($block," ",148,8);
  463. $checksum = 0;
  464. for($i = 0; $i < 512; $i++)
  465. {
  466. $checksum += ord(substr($block,$i,1));
  467. }
  468. if($file['checksum'] != $checksum)
  469. {
  470. $this->error[] = "Could not extract from {$this->options['name']}, it is corrupt.";
  471. }
  472. if($this->options['inmemory'] == 1)
  473. {
  474. $file['data'] = fread($fp,$file['stat'][7]);
  475. fread($fp,(512 - $file['stat'][7] % 512) == 512? 0 : (512 - $file['stat'][7] % 512));
  476. unset($file['checksum'],$file['magic']);
  477. $this->files[] = $file;
  478. }
  479. else
  480. {
  481. if($file['type'] == 5)
  482. {
  483. if(!is_dir($file['name']))
  484. {
  485. mkdir($file['name'],$file['stat'][2]);
  486. chown($file['name'],$file['stat'][4]);
  487. chgrp($file['name'],$file['stat'][5]);
  488. }
  489. }
  490. else if($this->options['overwrite'] == 0 && file_exists($file['name']))
  491. {
  492. $this->error[] = "{$file['name']} already exists.";
  493. }
  494. else if($new = @fopen($file['name'],"wb"))
  495. {
  496. fwrite($new,fread($fp,$file['stat'][7]));
  497. fread($fp,(512 - $file['stat'][7] % 512) == 512? 0 : (512 - $file['stat'][7] % 512));
  498. fclose($new);
  499. chmod($file['name'],$file['stat'][2]);
  500. chown($file['name'],$file['stat'][4]);
  501. chgrp($file['name'],$file['stat'][5]);
  502. }
  503. else
  504. {
  505. $this->error[] = "Could not open {$file['name']} for writing.";
  506. }
  507. }
  508. unset($file);
  509. }
  510. }
  511. else
  512. {
  513. $this->error[] = "Could not open file {$this->options['name']}";
  514. }
  515. chdir($pwd);
  516. }
  517. function open_archive()
  518. {
  519. return @fopen($this->options['name'],"rb");
  520. }
  521. }
  522. class gzip_file extends tar_file
  523. {
  524. function gzip_file($name)
  525. {
  526. $this->tar_file($name);
  527. $this->options['type'] = "gzip";
  528. }
  529. function create_gzip()
  530. {
  531. if($this->options['inmemory'] == 0)
  532. {
  533. $pwd = getcwd();
  534. chdir($this->options['basedir']);
  535. if($fp = gzopen($this->options['name'],"wb{$this->options['level']}"))
  536. {
  537. fseek($this->archive,0);
  538. while($temp = fread($this->archive,1048576))
  539. {
  540. gzwrite($fp,$temp);
  541. }
  542. gzclose($fp);
  543. chdir($pwd);
  544. }
  545. else
  546. {
  547. $this->error[] = "Could not open {$this->options['name']} for writing.";
  548. chdir($pwd);
  549. return 0;
  550. }
  551. }
  552. else
  553. {
  554. $this->archive = gzencode($this->archive,$this->options['level']);
  555. }
  556. return 1;
  557. }
  558. function open_archive()
  559. {
  560. return @gzopen($this->options['name'],"rb");
  561. }
  562. }
  563. class bzip_file extends tar_file
  564. {
  565. function bzip_file($name)
  566. {
  567. $this->tar_file($name);
  568. $this->options['type'] = "bzip";
  569. }
  570. function create_bzip()
  571. {
  572. if($this->options['inmemory'] == 0)
  573. {
  574. $pwd = getcwd();
  575. chdir($this->options['basedir']);
  576. if($fp = bzopen($this->options['name'],"wb"))
  577. {
  578. fseek($this->archive,0);
  579. while($temp = fread($this->archive,1048576))
  580. {
  581. bzwrite($fp,$temp);
  582. }
  583. bzclose($fp);
  584. chdir($pwd);
  585. }
  586. else
  587. {
  588. $this->error[] = "Could not open {$this->options['name']} for writing.";
  589. chdir($pwd);
  590. return 0;
  591. }
  592. }
  593. else
  594. {
  595. $this->archive = bzcompress($this->archive,$this->options['level']);
  596. }
  597. return 1;
  598. }
  599. function open_archive()
  600. {
  601. return @bzopen($this->options['name'],"rb");
  602. }
  603. }
  604. class zip_file extends archive
  605. {
  606. function zip_file($name)
  607. {
  608. $this->archive($name);
  609. $this->options['type'] = "zip";
  610. }
  611. function create_zip()
  612. {
  613. $files = 0;
  614. $offset = 0;
  615. $central = "";
  616. if(!empty($this->options['sfx']))
  617. {
  618. if($fp = @fopen($this->options['sfx'],"rb"))
  619. {
  620. $temp = fread($fp,filesize($this->options['sfx']));
  621. fclose($fp);
  622. $this->add_data($temp);
  623. $offset += strlen($temp);
  624. unset($temp);
  625. }
  626. else
  627. {
  628. $this->error[] = "Could not open sfx module from {$this->options['sfx']}.";
  629. }
  630. }
  631. $pwd = getcwd();
  632. chdir($this->options['basedir']);
  633. foreach($this->files as $current)
  634. {
  635. if($current['name'] == $this->options['name'])
  636. {
  637. continue;
  638. }
  639. $translate = array('Ç'=>pack("C",128),'ü'=>pack("C",129),'é'=>pack("C",130),'â'=>pack("C",131),'ä'=>pack("C",132),
  640. 'ŕ'=>pack("C",133),'ĺ'=>pack("C",134),'ç'=>pack("C",135),'ę'=>pack("C",136),'ë'=>pack("C",137),
  641. 'č'=>pack("C",138),'ď'=>pack("C",139),'î'=>pack("C",140),'ě'=>pack("C",141),'Ä'=>pack("C",142),
  642. 'Ĺ'=>pack("C",143),'É'=>pack("C",144),'ć'=>pack("C",145),'Ć'=>pack("C",146),'ô'=>pack("C",147),
  643. 'ö'=>pack("C",148),'ň'=>pack("C",149),'ű'=>pack("C",150),'ů'=>pack("C",151),'Ö'=>pack("C",153),
  644. 'Ü'=>pack("C",154),'Ł'=>pack("C",156),'Ľ'=>pack("C",157),'ƒ'=>pack("C",159),'á'=>pack("C",160),
  645. 'í'=>pack("C",161),'ó'=>pack("C",162),'ú'=>pack("C",163),'ń'=>pack("C",164),'Ń'=>pack("C",165));
  646. $current['name2'] = strtr($current['name2'],$translate);
  647. $timedate = explode(" ",date("Y n j G i s",$current['stat'][9]));
  648. $timedate = ($timedate[0] - 1980 << 25) | ($timedate[1] << 21) | ($timedate[2] << 16) |
  649. ($timedate[3] << 11) | ($timedate[4] << 5) | ($timedate[5]);
  650. $block = pack("VvvvV",0x04034b50,0x000A,0x0000,(isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate);
  651. if($current['stat'][7] == 0 && $current['type'] == 5)
  652. {
  653. $block .= pack("VVVvv",0x00000000,0x00000000,0x00000000,strlen($current['name2']) + 1,0x0000);
  654. $block .= $current['name2'] . "/";
  655. $this->add_data($block);
  656. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  657. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  658. 0x00000000,0x00000000,0x00000000,strlen($current['name2']) + 1,0x0000,0x0000,0x0000,0x0000,$current['type'] == 5? 0x00000010 : 0x00000000,$offset);
  659. $central .= $current['name2'] . "/";
  660. $files++;
  661. $offset += (31 + strlen($current['name2']));
  662. }
  663. else if($current['stat'][7] == 0)
  664. {
  665. $block .= pack("VVVvv",0x00000000,0x00000000,0x00000000,strlen($current['name2']),0x0000);
  666. $block .= $current['name2'];
  667. $this->add_data($block);
  668. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  669. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  670. 0x00000000,0x00000000,0x00000000,strlen($current['name2']),0x0000,0x0000,0x0000,0x0000,$current['type'] == 5? 0x00000010 : 0x00000000,$offset);
  671. $central .= $current['name2'];
  672. $files++;
  673. $offset += (30 + strlen($current['name2']));
  674. }
  675. else if($fp = @fopen($current['name'],"rb"))
  676. {
  677. $temp = fread($fp,$current['stat'][7]);
  678. fclose($fp);
  679. $crc32 = crc32($temp);
  680. if(!isset($current['method']) && $this->options['method'] == 1)
  681. {
  682. $temp = gzcompress($temp,$this->options['level']);
  683. $size = strlen($temp) - 6;
  684. $temp = substr($temp,2,$size);
  685. }
  686. else
  687. {
  688. $size = strlen($temp);
  689. }
  690. $block .= pack("VVVvv",$crc32,$size,$current['stat'][7],strlen($current['name2']),0x0000);
  691. $block .= $current['name2'];
  692. $this->add_data($block);
  693. $this->add_data($temp);
  694. unset($temp);
  695. $central .= pack("VvvvvVVVVvvvvvVV",0x02014b50,0x0014,$this->options['method'] == 0? 0x0000 : 0x000A,0x0000,
  696. (isset($current['method']) || $this->options['method'] == 0)? 0x0000 : 0x0008,$timedate,
  697. $crc32,$size,$current['stat'][7],strlen($current['name2']),0x0000,0x0000,0x0000,0x0000,0x00000000,$offset);
  698. $central .= $current['name2'];
  699. $files++;
  700. $offset += (30 + strlen($current['name2']) + $size);
  701. }
  702. else
  703. {
  704. $this->error[] = "Could not open file {$current['name']} for reading. It was not added.";
  705. }
  706. }
  707. $this->add_data($central);
  708. $this->add_data(pack("VvvvvVVv",0x06054b50,0x0000,0x0000,$files,$files,strlen($central),$offset,
  709. !empty($this->options['comment'])? strlen($this->options['comment']) : 0x0000));
  710. if(!empty($this->options['comment']))
  711. {
  712. $this->add_data($this->options['comment']);
  713. }
  714. chdir($pwd);
  715. return 1;
  716. }
  717. } ?>