ZipLib.php 5.0 KB

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