No Description

ZipStreamWrapper.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2014 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 - 2014 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. /**
  28. * PHPExcel_Shared_ZipStreamWrapper
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  33. */
  34. class PHPExcel_Shared_ZipStreamWrapper {
  35. /**
  36. * Internal ZipAcrhive
  37. *
  38. * @var ZipAcrhive
  39. */
  40. private $_archive;
  41. /**
  42. * Filename in ZipAcrhive
  43. *
  44. * @var string
  45. */
  46. private $_fileNameInArchive = '';
  47. /**
  48. * Position in file
  49. *
  50. * @var int
  51. */
  52. private $_position = 0;
  53. /**
  54. * Data
  55. *
  56. * @var mixed
  57. */
  58. private $_data = '';
  59. /**
  60. * Register wrapper
  61. */
  62. public static function register() {
  63. @stream_wrapper_unregister("zip");
  64. @stream_wrapper_register("zip", __CLASS__);
  65. }
  66. /**
  67. * Implements support for fopen().
  68. *
  69. * @param string $path resource name including scheme, e.g.
  70. * @param string $mode only "r" is supported
  71. * @param int $options mask of STREAM_REPORT_ERRORS and STREAM_USE_PATH
  72. * @param string &$openedPath absolute path of the opened stream (out parameter)
  73. * @return bool true on success
  74. */
  75. public function stream_open($path, $mode, $options, &$opened_path) {
  76. // Check for mode
  77. if ($mode{0} != 'r') {
  78. throw new PHPExcel_Reader_Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
  79. }
  80. $pos = strrpos($path, '#');
  81. $url['host'] = substr($path, 6, $pos - 6); // 6: strlen('zip://')
  82. $url['fragment'] = substr($path, $pos + 1);
  83. // Open archive
  84. $this->_archive = new ZipArchive();
  85. $this->_archive->open($url['host']);
  86. $this->_fileNameInArchive = $url['fragment'];
  87. $this->_position = 0;
  88. $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive );
  89. return true;
  90. }
  91. /**
  92. * Implements support for fstat().
  93. *
  94. * @return boolean
  95. */
  96. public function statName() {
  97. return $this->_fileNameInArchive;
  98. }
  99. /**
  100. * Implements support for fstat().
  101. *
  102. * @return boolean
  103. */
  104. public function url_stat() {
  105. return $this->statName( $this->_fileNameInArchive );
  106. }
  107. /**
  108. * Implements support for fstat().
  109. *
  110. * @return boolean
  111. */
  112. public function stream_stat() {
  113. return $this->_archive->statName( $this->_fileNameInArchive );
  114. }
  115. /**
  116. * Implements support for fread(), fgets() etc.
  117. *
  118. * @param int $count maximum number of bytes to read
  119. * @return string
  120. */
  121. function stream_read($count) {
  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. return $this->_position;
  134. }
  135. /**
  136. * EOF stream
  137. *
  138. * @return bool
  139. */
  140. public function stream_eof() {
  141. return $this->_position >= strlen($this->_data);
  142. }
  143. /**
  144. * Seek stream
  145. *
  146. * @param int $offset byte offset
  147. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  148. * @return bool
  149. */
  150. public function stream_seek($offset, $whence) {
  151. switch ($whence) {
  152. case SEEK_SET:
  153. if ($offset < strlen($this->_data) && $offset >= 0) {
  154. $this->_position = $offset;
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. break;
  160. case SEEK_CUR:
  161. if ($offset >= 0) {
  162. $this->_position += $offset;
  163. return true;
  164. } else {
  165. return false;
  166. }
  167. break;
  168. case SEEK_END:
  169. if (strlen($this->_data) + $offset >= 0) {
  170. $this->_position = strlen($this->_data) + $offset;
  171. return true;
  172. } else {
  173. return false;
  174. }
  175. break;
  176. default:
  177. return false;
  178. }
  179. }
  180. }