PHP generates ZIP archive instance code to explain _php tutorial

Source: Internet
Author: User
PHP Compressed file Zip example
1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I was placed in the virtual directory in the Include folder.
2. Add the following code to your PHP file
Usage:
Require_once "./include/zip.php";
$zip = new Phpzip ();
$zip-Createzip ("folder directory address to compress", "compressed file name. zip"); Generate non-automatic downloads only
$zip-Downloadzip ("folder directory address to compress", "compressed file name. zip"); Automatic download

Example: You can refer to the following pseudo-code to see a specific usage scenario: code
Require_once "./include/zip.php";
if (! $download) {
Exit ();
}
Set_time_limit (60);
$tmpManager = new Tmpmanager (); Let's say we have a class to do the following
$tempfolder = Array ();
$tempfile = Array (); Let's say we download the selected folder or file by selecting the 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);
Handling folders and files separately
if ($this _type = = d) {
$tempfolder [] = $this _id;
}
ElseIf ($this _type = = f) {

$tempfile [] = $this _id;
}
}
}
@mkdir ($tempdir);
$curtempdir = "$tempdir/". $userid; Different users operate under 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 the folder if you have a folder to package the files
}
if (sizeof ($tempfile) > 0) {
$tmpManager->copyfile ($tempfile, $curtempdir); Copy the file you want to download to the folder you created
}
if (sizeof ($tempfolder) > 0) {
$tmpManager->copyfolder ($tempfolder, $curtempdir); Copy the folder you want to download to the folder you created
}
$zip = new Phpzip ();
$zip-Downloadzip ($curtempdir, "File_". Date (YMD). ". Zip ");//Package and download

Compression class:
  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 = "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 (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 = "x50x4bx01x02";
  119. $cdrec. = "x00x00"; Version made by
  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

    http://www.bkjia.com/PHPjc/486133.html www.bkjia.com true http://www.bkjia.com/PHPjc/486133.html techarticle PHP Compressed File Zip example 1. Please download my prepared zip.php tool class, download and unzip, put the files inside the corresponding directory, I was placed in the virtual directory of the include file ...

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.