Heim Backend-Entwicklung PHP-Tutorial 一个php把一组文件打包成zip的类

一个php把一组文件打包成zip的类

Jul 25, 2016 am 08:43 AM

这段php类可以挨个添加文件到数组,最后将添加的文件打包成zip

  1. /* $Id: zip.lib.php,v 1.1 2004/02/14 15:21:18 anoncvs_tusedb Exp $ */
  2. // vim: expandtab sw=4 ts=4 sts=4:
  3. /**
  4. * Zip file creation class.
  5. * Makes zip files.
  6. *
  7. * Last Modification and Extension By :
  8. *
  9. * Hasin Hayder
  10. * HomePage : www.hasinme.info
  11. * Email : countdraculla@gmail.com
  12. * IDE : PHP Designer 2005
  13. *
  14. *
  15. * Originally Based on :
  16. *
  17. * http://www.zend.com/codex.php?id=535&single=1
  18. * By Eric Mueller
  19. *
  20. * http://www.zend.com/codex.php?id=470&single=1
  21. * by Denis125
  22. *
  23. * a patch from Peter Listiak for last modified
  24. * date and time of the compressed file
  25. *
  26. * Official ZIP file format: http://www.pkware.com/appnote.txt
  27. *
  28. * @access public
  29. */
  30. class zipfile
  31. {
  32. /**
  33. * Array to store compressed data
  34. *
  35. * @public array $datasec
  36. */
  37. public $datasec = array();
  38. /**
  39. * Central directory
  40. *
  41. * @public array $ctrl_dir
  42. */
  43. public $ctrl_dir = array();
  44. /**
  45. * End of central directory record
  46. *
  47. * @public string $eof_ctrl_dir
  48. */
  49. public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
  50. /**
  51. * Last offset position
  52. *
  53. * @public integer $old_offset
  54. */
  55. public $old_offset = 0;
  56. /**
  57. * Converts an Unix timestamp to a four byte DOS date and time format (date
  58. * in high two bytes, time in low two bytes allowing magnitude comparison).
  59. *
  60. * @param integer the current Unix timestamp
  61. *
  62. * @return integer the current date in a four byte DOS format
  63. *
  64. * @access private
  65. */
  66. function unix2DosTime($unixtime = 0) {
  67. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  68. if ($timearray['year'] $timearray['year'] = 1980;
  69. $timearray['mon'] = 1;
  70. $timearray['mday'] = 1;
  71. $timearray['hours'] = 0;
  72. $timearray['minutes'] = 0;
  73. $timearray['seconds'] = 0;
  74. } // end if
  75. return (($timearray['year'] - 1980) ($timearray['hours'] > 1);
  76. } // end of the 'unix2DosTime()' method
  77. /**
  78. * Adds "file" to archive
  79. *
  80. * @param string file contents
  81. * @param string name of the file in the archive (may contains the path)
  82. * @param integer the current timestamp
  83. *
  84. * @access public
  85. */
  86. function addFile($data, $name, $time = 0)
  87. {
  88. $name = str_replace('\\', '/', $name);
  89. $dtime = dechex($this->unix2DosTime($time));
  90. $hexdtime = '\x' . $dtime[6] . $dtime[7]
  91. . '\x' . $dtime[4] . $dtime[5]
  92. . '\x' . $dtime[2] . $dtime[3]
  93. . '\x' . $dtime[0] . $dtime[1];
  94. eval('$hexdtime = "' . $hexdtime . '";');
  95. $fr = "\x50\x4b\x03\x04";
  96. $fr .= "\x14\x00"; // ver needed to extract
  97. $fr .= "\x00\x00"; // gen purpose bit flag
  98. $fr .= "\x08\x00"; // compression method
  99. $fr .= $hexdtime; // last mod time and date
  100. // "local file header" segment
  101. $unc_len = strlen($data);
  102. $crc = crc32($data);
  103. $zdata = gzcompress($data);
  104. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  105. $c_len = strlen($zdata);
  106. $fr .= pack('V', $crc); // crc32
  107. $fr .= pack('V', $c_len); // compressed filesize
  108. $fr .= pack('V', $unc_len); // uncompressed filesize
  109. $fr .= pack('v', strlen($name)); // length of filename
  110. $fr .= pack('v', 0); // extra field length
  111. $fr .= $name;
  112. // "file data" segment
  113. $fr .= $zdata;
  114. // "data descriptor" segment (optional but necessary if archive is not
  115. // served as file)
  116. $fr .= pack('V', $crc); // crc32
  117. $fr .= pack('V', $c_len); // compressed filesize
  118. $fr .= pack('V', $unc_len); // uncompressed filesize
  119. // add this entry to array
  120. $this -> datasec[] = $fr;
  121. // now add to central directory record
  122. $cdrec = "\x50\x4b\x01\x02";
  123. $cdrec .= "\x00\x00"; // version made by
  124. $cdrec .= "\x14\x00"; // version needed to extract
  125. $cdrec .= "\x00\x00"; // gen purpose bit flag
  126. $cdrec .= "\x08\x00"; // compression method
  127. $cdrec .= $hexdtime; // last mod time & date
  128. $cdrec .= pack('V', $crc); // crc32
  129. $cdrec .= pack('V', $c_len); // compressed filesize
  130. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  131. $cdrec .= pack('v', strlen($name) ); // length of filename
  132. $cdrec .= pack('v', 0 ); // extra field length
  133. $cdrec .= pack('v', 0 ); // file comment length
  134. $cdrec .= pack('v', 0 ); // disk number start
  135. $cdrec .= pack('v', 0 ); // internal file attributes
  136. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  137. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
  138. $this -> old_offset += strlen($fr);
  139. $cdrec .= $name;
  140. // optional extra field, file comment goes here
  141. // save to central directory
  142. $this -> ctrl_dir[] = $cdrec;
  143. } // end of the 'addFile()' method
  144. /**
  145. * Dumps out file
  146. *
  147. * @return string the zipped file
  148. *
  149. * @access public
  150. */
  151. function file()
  152. {
  153. $data = implode('', $this -> datasec);
  154. $ctrldir = implode('', $this -> ctrl_dir);
  155. return
  156. $data .
  157. $ctrldir .
  158. $this -> eof_ctrl_dir .
  159. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  160. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  161. pack('V', strlen($ctrldir)) . // size of central dir
  162. pack('V', strlen($data)) . // offset to start of central dir
  163. "\x00\x00"; // .zip file comment length
  164. } // end of the 'file()' method
  165. /**
  166. * A Wrapper of original addFile Function
  167. *
  168. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  169. *
  170. * @param array An Array of files with relative/absolute path to be added in Zip File
  171. *
  172. * @access public
  173. */
  174. function addFiles($files /*Only Pass Array*/)
  175. {
  176. foreach($files as $file)
  177. {
  178. if (is_file($file)) //directory check
  179. {
  180. $data = implode("",file($file));
  181. $this->addFile($data,$file);
  182. }
  183. }
  184. }
  185. /**
  186. * A Wrapper of original file Function
  187. *
  188. * Created By Hasin Hayder at 29th Jan, 1:29 AM
  189. *
  190. * @param string Output file name
  191. *
  192. * @access public
  193. */
  194. function output($file)
  195. {
  196. $fp=fopen($file,"w");
  197. fwrite($fp,$this->file());
  198. fclose($fp);
  199. }
  200. } // end of the 'zipfile' class
复制代码

php, zip


Erklärung dieser Website
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

Heiße KI -Werkzeuge

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Clothoff.io

Clothoff.io

KI-Kleiderentferner

AI Hentai Generator

AI Hentai Generator

Erstellen Sie kostenlos Ai Hentai.

Heißer Artikel

R.E.P.O. Energiekristalle erklärten und was sie tun (gelber Kristall)
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Beste grafische Einstellungen
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. So reparieren Sie Audio, wenn Sie niemanden hören können
3 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: Wie man alles in Myrise freischaltet
4 Wochen vor By 尊渡假赌尊渡假赌尊渡假赌

Heiße Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)

Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Curl in PHP: So verwenden Sie die PHP -Curl -Erweiterung in REST -APIs Mar 14, 2025 am 11:42 AM

