ZipLib.php 5.0 KB

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