Php compressed file help class

Source: Internet
Author: User
Tags bit set crc32 create zip ziparchive
Php compressed file help class

  1. /*
  2. File name:/include/zip. php
  3. Author: hsf-e 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 ['urs'] = 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 ['urs'] <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 ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the 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 is 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
  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. $ This-> eof_ctrl_dir.
  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 central dir
  151. "/X00/x00"; //. zip file comment length
  152. }
  153. }
  154. ?>
  155. There is another relatively simple
  156. [Php] view plaincopy
  157. Class createzip
  158. {
  159. /* @ Creates a compressed zip file: function for compressing multiple files into one zip file
  160. * @ $ Files array type instance array ("1.jpg"," 2.jpg ");
  161. * @ Destination the path of the target file, for example, "c:/androidyue.zip"
  162. * @ $ Overwrite indicates whether to overwrite the same object as the target object.
  163. * @ Recorded By Androidyue
  164. * @ Blog: http://thinkblog.sinaapp.com
  165. */
  166. Function create_zip ($ files = array (), $ destination = '', $ overwrite = false)
  167. {
  168. // If the zip file already exists and is set to not overwrite, false is returned.
  169. If (file_exists ($ destination )&&! $ Overwrite) {return false ;}
  170. $ Valid_files = array ();
  171. // If files were passed in...
  172. // Obtain the real and 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 a real and valid file exists
  183. If (count ($ valid_files ))
  184. {
  185. // Create the archive
  186. $ Zip = new ZipArchive ();
  187. // Open the file and overwrite it if it already exists. otherwise, create
  188. If ($ zip-> open ($ destination, $ overwrite? ZIPARCHIVE: OVERWRITE: ZIPARCHIVE: CREATE )! = True ){
  189. Return false;
  190. }
  191. // Add a file to the compressed file
  192. Foreach ($ valid_files as $ file ){
  193. $ Zip-> addFile ($ file, $ file );
  194. }
  195. // Close the file
  196. $ Zip-> close ();
  197. // Check whether the file exists
  198. Return file_exists ($ destination );
  199. }
  200. Else {
  201. // If no valid file exists, false is returned.
  202. Return false;
  203. }
  204. }
  205. }
  206. /****
  207. // Test the function
  208. $ Files = array ('temp. php', 'Test. php ');
  209. Create_zip ($ files, 'myzipfile.zip ', true );
  210. ****/
  211. ?>


Compressed file, 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.