Home Backend Development PHP Tutorial Example code explanation for generating zip compressed files in php_PHP tutorial

Example code explanation for generating zip compressed files in php_PHP tutorial

Jul 13, 2016 pm 05:41 PM
php zip download code example Prepare compression Example I document generate of explain

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles