php文件在线压缩程序类

提供一款超完美的php文件在线压缩程序,原理很简单就是把文件以二进制形式保存了,以前用过利用rar的内核程序,这是php自带的压缩功能,实例代码如下:

  1. set_time_limit(0);
  2. class phpzip{
  3. var $file_count = 0 ;
  4. var $datastr_len = 0;
  5. var $dirstr_len = 0;
  6. var $filedata = ''; //该变量只被类外部程序访问
  7. var $gzfilename;
  8. var $fp;
  9. var $dirstr='';
  10. /*
  11. 返回文件的修改时间格式.
  12. 只为本类内部函数调用.
  13. */
  14. function unix2dostime($unixtime = 0) {
  15. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  16. if ($timearray['year'] < 1980) {
  17. $timearray['year'] = 1980;
  18. $timearray['mon'] = 1;
  19. $timearray['mday'] = 1;
  20. $timearray['hours'] = 0;
  21. $timearray['minutes'] = 0;
  22. $timearray['seconds'] = 0;
  23. }
  24. return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
  25. ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
  26. }
  27. /*
  28. 初始化文件,建立文件目录,
  29. 并返回文件的写入权限.
  30. */
  31. function startfile($path = 'faisun.zip'){
  32. $this->gzfilename=$path;
  33. $mypathdir=array();
  34. do{
  35. $mypathdir[] = $path = dirname($path);
  36. }while($path != '.');
  37. @end($mypathdir);
  38. do{
  39. $path = @current($mypathdir);
  40. @mkdir($path);
  41. }while(@prev($mypathdir));
  42. if($this->fp=@fopen($this->gzfilename,"w")){
  43. return true;
  44. }
  45. return false;
  46. }
  47. /*
  48. 添加一个文件到 zip 压缩包中.
  49. */
  50. function addfile($data, $name){
  51. $name = str_replace('\', '/', $name);
  52. if(strrchr($name,'/')=='/') return $this->adddir($name);
  53. $dtime = dechex($this->unix2dostime());
  54. $hexdtime = 'x' . $dtime[6] . $dtime[7]
  55. . 'x' . $dtime[4] . $dtime[5]
  56. . 'x' . $dtime[2] . $dtime[3]
  57. . 'x' . $dtime[0] . $dtime[1];
  58. eval('$hexdtime = "' . $hexdtime . '";');
  59. $unc_len = strlen($data);
  60. $crc = crc32($data);
  61. $zdata = gzcompress($data);
  62. $c_len = strlen($zdata);
  63. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
  64. //新添文件内容格式化:
  65. $datastr = "PK";
  66. $datastr .= ""; // ver needed to extract
  67. $datastr .= ""; // gen purpose bit flag
  68. $datastr .= ""; // compression method
  69. $datastr .= $hexdtime; // last mod time and date
  70. $datastr .= pack('v', $crc); // crc32
  71. $datastr .= pack('v', $c_len); // compressed filesize
  72. $datastr .= pack('v', $unc_len); // uncompressed filesize
  73. $datastr .= pack('v', strlen($name)); // length of filename
  74. $datastr .= pack('v', 0); // extra field length
  75. $datastr .= $name;
  76. $datastr .= $zdata;
  77. $datastr .= pack('v', $crc); // crc32
  78. $datastr .= pack('v', $c_len); // compressed filesize
  79. $datastr .= pack('v', $unc_len); // uncompressed filesize
  80. fwrite($this->fp,$datastr); //写入新的文件内容
  81. $my_datastr_len = strlen($datastr);
  82. unset($datastr);
  83. //新添文件目录信息
  84. $dirstr = "PK";
  85. $dirstr .= ""; // version made by
  86. $dirstr .= ""; // version needed to extract
  87. $dirstr .= ""; // gen purpose bit flag
  88. $dirstr .= ""; // compression method
  89. $dirstr .= $hexdtime; // last mod time & date
  90. $dirstr .= pack('v', $crc); // crc32
  91. $dirstr .= pack('v', $c_len); // compressed filesize
  92. $dirstr .= pack('v', $unc_len); // uncompressed filesize
  93. $dirstr .= pack('v', strlen($name) ); // length of filename
  94. $dirstr .= pack('v', 0 ); // extra field length
  95. $dirstr .= pack('v', 0 ); // file comment length
  96. $dirstr .= pack('v', 0 ); // disk number start
  97. $dirstr .= pack('v', 0 ); // internal file attributes
  98. $dirstr .= pack('v', 32 ); // external file attributes - 'archive' bit set
  99. $dirstr .= pack('v',$this->datastr_len ); // relative offset of local header
  100. $dirstr .= $name;
  101. $this->dirstr .= $dirstr; //目录信息
  102. $this -> file_count ++;
  103. $this -> dirstr_len += strlen($dirstr);
  104. $this -> datastr_len += $my_datastr_len;
  105. }
  106. function adddir($name){
  107. $name = str_replace("\", "/", $name);
  108. $datastr = "PK ";
  109. $datastr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
  110. $datastr .= pack("v", 0 ).$name.pack("v", 0).pack("v", 0).pack("v", 0);
  111. fwrite($this->fp,$datastr); //写入新的文件内容
  112. $my_datastr_len = strlen($datastr);
  113. unset($datastr);
  114. $dirstr = "PK ";
  115. $dirstr .= pack("v",0).pack("v",0).pack("v",0).pack("v", strlen($name) );
  116. $dirstr .= pack("v", 0 ).pack("v", 0 ).pack("v", 0 ).pack("v", 0 );
  117. $dirstr .= pack("v", 16 ).pack("v",$this->datastr_len).$name;
  118. $this->dirstr .= $dirstr; //目录信息
  119. $this -> file_count ++;
  120. $this -> dirstr_len += strlen($dirstr);
  121. $this -> datastr_len += $my_datastr_len;
  122. }//开源代码phpfensi.com
  123. function createfile(){
  124. //压缩包结束信息,包括文件总数,目录信息读取指针位置等信息
  125. $endstr = "PK" .
  126. pack('v', $this -> file_count) .
  127. pack('v', $this -> file_count) .
  128. pack('v', $this -> dirstr_len) .
  129. pack('v', $this -> datastr_len) .
  130. "";
  131. fwrite($this->fp,$this->dirstr.$endstr);
  132. fclose($this->fp);
  133. }
  134. }