PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/fsn-site-central/library/PHPExcel/Shared/ZipArchive.php

https://gitlab.com/team_fsn/fsn-php
PHP | 175 lines | 82 code | 22 blank | 71 comment | 22 complexity | e2fa9952bbefd2d81507e458d539ded4 MD5 | raw file
  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_ZipArchive
  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 1.8.0, 2014-03-02
  26. */
  27. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  28. define('PCLZIP_TEMPORARY_DIR', PHPExcel_Shared_File::sys_get_temp_dir());
  29. }
  30. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PCLZip/pclzip.lib.php';
  31. /**
  32. * PHPExcel_Shared_ZipArchive
  33. *
  34. * @category PHPExcel
  35. * @package PHPExcel_Shared_ZipArchive
  36. * @copyright Copyright (c) 2006 - 2014 PHPExcel (http://www.codeplex.com/PHPExcel)
  37. */
  38. class PHPExcel_Shared_ZipArchive
  39. {
  40. /** constants */
  41. const OVERWRITE = 'OVERWRITE';
  42. const CREATE = 'CREATE';
  43. /**
  44. * Temporary storage directory
  45. *
  46. * @var string
  47. */
  48. private $_tempDir;
  49. /**
  50. * Zip Archive Stream Handle
  51. *
  52. * @var string
  53. */
  54. private $_zip;
  55. /**
  56. * Open a new zip archive
  57. *
  58. * @param string $fileName Filename for the zip archive
  59. * @return boolean
  60. */
  61. public function open($fileName)
  62. {
  63. $this->_tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  64. $this->_zip = new PclZip($fileName);
  65. return true;
  66. }
  67. /**
  68. * Close this zip archive
  69. *
  70. */
  71. public function close()
  72. {
  73. }
  74. /**
  75. * Add a new file to the zip archive from a string of raw data.
  76. *
  77. * @param string $localname Directory/Name of the file to add to the zip archive
  78. * @param string $contents String of data to add to the zip archive
  79. */
  80. public function addFromString($localname, $contents)
  81. {
  82. $filenameParts = pathinfo($localname);
  83. $handle = fopen($this->_tempDir.'/'.$filenameParts["basename"], "wb");
  84. fwrite($handle, $contents);
  85. fclose($handle);
  86. $res = $this->_zip->add($this->_tempDir.'/'.$filenameParts["basename"],
  87. PCLZIP_OPT_REMOVE_PATH, $this->_tempDir,
  88. PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"]
  89. );
  90. if ($res == 0) {
  91. throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->_zip->errorInfo(true));
  92. }
  93. unlink($this->_tempDir.'/'.$filenameParts["basename"]);
  94. }
  95. /**
  96. * Find if given fileName exist in archive (Emulate ZipArchive locateName())
  97. *
  98. * @param string $fileName Filename for the file in zip archive
  99. * @return boolean
  100. */
  101. public function locateName($fileName)
  102. {
  103. $list = $this->_zip->listContent();
  104. $listCount = count($list);
  105. $list_index = -1;
  106. for ($i = 0; $i < $listCount; ++$i) {
  107. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  108. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  109. $list_index = $i;
  110. break;
  111. }
  112. }
  113. return ($list_index > -1);
  114. }
  115. /**
  116. * Extract file from archive by given fileName (Emulate ZipArchive getFromName())
  117. *
  118. * @param string $fileName Filename for the file in zip archive
  119. * @return string $contents File string contents
  120. */
  121. public function getFromName($fileName)
  122. {
  123. $list = $this->_zip->listContent();
  124. $listCount = count($list);
  125. $list_index = -1;
  126. for ($i = 0; $i < $listCount; ++$i) {
  127. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  128. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  129. $list_index = $i;
  130. break;
  131. }
  132. }
  133. $extracted = "";
  134. if ($list_index != -1) {
  135. $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  136. } else {
  137. $filename = substr($fileName, 1);
  138. $list_index = -1;
  139. for ($i = 0; $i < $listCount; ++$i) {
  140. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  141. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  142. $list_index = $i;
  143. break;
  144. }
  145. }
  146. $extracted = $this->_zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  147. }
  148. if ((is_array($extracted)) && ($extracted != 0)) {
  149. $contents = $extracted[0]["content"];
  150. }
  151. return $contents;
  152. }
  153. }