ZipStreamWrapper.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. <?php
  2. /**
  3. * PHPExcel_Shared_ZipStreamWrapper
  4. *
  5. * Copyright (c) 2006 - 2015 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel_Shared
  23. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  26. */
  27. class PHPExcel_Shared_ZipStreamWrapper
  28. {
  29. /**
  30. * Internal ZipAcrhive
  31. *
  32. * @var ZipArchive
  33. */
  34. private $archive;
  35. /**
  36. * Filename in ZipAcrhive
  37. *
  38. * @var string
  39. */
  40. private $fileNameInArchive = '';
  41. /**
  42. * Position in file
  43. *
  44. * @var int
  45. */
  46. private $position = 0;
  47. /**
  48. * Data
  49. *
  50. * @var mixed
  51. */
  52. private $data = '';
  53. /**
  54. * Register wrapper
  55. */
  56. public static function register()
  57. {
  58. @stream_wrapper_unregister('zip');
  59. @stream_wrapper_register('zip', __CLASS__);
  60. }
  61. /**
  62. * Implements support for fopen().
  63. *
  64. * @param string $path resource name including scheme, e.g.
  65. * @param string $mode only "r" is supported
  66. * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  67. * @param string &$openedPath absolute path of the opened stream (out parameter)
  68. * @return bool true on success
  69. */
  70. public function stream_open($path, $mode, $options, &$opened_path)
  71. {
  72. // Check for mode
  73. if ($mode{0} != 'r') {
  74. throw new PHPExcel_Reader_Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
  75. }
  76. $pos = strrpos($path, '#');
  77. $url['host'] = substr($path, 6, $pos - 6); // 6: strlen('zip://')
  78. $url['fragment'] = substr($path, $pos + 1);
  79. // Open archive
  80. $this->archive = new ZipArchive();
  81. $this->archive->open($url['host']);
  82. $this->fileNameInArchive = $url['fragment'];
  83. $this->position = 0;
  84. $this->data = $this->archive->getFromName($this->fileNameInArchive);
  85. return true;
  86. }
  87. /**
  88. * Implements support for fstat().
  89. *
  90. * @return boolean
  91. */
  92. public function statName()
  93. {
  94. return $this->fileNameInArchive;
  95. }
  96. /**
  97. * Implements support for fstat().
  98. *
  99. * @return boolean
  100. */
  101. public function url_stat()
  102. {
  103. return $this->statName($this->fileNameInArchive);
  104. }
  105. /**
  106. * Implements support for fstat().
  107. *
  108. * @return boolean
  109. */
  110. public function stream_stat()
  111. {
  112. return $this->archive->statName($this->fileNameInArchive);
  113. }
  114. /**
  115. * Implements support for fread(), fgets() etc.
  116. *
  117. * @param int $count maximum number of bytes to read
  118. * @return string
  119. */
  120. public function stream_read($count)
  121. {
  122. $ret = substr($this->data, $this->position, $count);
  123. $this->position += strlen($ret);
  124. return $ret;
  125. }
  126. /**
  127. * Returns the position of the file pointer, i.e. its offset into the file
  128. * stream. Implements support for ftell().
  129. *
  130. * @return int
  131. */
  132. public function stream_tell()
  133. {
  134. return $this->position;
  135. }
  136. /**
  137. * EOF stream
  138. *
  139. * @return bool
  140. */
  141. public function stream_eof()
  142. {
  143. return $this->position >= strlen($this->data);
  144. }
  145. /**
  146. * Seek stream
  147. *
  148. * @param int $offset byte offset
  149. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  150. * @return bool
  151. */
  152. public function stream_seek($offset, $whence)
  153. {
  154. switch ($whence) {
  155. case SEEK_SET:
  156. if ($offset < strlen($this->data) && $offset >= 0) {
  157. $this->position = $offset;
  158. return true;
  159. } else {
  160. return false;
  161. }
  162. break;
  163. case SEEK_CUR:
  164. if ($offset >= 0) {
  165. $this->position += $offset;
  166. return true;
  167. } else {
  168. return false;
  169. }
  170. break;
  171. case SEEK_END:
  172. if (strlen($this->data) + $offset >= 0) {
  173. $this->position = strlen($this->data) + $offset;
  174. return true;
  175. } else {
  176. return false;
  177. }
  178. break;
  179. default:
  180. return false;
  181. }
  182. }
  183. }