Die PHP Client -URL -Erweiterung (CURL) ist ein leistungsstarkes Tool für Entwickler, das eine nahtlose Interaktion mit Remote -Servern und REST -APIs ermöglicht. Durch die Nutzung von Libcurl, einer angesehenen Bibliothek mit Multi-Protokoll-Dateien, erleichtert PHP Curl effiziente Execu

Erklären Sie das Konzept der späten statischen Bindung in PHP. Erklären Sie das Konzept der späten statischen Bindung in PHP. Mar 21, 2025 pm 01:33 PM

In Artikel wird die in PHP 5.3 eingeführte LSB -Bindung (LSB) erörtert, die die Laufzeitauflösung der statischen Methode ermöglicht, um eine flexiblere Vererbung zu erfordern. Die praktischen Anwendungen und potenziellen Perfo von LSB

Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Erklären Sie JSON Web Tokens (JWT) und ihren Anwendungsfall in PHP -APIs. Apr 05, 2025 am 12:04 AM

JWT ist ein offener Standard, der auf JSON basiert und zur sicheren Übertragung von Informationen zwischen Parteien verwendet wird, hauptsächlich für die Identitätsauthentifizierung und den Informationsaustausch. 1. JWT besteht aus drei Teilen: Header, Nutzlast und Signatur. 2. Das Arbeitsprinzip von JWT enthält drei Schritte: Generierung von JWT, Überprüfung von JWT und Parsingnayload. 3. Bei Verwendung von JWT zur Authentifizierung in PHP kann JWT generiert und überprüft werden, und die Funktionen und Berechtigungsinformationen der Benutzer können in die erweiterte Verwendung aufgenommen werden. 4. Häufige Fehler sind Signaturüberprüfungsfehler, Token -Ablauf und übergroße Nutzlast. Zu Debugging -Fähigkeiten gehört die Verwendung von Debugging -Tools und Protokollierung. 5. Leistungsoptimierung und Best Practices umfassen die Verwendung geeigneter Signaturalgorithmen, das Einstellen von Gültigkeitsperioden angemessen.

Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Rahmensicherheitsmerkmale: Schutz vor Schwachstellen. Mar 28, 2025 pm 05:11 PM

