Home  >  Article  >  Backend Development  >  Example code explanation for generating zip compressed files in php_PHP tutorial

Example code explanation for generating zip compressed files in php_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:41:32902browse

Example of php compressed file zip
1. Please download the zip.php tool class I prepared first, unzip it after downloading, and put the files inside into the corresponding directory. I put it 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("Directory address of the folder to be compressed", "Compressed file name.zip"); //Only generated and not automatically downloaded
$zip -> downloadZip("To be compressed Folder directory address", "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(); // Suppose we have a class to complete the following operations
$tempfolder = array();
$tempfile = array(); //Suppose we download the selected folder or file by selecting the checkbox on the page , and package together
for($i = 0;$i < $checkboxnum;$i ) {
$value = ${"select".$i};
if ($value != ) {
$this_type = substr($value, 0, 1);
$this_id = substr($value, 1);
//Handle 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 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); //If there are files or folders to be packaged, recreate the folder
}
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: 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 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 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

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/486133.htmlTechArticlephp压缩文件zip的例子 1.请先下载我准备好的zip.php工具类,下载后解压,将里面的文件放入对应的目录中,我是放在虚拟目录下的include文件...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn