PHP 压缩文件类

  1. class createZip { 
  2. public $compressedData = array(); 
  3. public $centralDirectory = array(); // central directory   
  4. public $endOfCentralDirectory = "/x50/x4b/x05/x06/x00/x00/x00/x00"//end of Central directory record
  5. public $oldOffset = 0;
  6. public function addDirectory($directoryName) {
  7. $directoryName = str_replace("//", "/", $directoryName); 
  8. $feedArrayRow = "/x50/x4b/x03/x04";
  9. $feedArrayRow .= "/x0a/x00";    
  10. $feedArrayRow .= "/x00/x00";    
  11. $feedArrayRow .= "/x00/x00";    
  12. $feedArrayRow .= "/x00/x00/x00/x00";
  13. $feedArrayRow .= pack("V",0); 
  14. $feedArrayRow .= pack("V",0); 
  15. $feedArrayRow .= pack("V",0); 
  16. $feedArrayRow .= pack("v", strlen($directoryName) ); 
  17. $feedArrayRow .= pack("v", 0 ); 
  18. $feedArrayRow .= $directoryName; 
  19. $feedArrayRow .= pack("V",0); 
  20. $feedArrayRow .= pack("V",0); 
  21. $feedArrayRow .= pack("V",0);
  22. $this -> compressedData[] = $feedArrayRow;
  23. $newOffset = strlen(implode("", $this->compressedData));
  24. $addCentralRecord = "/x50/x4b/x01/x02";
  25. $addCentralRecord .="/x00/x00";    
  26. $addCentralRecord .="/x0a/x00";    
  27. $addCentralRecord .="/x00/x00";    
  28. $addCentralRecord .="/x00/x00";    
  29. $addCentralRecord .="/x00/x00/x00/x00"
  30. $addCentralRecord .= pack("V",0); 
  31. $addCentralRecord .= pack("V",0); 
  32. $addCentralRecord .= pack("V",0); 
  33. $addCentralRecord .= pack("v", strlen($directoryName) ); 
  34. $addCentralRecord .= pack("v", 0 ); 
  35. $addCentralRecord .= pack("v", 0 ); 
  36. $addCentralRecord .= pack("v", 0 ); 
  37. $addCentralRecord .= pack("v", 0 ); 
  38. $ext = "/x00/x00/x10/x00";
  39. $ext = "/xff/xff/xff/xff"
  40. $addCentralRecord .= pack("V", 16 );
  41. $addCentralRecord .= pack("V", $this -> oldOffset ); 
  42. $this -> oldOffset = $newOffset;
  43. $addCentralRecord .= $directoryName; 
  44. $this -> centralDirectory[] = $addCentralRecord; 
  45. public function addFile($data, $directoryName)   {
  46. $directoryName = str_replace("//", "/", $directoryName); 
  47. $feedArrayRow = "/x50/x4b/x03/x04";
  48. $feedArrayRow .= "/x14/x00";    
  49. $feedArrayRow .= "/x00/x00";    
  50. $feedArrayRow .= "/x08/x00";    
  51. $feedArrayRow .= "/x00/x00/x00/x00";
  52. $uncompressedLength = strlen($data); 
  53. $compression = crc32($data); 
  54. $gzCompressedData = gzcompress($data); 
  55. $gzCompressedData = substr( substr($gzCompressedData, 0, strlen($gzCompressedData) - 4), 2); 
  56. $compressedLength = strlen($gzCompressedData); 
  57. $feedArrayRow .= pack("V",$compression); 
  58. $feedArrayRow .= pack("V",$compressedLength); 
  59. $feedArrayRow .= pack("V",$uncompressedLength); 
  60. $feedArrayRow .= pack("v", strlen($directoryName) ); 
  61. $feedArrayRow .= pack("v", 0 ); 
  62. $feedArrayRow .= $directoryName; 
  63. $feedArrayRow .= $gzCompressedData; 
  64. $feedArrayRow .= pack("V",$compression); 
  65. $feedArrayRow .= pack("V",$compressedLength); 
  66. $feedArrayRow .= pack("V",$uncompressedLength);
  67. $this -> compressedData[] = $feedArrayRow;
  68. $newOffset = strlen(implode("", $this->compressedData));
  69. $addCentralRecord = "/x50/x4b/x01/x02";
  70. $addCentralRecord .="/x00/x00";    
  71. $addCentralRecord .="/x14/x00";    
  72. $addCentralRecord .="/x00/x00";    
  73. $addCentralRecord .="/x08/x00";    
  74. $addCentralRecord .="/x00/x00/x00/x00"
  75. $addCentralRecord .= pack("V",$compression); 
  76. $addCentralRecord .= pack("V",$compressedLength); 
  77. $addCentralRecord .= pack("V",$uncompressedLength); 
  78. $addCentralRecord .= pack("v", strlen($directoryName) ); 
  79. $addCentralRecord .= pack("v", 0 );
  80. $addCentralRecord .= pack("v", 0 );
  81. $addCentralRecord .= pack("v", 0 );
  82. $addCentralRecord .= pack("v", 0 );
  83. $addCentralRecord .= pack("V", 32 );
  84. $addCentralRecord .= pack("V", $this -> oldOffset ); 
  85. $this -> oldOffset = $newOffset;
  86. $addCentralRecord .= $directoryName; 
  87. $this -> centralDirectory[] = $addCentralRecord; 
  88. }
  89. public function getZippedfile() {
  90. $data = implode("", $this -> compressedData); 
  91. $controlDirectory = implode("", $this -> centralDirectory); 
  92. return   
  93.    $data. 
  94.    $controlDirectory. 
  95.    $this -> endOfCentralDirectory. 
  96.    pack("v"sizeof($this -> centralDirectory)).     
  97.    pack("v"sizeof($this -> centralDirectory)).     
  98.    pack("V", strlen($controlDirectory)).             
  99.    pack("V", strlen($data)).                
  100.    "/x00/x00";                             
  101. }
  102. public function forceDownload($archiveName) {
  103. $headerInfo = '';
  104.    
  105. if(ini_get('zlib.output_compression')) {
  106.    ini_set('zlib.output_compression''Off');
  107. }
  108. // Security checks
  109. if( $archiveName == "" ) {
  110.    echo "
  111. ERROR: The download file was NOT SPECIFIED.";
  112.    exit;
  113. elseif ( ! file_exists( $archiveName ) ) {
  114.    echo "
  115. ERROR: File not found.";
  116.    exit;
  117. }
  118. header("Pragma: public");
  119. header("Expires: 0");
  120. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  121. header("Cache-Control: private",false);
  122. header("Content-Type: application/zip");
  123. header("Content-Disposition: attachment; filename=".basename($archiveName).";" );
  124. header("Content-Transfer-Encoding: binary");
  125. header("Content-Length: ".filesize($archiveName));
  126. readfile("$archiveName");
  127. }
  128. }
  129. function getDir($dir)   
  130. {
  131. foreach (glob($dir."*"as $value)
  132. {
  133.      if (is_dir($value))
  134.          {
  135.         $currFolder = substr($value,strlen($dir));
  136.      $currPath = $value;
  137.      echo $currFolder."<br>";;
  138.      //echo $currPath ."<br>"; 
  139.      $_SESSION['createZip']-> addDirectory($currPath); 
  140.      $newFolder = $value."/";
  141.      getDir($newFolder);
  142.    }
  143.       else
  144. {
  145.      $currFile = substr($value,strlen($dir));
  146.      $currPath = $dir;
  147.      $fileContents = file_get_contents($value);   
  148.      $_SESSION['createZip'] -> addFile($fileContents,$value);   
  149.    
  150.        echo $currFile ."<br>"
  151.    } 
  152. }
  153. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值