Php code for generating zip compressed files

Source: Internet
Author: User
Tags bit set crc32 create zip

Example of zip compression for php files
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. <? Php
  2. /*
  3. File name:/include/zip. php
  4. Author: hsf-e 2009/04/15
  5. */
  6. Class PHPZip {
  7. Var $ dirInfo = array ("0", "0 ");
  8. Var $ rootDir =;
  9. Var $ datasec = array ();
  10. Var $ ctrl_dir = array ();
  11. Var $ eof_ctrl_dir = "x50x4bx05x06x00x00x00x00 ";
  12. Var $ old_offset = 0;
  13. Function downloadZip (){
  14. CreateZip ($ dir, $ zipfilename, true );
  15. }
  16. Function createZip ($ dir, $ zipfilename, $ autoDownload = false ){
  17. If (@ function_exists (gzcompress )){
  18. @ Set_time_limit ("0 ");
  19. If (is_array ($ dir )){
  20. $ Fd = fopen ($ dir, "r ");
  21. $ FileValue = fread ($ fd, filesize ($ filename ));
  22. Fclose ($ fd );
  23. If (is_array ($ dir) $ filename = basename ($ dir );
  24. $ This-> addFile ($ fileValue, "$ filename ");
  25. } Else {
  26. $ This-> dirTree ($ dir, $ dir );
  27. }
  28. $ Zipfilenametemp = time (). $ zipfilename;
  29. $ Out = $ this-> filezip ();
  30. $ Fp = fopen ($ zipfilenametemp, "w ");
  31. Fwrite ($ fp, $ out, strlen ($ out ));
  32. Fclose ($ fp );
  33. $ Filesize = filesize ($ zipfilenametemp );
  34. If ($ filesize <104857600 ){
  35. If ($ autoDownload ){
  36. Header ("Content-type: application/octet-stream ");
  37. Header ("Content-disposition: attachment; filename =". $ zipfilename );
  38. }
  39. Echo $ this-> filezip ();
  40. } Else {
  41. Echo "create zip error! ";
  42. }
  43. Unlink ($ zipfilenametemp );
  44. }
  45. }
  46. // Get dir tree ..
  47. Function dirTree ($ directory, $ rootDir ){
  48. Global $ _ SERVER, $ dirInfo, $ rootDir;
  49. $ FileDir = $ rootDir;
  50. $ MyDir = dir ($ directory );
  51. While ($ file = $ myDir-> read ()){
  52. If (is_dir ("$ directory/$ file") and $ file! = "." And $ file! = ".."){
  53. $ DirInfo [0];
  54. $ RootDir = "$ fileDir $ file /";
  55. $ This-> addFile (, "$ rootDir ");
  56. // Go on ns folders
  57. $ This-> dirTree ("$ directory/$ file", $ rootDir );
  58. } Else {
  59. If ($ file! = "." And $ file! = ".."){
  60. $ DirInfo [1];
  61. // $ Fd = fopen ("$ directory/$ file", "r ");
  62. $ FileValue = file_get_contents ("$ directory/$ file ");
  63. // Fclose ($ fd );
  64. $ This-> addFile ($ fileValue, "$ fileDir $ file ");
  65. }
  66. }
  67. }
  68. $ MyDir-> close ();
  69. }
  70. Function unix2DosTime ($ unixtime = 0 ){
  71. $ Timearray = ($ unixtime = 0 )? Getdate (): getdate ($ unixtime );
  72. If ($ timearray [year] <1980 ){
  73. $ Timearray [year] = 1980;
  74. $ Timearray [mon] = 1;
  75. $ Timearray [mday] = 1;
  76. $ Timearray [hours] = 0;
  77. $ Timearray [minutes] = 0;
  78. $ Timearray [seconds] = 0;
  79. } // End if
  80. Return ($ timearray [year]-1980) <25) | ($ timearray [mon] <21) | ($ timearray [mday] <16) |
  81. ($ Timearray [hours] <11) | ($ timearray [minutes] <5) | ($ timearray [seconds]> 1 );
  82. }
  83. Function addFile ($ data, $ name, $ time = 0 ){
  84. $ Name = str_replace (,/, $ name );
  85. $ Dtime = dechex ($ this-> unix2DosTime ($ time ));
  86. $ Hexdtime = x. $ dtime [6]. $ dtime [7]
  87. . X. $ dtime [4]. $ dtime [5]
  88. . X. $ dtime [2]. $ dtime [3]
  89. . X. $ dtime [0]. $ dtime [1];
  90. Eval ($ hexdtime = ". $ hexdtime .";);
  91. $ Fr = "x50x4bx03x04 ";
  92. $ Fr. = "x14x00"; // ver needed to extract
  93. $ Fr. = "x00x00"; // gen purpose bit flag
  94. $ Fr. = "x08x00"; // compression method
  95. $ Fr. = $ hexdtime; // last mod time and date
  96. // "Local file header" segment
  97. $ Unc_len = strlen ($ data );
  98. $ Crc = crc32 ($ data );
  99. $ Zdata = gzcompress ($ data );
  100. $ C_len = strlen ($ zdata );
  101. $ Zdata = substr ($ zdata, 0, strlen ($ zdata)-4), 2); // fix the crc bug
  102. $ Fr. = pack (V, $ crc); // crc32
  103. $ Fr. = pack (V, $ c_len); // compressed filesize
  104. $ Fr. = pack (V, $ unc_len); // uncompressed filesize
  105. $ Fr. = pack (v, strlen ($ name); // length of filename
  106. $ Fr. = pack (v, 0); // extra field length
  107. $ Fr. = $ name;
  108. // "File data" segment
  109. $ Fr. = $ zdata;
  110. // "Data descriptor" segment (optional but necessary if archive is not
  111. // Served as file)
  112. $ Fr. = pack (V, $ crc); // crc32
  113. $ Fr. = pack (V, $ c_len); // compressed filesize
  114. $ Fr. = pack (V, $ unc_len); // uncompressed filesize
  115. // Add this entry to array
  116. $ This-> datasec [] = $ fr;
  117. $ New_offset = strlen (implode (, $ this-> datasec ));
  118. // Now add to central directory record
  119. $ Cdrec = "x50x4bx01x02 ";
  120. $ Cdrec. = "x00x00"; // version made
  121. $ Cdrec. = "x14x00"; // version needed to extract
  122. $ Cdrec. = "x00x00"; // gen purpose bit flag
  123. $ Cdrec. = "x08x00"; // compression method
  124. $ Cdrec. = $ hexdtime; // last mod time & date
  125. $ Cdrec. = pack (V, $ crc); // crc32
  126. $ Cdrec. = pack (V, $ c_len); // compressed filesize
  127. $ Cdrec. = pack (V, $ unc_len); // uncompressed filesize
  128. $ Cdrec. = pack (v, strlen ($ name); // length of filename
  129. $ Cdrec. = pack (v, 0); // extra field length
  130. $ Cdrec. = pack (v, 0); // file comment length
  131. $ Cdrec. = pack (v, 0); // disk number start
  132. $ Cdrec. = pack (v, 0); // internal file attributes
  133. $ Cdrec. = pack (V, 32); // external file attributes-archive bit set
  134. $ Cdrec. = pack (V, $ this-> old_offset); // relative offset of local header
  135. $ This-> old_offset = $ new_offset;
  136. $ Cdrec. = $ name;
  137. // 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.