PageRenderTime 24ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/common/libraries/plugin/phpexcel/PHPExcel/Shared/ZipStreamWrapper.php

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