/protected/library/PHPExcel/Shared/ZipStreamWrapper.php

https://github.com/rjdjohnston/core · PHP · 179 lines · 79 code · 22 blank · 78 comment · 13 complexity · 120cdd8bbba79306066e306574d734f7 MD5 · raw file

  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2008 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 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.6.3, 2008-08-25
  26. */
  27. /** Register new zip wrapper */
  28. PHPExcel_Shared_ZipStreamWrapper::register();
  29. /**
  30. * PHPExcel_Shared_ZipStreamWrapper
  31. *
  32. * @category PHPExcel
  33. * @package PHPExcel_Shared
  34. * @copyright Copyright (c) 2006 - 2008 PHPExcel (http://www.codeplex.com/PHPExcel)
  35. */
  36. class PHPExcel_Shared_ZipStreamWrapper {
  37. /**
  38. * Internal ZipAcrhive
  39. *
  40. * @var ZipAcrhive
  41. */
  42. private $_archive;
  43. /**
  44. * Filename in ZipAcrhive
  45. *
  46. * @var string
  47. */
  48. private $_fileNameInArchive = '';
  49. /**
  50. * Position in file
  51. *
  52. * @var int
  53. */
  54. private $_position = 0;
  55. /**
  56. * Data
  57. *
  58. * @var mixed
  59. */
  60. private $_data = '';
  61. /**
  62. * Register wrapper
  63. */
  64. public static function register() {
  65. @stream_wrapper_unregister("zip");
  66. @stream_wrapper_register("zip", __CLASS__);
  67. }
  68. /**
  69. * Open stream
  70. */
  71. public function stream_open($path, $mode, $options, &$opened_path) {
  72. // Check for mode
  73. if (substr($mode, 0, 1) != 'r') {
  74. throw new Exception('Mode ' . $mode . ' is not supported. Only read mode is supported.');
  75. }
  76. // Parse URL
  77. $url = @parse_url($path);
  78. // Fix URL
  79. if (!is_array($url)) {
  80. $url['host'] = substr($path, strlen('zip://'));
  81. $url['path'] = '';
  82. }
  83. if (strpos($url['host'], '#') !== false) {
  84. if (!isset($url['fragment'])) {
  85. $url['fragment'] = substr($url['host'], strpos($url['host'], '#') + 1) . $url['path'];
  86. $url['host'] = substr($url['host'], 0, strpos($url['host'], '#'));
  87. unset($url['path']);
  88. }
  89. }
  90. // Open archive
  91. $this->_archive = new ZipArchive();
  92. $this->_archive->open($url['host']);
  93. $this->_fileNameInArchive = $url['fragment'];
  94. $this->_position = 0;
  95. $this->_data = $this->_archive->getFromName( $this->_fileNameInArchive );
  96. return true;
  97. }
  98. /**
  99. * Stat stream
  100. */
  101. public function stream_stat() {
  102. return $this->_archive->statName( $this->_fileNameInArchive );
  103. }
  104. /**
  105. * Read stream
  106. */
  107. function stream_read($count) {
  108. $ret = substr($this->_data, $this->_position, $count);
  109. $this->_position += strlen($ret);
  110. return $ret;
  111. }
  112. /**
  113. * Tell stream
  114. */
  115. public function stream_tell() {
  116. return $this->_position;
  117. }
  118. /**
  119. * EOF stream
  120. */
  121. public function stream_eof() {
  122. return $this->_position >= strlen($this->_data);
  123. }
  124. /**
  125. * Seek stream
  126. */
  127. public function stream_seek($offset, $whence) {
  128. switch ($whence) {
  129. case SEEK_SET:
  130. if ($offset < strlen($this->_data) && $offset >= 0) {
  131. $this->_position = $offset;
  132. return true;
  133. } else {
  134. return false;
  135. }
  136. break;
  137. case SEEK_CUR:
  138. if ($offset >= 0) {
  139. $this->_position += $offset;
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. break;
  145. case SEEK_END:
  146. if (strlen($this->_data) + $offset >= 0) {
  147. $this->_position = strlen($this->_data) + $offset;
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. break;
  153. default:
  154. return false;
  155. }
  156. }
  157. }