ホームページ  >  記事  >  バックエンド開発  >  php_PHPチュートリアルでzip圧縮ファイルを生成するサンプルコードの説明

php_PHPチュートリアルでzip圧縮ファイルを生成するサンプルコードの説明

WBOY
WBOYオリジナル
2016-07-13 17:41:32903ブラウズ

php圧縮ファイルzipの例
1. 最初に用意したzip.phpツールクラスをダウンロードし、ダウンロード後解凍し、中のファイルを仮想ディレクトリ配下のincludeフォルダに置きます。真ん中。
2. 次のコードを php ファイルに追加するだけです
使用方法:
require_once "./include/zip.php";
$zip = new PHPZip();
//$zip ->ディレクトリアドレス", "圧縮ファイル名.zip"); //生成のみで自動ダウンロードはしません
$zip -> downloadZip("圧縮するフォルダディレクトリアドレス", "圧縮ファイル名.zip"); //自動ダウンロード

例: 特定の使用シナリオを確認するには、以下の疑似コードを参照できます: Code
require_once "./include/zip.php";
if (!$download) {
exit ();
}
set_time_limit( 60);
$tmpManager = new TmpManager() //次の操作を完了するクラスがあるとします
$tempfolder = array();
$tempfile = array(); // 選択したフォルダーまたはファイルをダウンロードするとします。ページ上のチェックボックスを選択して一緒にパッケージ化します
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); //ダウンロードするファイルをコピーしてフォルダーを作成します
}
if (sizeof($tempfolder) > 0) {
$tmpManager ->CopyFolder($tempfolder,$curtempdir); //作成したフォルダーにダウンロードするフォルダーをコピーします
}
$ zip = new PHPZip();
$zip ->downloadZip($curtempdir, "file_". date(Ymd).".zip");//パッケージ化してダウンロード

Compression class:

  1. /*
  2. ファイル名: /include/zip.php
  3. 作成者: 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,名前、本当);
  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)) $ファイル名 = ベース名($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 "zipエラーを作成!";
  41. }
  42. unlink($zipfilenametemp);
  43. }
  44. }
  45. //ディレクトリツリーを取得..
  46. function dirTree($directory,$rootDir ){
  47. グローバル $_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. // ns フォルダーに進みます
  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->
  68. function unix2DosTime($unixtime = 0) {
  69. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  70. if ($timearray[年]
  71. $timearray[年] = 1980;
  72. $timearray[mon] = 1;
  73. $timearray[月曜日] = 1;
  74. $timearray[時間] = 0;
  75. $timearray[分] = 0;
  76. $timearray[秒] = 0;
  77. } // end if
  78. return (($時間配列[年] - 1980)
  79. }
  80. function addFile($data, $name, $time = 0){
  81. $name = str_replace(, /, $name);
  82. $ dtime = dechex($this->unix2DosTime($time));
  83. $hexdtime = x 。 $dtime[6] 。 $dtime[7]
  84. 。バツ 。 $dtime[4] 。 $dtime[5]
  85. 。バツ 。 $dtime[2] 。 $dtime[3]
  86. 。バツ 。 $dtime[0] 。 $dtime[1];
  87. eval($hexdtime = " . $hexdtime . ";);
  88. $fr = "x50x4bx03x04";
  89. $fr .= "x14x00"; // 抽出するにはverが必要
  90. $fr .= "x00x00"; // 生成目的ビットフラグ
  91. $fr .= "x08x00"; // 圧縮方法
  92. $fr .= $hexdtime; // 最終更新日時
  93. // 「ローカル ファイル ヘッダー」セグメント
  94. $unc_len = strlen($data);
  95. $crc = crc32($data);
  96. $zdata = gzcompress($data);
  97. $c_len = strlen($zdata);
  98. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // crc バグを修正
  99. $fr .= Pack(V, $crc); // crc32
  100. $fr .= Pack(V, $c_len); // 圧縮ファイルサイズ
  101. $fr .= Pack(V, $unc_len); // 非圧縮ファイルサイズ
  102. $fr .= Pack(v, strlen($name)); // ファイル名の長さ
  103. $fr .= Pack(v, 0); // 余分なフィールド長
  104. $fr .= $name;
  105. // "ファイルデータ" セグメント
  106. $fr .= $zdata;
  107. // "データ記述子" セグメント (オプションですが、アーカイブしない場合は必須です
  108. )
  109. // ファイルとして機能)
  110. $fr .= Pack(V, $crc); // crc32
  111. $fr .= Pack(V, $c_len); // 圧縮ファイルサイズ
  112. $fr .= Pack(V, $unc_len); // 非圧縮ファイルサイズ
  113. // このエントリを array
  114. $this に追加します
  115. -> datasec[] = $fr;
  116. $new_offset = strlen(implode(, $this->datasec));
  117. // 中央ディレクトリにレコードを追加します
  118. $cdrec = "x50x4bx01x02";
  119. $cdrec 。 = "x00x00"; //
  120. $cdrec が作成したバージョン .= "x14x00"; // 抽出に必要なバージョン
  121. $cdrec .= "x00x00"; // 生成目的ビットフラグ
  122. $cdrec .= "x08x00"; // 圧縮方法
  123. $cdrec .= $hexdtime; // 最終更新日時
  124. $cdrec .= Pack(V, $crc); // crc32
  125. $cdrec .= Pack(V, $c_len); // 圧縮ファイルサイズ
  126. $cdrec .= Pack(V, $unc_len); // 非圧縮ファイルサイズ
  127. $cdrec .= Pack(v, strlen($name) ); // ファイル名の長さ
  128. $cdrec .= Pack(v, 0 ); // 追加のフィールド長
  129. $cdrec .= Pack(v, 0 ); // ファイルコメントの長さ
  130. $cdrec .= Pack(v, 0 ); // ディスク番号 start
  131. $cdrec .= Pack(v, 0 ); // 内部ファイル属性
  132. $cdrec .= Pack(V, 32 ); // 外部ファイル属性 - アーカイブ ビット set
  133. $cdrec .= Pack(V, $this -> old_offset ); // ローカルヘッダーの相対オフセット
  134. $this -> old_offset = $new_offset;
  135. $cdrec .= $name;

    // オプションの追加フィールド、ファイルコメント

    http://www.bkjia.com/PHPjc/486133.htmlwww.bkjia.comtru​​ehttp://www.bkjia.com/PHPjc/486133.html技術記事
    php压缩文件zipの例 1.请先下ダウンロード我標準备好的zip.php工具类、下載後解压、将里面の文件放入对应的目录中、我是放在虚拟目录下的include文件...
🎜🎜
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。