Php code for generating zip compressed files

Source: Internet
Author: User
Tags bit set crc32 create zip
Example 1. download the prepared zip file first. php tool class. after downloading and decompressing the file, put the file in the corresponding directory. I put it in the include folder under the virtual directory. 2. add the following code to your php file: require_once./include/zip. php; $ zipnewPHS php compressed file zip example
1. download the zip. php tool class that I have prepared, decompress it, and put the files in the directory. I put the files in the include folder under the virtual directory.
2. add the following code to your php file:
Usage:
Require_once "./include/zip. php ";
$ Zip = new PHPZip ();
// $ Zip-> createZip ("directory address of the folder to be compressed", "file name .zip"); // only generate files that are not automatically downloaded
$ Zip-> downloadZip ("directory address of the folder to be compressed", "file name .zip"); // automatically download

Example: You can refer to the following pseudocode to see a specific use case: code
Require_once "./include/zip. php ";
If (! $ Download ){
Exit ();
}
Set_time_limit (60 );
$ TmpManager = new TmpManager (); // assume that we have a class to complete subsequent operations.
$ Tempfolder = array ();
$ Tempfile = array (); // suppose we download the selected folder or file by selecting checkbox on the page and package it together
For ($ I = 0; $ I <$ checkboxnum; $ I ){
$ Value =$ {"select". $ I };
If ($ value! = ){
$ This_type = substr ($ value, 0, 1 );
$ This_id = substr ($ value, 1 );
// Separate folders and files
If ($ this_type = d ){
$ Tempfolder [] = $ this_id;
}
Elseif ($ this_type = f ){

$ Tempfile [] = $ this_id;
}
}
}
@ Mkdir ($ tempdir );
$ Curtempdir = "$ tempdir/". $ userid; // different users operate in different temporary folders
If (file_exists ($ curtempdir )){
$ TmpManager-> DeleteDir ($ curtempdir); // delete the old folder
}
If (sizeof ($ tempfolder)> 0 | sizeof ($ tempfile)> 0 ){
Mkdir ($ curtempdir, 0777); // re-create a folder if there is a folder for packaging objects
}
If (sizeof ($ tempfile)> 0 ){
$ TmpManager-> CopyFile ($ tempfile, $ curtempdir); // copy the file to be downloaded to the created folder.
}
If (sizeof ($ tempfolder)> 0 ){
$ TmpManager-> CopyFolder ($ tempfolder, $ curtempdir); // copy the folder to be downloaded to the created folder
}
$ Zip = new PHPZip ();
$ Zip-> downloadZip ($ curtempdir, "file _". date (Ymd). ". zip"); // package and download

Compression 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 = "x50x4bx05x06x00x00x00x00 ";
  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 ns 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 = "x50x4bx03x04 ";
  91. $ Fr. = "x14x00"; // ver needed to extract
  92. $ Fr. = "x00x00"; // gen purpose bit flag
  93. $ Fr. = "x08x00"; // 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 = "x50x4bx01x02 ";
  119. $ Cdrec. = "x00x00"; // version made
  120. $ Cdrec. = "x14x00"; // version needed to extract
  121. $ Cdrec. = "x00x00"; // gen purpose bit flag
  122. $ Cdrec. = "x08x00"; // 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

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.