In Artikel werden wichtige Sicherheitsfunktionen in Frameworks erörtert, um vor Schwachstellen zu schützen, einschließlich Eingabevalidierung, Authentifizierung und regelmäßigen Aktualisierungen.

Anpassung/Erweiterung von Frameworks: So fügen Sie benutzerdefinierte Funktionen hinzu. Anpassung/Erweiterung von Frameworks: So fügen Sie benutzerdefinierte Funktionen hinzu. Mar 28, 2025 pm 05:12 PM

In dem Artikel werden Frameworks hinzugefügt, das sich auf das Verständnis der Architektur, das Identifizieren von Erweiterungspunkten und Best Practices für die Integration und Debuggierung hinzufügen.

Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Wie sende ich eine Postanforderung mit JSON -Daten mithilfe der Curl -Bibliothek von PHP? Apr 01, 2025 pm 03:12 PM

Senden von JSON -Daten mithilfe der Curl -Bibliothek von PHP in der PHP -Entwicklung müssen häufig mit externen APIs interagieren. Eine der gängigen Möglichkeiten besteht darin, die Curl Library zu verwenden, um Post � ...

Was genau ist das nicht blockierende Merkmal von ReactPhp? Wie gehe ich mit seinen blockierenden E/A -Operationen um? Was genau ist das nicht blockierende Merkmal von ReactPhp? Wie gehe ich mit seinen blockierenden E/A -Operationen um? Apr 01, 2025 pm 03:09 PM

Eine offizielle Einführung in das nicht blockierende Merkmal der detaillierten Interpretation der nicht blockierenden Funktion von ReactPhp hat viele Fragen vieler Entwickler gestellt: "Reactphpisnon-BlockingByDefault ...

See all articles