Heim  >  Artikel  >  Backend-Entwicklung  >  php生成zip压缩文件实例代码讲解_PHP教程

php生成zip压缩文件实例代码讲解_PHP教程

WBOY
WBOYOriginal
2016-07-13 17:41:32935Durchsuche

php压缩文件zip的例子
1.请先下载我准备好的zip.php工具类,下载后解压,将里面的文件放入对应的目录中,我是放在虚拟目录下的include文件夹中。
2.在你的php文件中加入下面代码即可
用法:
require_once "./include/zip.php";
$zip = new PHPZip();
//$zip -> createZip("要压缩的文件夹目录地址", "压缩后的文件名.zip");   //只生成不自动下载
$zip -> downloadZip("要压缩的文件夹目录地址", "压缩后的文件名.zip"); //自动下载

实例:可以参考下面的伪代码来看一个具体的使用场景: 代码
require_once "./include/zip.php";
if (!$download) {
exit();
}
set_time_limit(60);
$tmpManager = new TmpManager(); //假设我们有一个类来完成后面的操作
$tempfolder = array();
$tempfile = array(); //假设我们是通过在页面上选择checkbox来下载已选的文件夹或文件,并一同打包
for($i = 0;$i $value = ${"select".$i};
if ($value != ) {
$this_type = substr($value, 0, 1);
$this_id = substr($value, 1);
//将文件夹和文件的情况分开处理
if ($this_type == d) {
$tempfolder[] = $this_id;
}
elseif ($this_type == f) {

$tempfile[] = $this_id;
}
}
}
@mkdir($tempdir);
$curtempdir = "$tempdir/".$userid; //不同用户在不同的临时文件夹下操作
if (file_exists($curtempdir)) {
$tmpManager->DeleteDir($curtempdir); //删除旧的文件夹
}
if (sizeof($tempfolder) > 0 || sizeof($tempfile) > 0) {
mkdir($curtempdir, 0777); //如果有要打包的文件货文件夹,重新创建文件夹
}
if (sizeof($tempfile) > 0) {
$tmpManager->CopyFile($tempfile,$curtempdir); //将要下载的文件copy到创建的文件夹
}
if (sizeof($tempfolder) > 0) {
$tmpManager->CopyFolder($tempfolder,$curtempdir); //将要下载的文件夹copy到创建的文件夹
}
$zip = new PHPZip();
$zip -> downloadZip($curtempdir, "file_".date(Ymd).".zip");//打包并下载

压缩类:

  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
  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]
  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)
  80. ($timearray[hours] > 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文件...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn