程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php生成zip壓縮文件實例代碼講解

php生成zip壓縮文件實例代碼講解

編輯:關於PHP編程

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 < $checkboxnum;$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. <?php
  2. /*
  3. File name: /include/zip.php
  4. Author: Horace 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(substr($zdata, 0, strlen($zdata) - 4), 2); // fix 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 by
  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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved