ZipLib.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * A Zip reader class - mainly for reading and extracing Zip archives.
  4. *
  5. * To write to Zip use pclzip instead
  6. * @see http://www.php.net/manual/de/class.ziparchive.php
  7. *
  8. * @author Mark Scherer
  9. * @license http://opensource.org/licenses/mit-license.php MIT
  10. */
  11. class ZipLib {
  12. protected $Zip = null;
  13. protected $filename = null;
  14. protected $path = null;
  15. protected $error = null;
  16. public function __construct($path = null, $create = false) {
  17. if (!function_exists('zip_open')) {
  18. throw new CakeException('Zip not available (enable php extension "zip")');
  19. }
  20. if ($path !== null) {
  21. $this->open($path, $create);
  22. }
  23. }
  24. public function __destruct() {
  25. $this->close();
  26. }
  27. /**
  28. * Return the filename.
  29. *
  30. * @return string
  31. */
  32. public function filename() {
  33. return $this->filename;
  34. }
  35. /**
  36. * Count all files (not folders) - works recursivly.
  37. *
  38. * @return int Size or false on failure
  39. */
  40. public function numFiles() {
  41. if (!$this->Zip) {
  42. return false;
  43. }
  44. $size = 0;
  45. while ($dirResource = zip_read($this->Zip)) {
  46. $size++;
  47. }
  48. return $size;
  49. }
  50. /**
  51. * Size in bytes
  52. *
  53. * @return int Size or false on failure
  54. */
  55. public function size() {
  56. if (!$this->Zip) {
  57. return false;
  58. }
  59. $size = 0;
  60. while ($dirResource = zip_read($this->Zip)) {
  61. $size += zip_entry_filesize($dirResource);
  62. }
  63. return $size;
  64. }
  65. /**
  66. * Open a file.
  67. *
  68. * @params string, boolean
  69. * @return bool Success
  70. */
  71. public function open($path = null, $create = false) {
  72. $this->filename = basename($path);
  73. $path = str_replace('\\', '/', $path);
  74. $this->path = $path;
  75. $zip = zip_open($path);
  76. if (is_resource($zip)) {
  77. $this->Zip = $zip;
  78. return true;
  79. }
  80. $this->error = $zip;
  81. return false;
  82. }
  83. /**
  84. * Close the Zip
  85. *
  86. * @return bool Success
  87. */
  88. public function close() {
  89. if ($this->Zip !== null) {
  90. zip_close($this->Zip);
  91. $this->Zip = null;
  92. return true;
  93. }
  94. return false;
  95. }
  96. /**
  97. * Unzip to a specific location or the current path
  98. *
  99. * @param string $location
  100. * @param bool $flatten
  101. * @return bool Success
  102. */
  103. public function unzip($location = null, $flatten = false) {
  104. if (!$this->Zip) {
  105. return false;
  106. }
  107. $file = $this->path;
  108. if (empty($location)) {
  109. $location = dirname($file) . DS;
  110. } else {
  111. if (substr($location, 0, -1) !== DS) {
  112. $location .= DS;
  113. }
  114. if (!file_exists($location)) {
  115. if (!mkdir($location, 0770, true)) {
  116. return false;
  117. }
  118. }
  119. }
  120. while ($zipEntry = zip_read($this->Zip)) {
  121. $x = str_replace('\\', '/', $location) . zip_entry_name($zipEntry);
  122. if ($flatten) {
  123. $x = str_replace('\\', '/', $location) . basename(zip_entry_name($zipEntry));
  124. }
  125. if (!file_exists($l = dirname($x))) {
  126. if (!mkdir($l, 0770, true)) {
  127. return false;
  128. }
  129. }
  130. $fp = fopen($x, "w");
  131. if (zip_entry_open($this->Zip, $zipEntry, "r")) {
  132. $buf = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));
  133. fwrite($fp, $buf);
  134. zip_entry_close($zipEntry);
  135. fclose($fp);
  136. }
  137. }
  138. return true;
  139. }
  140. /**
  141. * Returns the error string, if no error, it will return empty string ''
  142. *
  143. * @return string
  144. */
  145. public function getError($text = false) {
  146. if ($this->error === null) {
  147. return '';
  148. }
  149. if ($text) {
  150. return $this->errMsg($this->error);
  151. }
  152. return $this->error;
  153. }
  154. /**
  155. * ZipLib::errMsg()
  156. *
  157. * @param mixed $errno
  158. * @return string
  159. */
  160. public function errMsg($errno) {
  161. // using constant name as a string to make this function PHP4 compatible
  162. $zipFileFunctionsErrors = [
  163. 'ZIPARCHIVE::ER_MULTIDISK' => 'Multi-disk zip archives not supported.',
  164. 'ZIPARCHIVE::ER_RENAME' => 'Renaming temporary file failed.',
  165. 'ZIPARCHIVE::ER_CLOSE' => 'Closing zip archive failed',
  166. 'ZIPARCHIVE::ER_SEEK' => 'Seek error',
  167. 'ZIPARCHIVE::ER_READ' => 'Read error',
  168. 'ZIPARCHIVE::ER_WRITE' => 'Write error',
  169. 'ZIPARCHIVE::ER_CRC' => 'CRC error',
  170. 'ZIPARCHIVE::ER_ZIPCLOSED' => 'Containing zip archive was closed',
  171. 'ZIPARCHIVE::ER_NOENT' => 'No such file.',
  172. 'ZIPARCHIVE::ER_EXISTS' => 'File already exists',
  173. 'ZIPARCHIVE::ER_OPEN' => 'Can\'t open file',
  174. 'ZIPARCHIVE::ER_TMPOPEN' => 'Failure to create temporary file.',
  175. 'ZIPARCHIVE::ER_ZLIB' => 'Zlib error',
  176. 'ZIPARCHIVE::ER_MEMORY' => 'Memory allocation failure',
  177. 'ZIPARCHIVE::ER_CHANGED' => 'Entry has been changed',
  178. 'ZIPARCHIVE::ER_COMPNOTSUPP' => 'Compression method not supported.',
  179. 'ZIPARCHIVE::ER_EOF' => 'Premature EOF',
  180. 'ZIPARCHIVE::ER_INVAL' => 'Invalid argument',
  181. 'ZIPARCHIVE::ER_NOZIP' => 'Not a zip archive',
  182. 'ZIPARCHIVE::ER_INTERNAL' => 'Internal error',
  183. 'ZIPARCHIVE::ER_INCONS' => 'Zip archive inconsistent',
  184. 'ZIPARCHIVE::ER_REMOVE' => 'Can\'t remove file',
  185. 'ZIPARCHIVE::ER_DELETED' => 'Entry has been deleted',
  186. ];
  187. $errmsg = 'unknown';
  188. foreach ($zipFileFunctionsErrors as $constName => $errorMessage) {
  189. if (defined($constName) && constant($constName) === $errno) {
  190. return 'Zip File Function error: ' . $errorMessage;
  191. }
  192. }
  193. return 'Zip File Function error: unknown';
  194. }
  195. }