/PHPExcel/Shared/ZipStreamWrapper.php

https://bitbucket.org/nfredricks/wp-employee-time · PHP · 183 lines · 69 code · 19 blank · 95 comment · 10 complexity · 40408ae9b99a4e97359763c453ecb66b MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.8, 2012-10-12
  26. */
  27. /**
  28. * PHPExcel_Shared_ZipStreamWrapper
  29. *
  30. * @category PHPExcel
  31. * @package PHPExcel_Shared
  32. * @copyright Copyright (c) 2006 - 2012 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 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 stream_stat() {
  97. return $this->_archive->statName( $this->_fileNameInArchive );
  98. }
  99. /**
  100. * Implements support for fread(), fgets() etc.
  101. *
  102. * @param int $count maximum number of bytes to read
  103. * @return string
  104. */
  105. function stream_read($count) {
  106. $ret = substr($this->_data, $this->_position, $count);
  107. $this->_position += strlen($ret);
  108. return $ret;
  109. }
  110. /**
  111. * Returns the position of the file pointer, i.e. its offset into the file
  112. * stream. Implements support for ftell().
  113. *
  114. * @return int
  115. */
  116. public function stream_tell() {
  117. return $this->_position;
  118. }
  119. /**
  120. * EOF stream
  121. *
  122. * @return bool
  123. */
  124. public function stream_eof() {
  125. return $this->_position >= strlen($this->_data);
  126. }
  127. /**
  128. * Seek stream
  129. *
  130. * @param int $offset byte offset
  131. * @param int $whence SEEK_SET, SEEK_CUR or SEEK_END
  132. * @return bool
  133. */
  134. public function stream_seek($offset, $whence) {
  135. switch ($whence) {
  136. case SEEK_SET:
  137. if ($offset < strlen($this->_data) && $offset >= 0) {
  138. $this->_position = $offset;
  139. return true;
  140. } else {
  141. return false;
  142. }
  143. break;
  144. case SEEK_CUR:
  145. if ($offset >= 0) {
  146. $this->_position += $offset;
  147. return true;
  148. } else {
  149. return false;
  150. }
  151. break;
  152. case SEEK_END:
  153. if (strlen($this->_data) + $offset >= 0) {
  154. $this->_position = strlen($this->_data) + $offset;
  155. return true;
  156. } else {
  157. return false;
  158. }
  159. break;
  160. default:
  161. return false;
  162. }
  163. }
  164. }