PHP Compressed Files Help class

Source: Internet
Author: User
Tags bit set crc32 create zip time and date ziparchive
  1. /*
  2. File Name:/include/zip.php
  3. Author:horace 2009/04/15
  4. */
  5. Class phpzip{
  6. var $dirInfo = Array ("0", "0");
  7. var $rootDir = ';
  8. var $datasec = array ();
  9. var $ctrl _dir = Array ();
  10. var $eof _ctrl_dir = "/x50/x4b/x05/x06/x00/x00/x00/x00";
  11. var $old _offset = 0;
  12. function Downloadzip () {
  13. Createzip ($dir, $zipfilename, true);
  14. }
  15. function Createzip ($dir, $zipfilename, $autoDownload = False) {
  16. if (@function_exists (' gzcompress ')) {
  17. @set_time_limit ("0");
  18. if (Is_array ($dir)) {
  19. $FD = fopen ($dir, "R");
  20. $fileValue = Fread ($FD, FileSize ($filename));
  21. Fclose ($FD);
  22. if (Is_array ($dir)) $filename = basename ($dir);
  23. $this-AddFile ($fileValue, "$filename");
  24. }else{
  25. $this->dirtree ($dir, $dir);
  26. }
  27. $zipfilenametemp = Time (). $zipfilename;
  28. $out = $this-Filezip ();
  29. $fp = fopen ($zipfilenametemp, "w");
  30. Fwrite ($fp, $out, strlen ($out));
  31. Fclose ($FP);
  32. $filesize = FileSize ($zipfilenametemp);
  33. if ($filesize < 104857600) {
  34. if ($autoDownload) {
  35. Header ("Content-type:application/octet-stream");
  36. Header ("content-disposition:attachment; Filename= ". $zipfilename);
  37. }
  38. Echo $this-Filezip ();
  39. }else{
  40. echo "Create zip error!";
  41. }
  42. Unlink ($zipfilenametemp);
  43. }
  44. }
  45. Get dir tree.
  46. function DirTree ($directory, $rootDir) {
  47. Global $_server, $dirInfo, $rootDir;
  48. $fileDir = $rootDir;
  49. $myDir =dir ($directory);
  50. while ($file = $myDir->read ()) {
  51. if (Is_dir ("$directory/$file") and $file! = "." and $file! = "...") {
  52. $dirInfo [0]++;
  53. $rootDir = "$fileDir $file/";
  54. $this-AddFile ("," $rootDir ");
  55. Go on n ' s folders
  56. $this->dirtree ("$directory/$file", $rootDir);
  57. }else{
  58. if ($file! = "." and $file! = "...") {
  59. $dirInfo [1]++;
  60. $FD = fopen ("$directory/$file", "R");
  61. $fileValue = file_get_contents ("$directory/$file");
  62. Fclose ($FD);
  63. $this-AddFile ($fileValue, "$fileDir $file");
  64. }
  65. }
  66. }
  67. $myDir->close ();
  68. }
  69. function Unix2dostime ($unixtime = 0) {
  70. $timearray = ($unixtime = = 0)? GETDATE (): getdate ($unixtime);
  71. if ($timearray [' Year '] < 1980) {
  72. $timearray [' year '] = 1980;
  73. $timearray [' mon '] = 1;
  74. $timearray [' mday '] = 1;
  75. $timearray [' hours '] = 0;
  76. $timearray [' minutes '] = 0;
  77. $timearray [' seconds '] = 0;
  78. }//End If
  79. Return (($timearray [' Year ']-1980) << 25) | ($timearray [' mon '] << 21) | ($timearray [' Mday '] << 16) |
  80. ($timearray [' hours '] << 11) | ($timearray [' minutes '] << 5) | ($timearray [' seconds '] >> 1);
  81. }
  82. function AddFile ($data, $name, $time = 0) {
  83. $name = Str_replace ('//', '/', $name);
  84. $dtime = Dechex ($this->unix2dostime ($time));
  85. $hexdtime = '/x '. $dtime [6]. $dtime [7]
  86. . '/x '. $dtime [4]. $dtime [5]
  87. . '/x '. $dtime [2]. $dtime [3]
  88. . '/x '. $dtime [0]. $dtime [1];
  89. Eval (' $hexdtime = '. $hexdtime. '";');
  90. $FR = "/x50/x4b/x03/x04";
  91. $fr. = "/x14/x00"; Ver needed to extract
  92. $fr. = "/x00/x00"; Gen Purpose bit Flag
  93. $fr. = "/x08/x00"; Compression method
  94. $fr. = $hexdtime; Last mod time and date
  95. "Local File Header" segment
  96. $unc _len = strlen ($data);
  97. $CRC = CRC32 ($data);
  98. $zdata = gzcompress ($data);
  99. $c _len = strlen ($zdata);
  100. $zdata = substr (substr ($zdata, 0, strlen ($zdata)-4), 2); Fix CRC Bug
  101. $fr. = Pack (' V ', $CRC); Crc32
  102. $fr. = Pack (' V ', $c _len); Compressed FileSize
  103. $fr. = Pack (' V ', $unc _len); Uncompressed FileSize
  104. $fr. = Pack (' V ', strlen ($name)); Length of filename
  105. $fr. = Pack (' V ', 0); Extra field length
  106. $fr. = $name;
  107. "File Data" segment
  108. $fr. = $zdata;
  109. "Data Descriptor" segment (optional but necessary if archive are not
  110. served as file)
  111. $fr. = Pack (' V ', $CRC); Crc32
  112. $fr. = Pack (' V ', $c _len); Compressed FileSize
  113. $fr. = Pack (' V ', $unc _len); Uncompressed FileSize
  114. Add this entry to array
  115. $this-datasec[] = $FR;
  116. $new _offset = strlen (Implode (", $this->datasec));
  117. Now add to Central directory record
  118. $cdrec = "/x50/x4b/x01/x02";
  119. $cdrec. = "/x00/x00"; Version made by
  120. $cdrec. = "/x14/x00"; Version needed to extract
  121. $cdrec. = "/x00/x00"; Gen Purpose bit Flag
  122. $cdrec. = "/x08/x00"; Compression method
  123. $cdrec. = $hexdtime; Last MoD time & date
  124. $cdrec. = Pack (' V ', $CRC); Crc32
  125. $cdrec. = Pack (' V ', $c _len); Compressed FileSize
  126. $cdrec. = Pack (' V ', $unc _len); Uncompressed FileSize
  127. $cdrec. = Pack (' V ', strlen ($name)); Length of filename
  128. $cdrec. = Pack (' V ', 0); Extra field length
  129. $cdrec. = Pack (' V ', 0); File Comment length
  130. $cdrec. = Pack (' V ', 0); Disk number Start
  131. $cdrec. = Pack (' V ', 0); Internal file attributes
  132. $cdrec. = Pack (' V ', 32); External file attributes-' archive ' bit set
  133. $cdrec. = Pack (' V ', $this-old_offset); Relative offset of local header
  134. $this-old_offset = $new _offset;
  135. $cdrec. = $name;
  136. Optional extra field, file comment goes here
  137. Save to Central directory
  138. $this-ctrl_dir[] = $cdrec;
  139. }
  140. function Filezip () {
  141. $data = Implode (", $this-datasec);
  142. $ctrldir = Implode (", $this-Ctrl_dir);
  143. Return
  144. $data.
  145. $ctrldir.
  146. Eof_ctrl_dir, $this.
  147. Pack (' V ', sizeof ($this-Ctrl_dir)). Total # of Entries "on this disk"
  148. Pack (' V ', sizeof ($this-Ctrl_dir)). Total # of entries overall
  149. Pack (' V ', strlen ($ctrldir)). Size of Central Dir
  150. Pack (' V ', strlen ($data)). Offset to start of the central Dir
  151. "/x00/x00"; . zip file Comment length
  152. }
  153. }
  154. ?>
  155. There's another one that's relatively simple.
  156. [PHP] View plaincopy
  157. Class Createzip
  158. {
  159. /* @creates A compressed zip file to compress multiple files into a single zip function
  160. * @ $files Array type instance, array ("1.jpg", "2.jpg");
  161. * @destination the path to the target file, such as "C:/androidyue.zip"
  162. * @ $overwrite whether to overwrite the same file as the target file
  163. * @Recorded by Androidyue
  164. * @Blog: http://thinkblog.sinaapp.com
  165. */
  166. function Create_zip ($files = Array (), $destination = "", $overwrite = False)
  167. {
  168. Returns False if the zip file already exists and is set to No Override
  169. if (file_exists ($destination) &&! $overwrite) {return false;}
  170. $valid _files = Array ();
  171. If files were passed in ...
  172. Get to a real valid file name
  173. if (Is_array ($files)) {
  174. Cycle through each file
  175. foreach ($files as $file) {
  176. Make sure the file exists
  177. if (file_exists ($file)) {
  178. $valid _files[] = $file;
  179. }
  180. }
  181. }
  182. If there is a real valid file
  183. if (count ($valid _files))
  184. {
  185. Create the Archive
  186. $zip = new Ziparchive ();
  187. Open file Overwrite if file already exists, if not, create
  188. if ($zip->open ($destination, $overwrite? Ziparchive::overwrite:ziparchive::create)!== true) {
  189. return false;
  190. }
  191. To add a file to a compressed file
  192. foreach ($valid _files as $file) {
  193. $zip->addfile ($file, $file);
  194. }
  195. Close File
  196. $zip->close ();
  197. Detects if a file exists
  198. return file_exists ($destination);
  199. }
  200. else{
  201. False if no real valid file is returned
  202. return false;
  203. }
  204. }
  205. }
  206. /****
  207. Test function
  208. $files =array (' temp.php ', ' test.php ');
  209. Create_zip ($files, ' Myzipfile.zip ', true);
  210. ****/
  211. ?>
Copy Code
Compressed files, PHP
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.