/lib/phpexcel/PHPExcel/Shared/ZipArchive.php

https://github.com/vadimonus/moodle · PHP · 163 lines · 79 code · 20 blank · 64 comment · 21 complexity · 1e6c0818f61604752e8a78939b8cb637 MD5 · raw file

  1. <?php
  2. if (!defined('PCLZIP_TEMPORARY_DIR')) {
  3. define('PCLZIP_TEMPORARY_DIR', PHPExcel_Shared_File::sys_get_temp_dir() . DIRECTORY_SEPARATOR);
  4. }
  5. require_once PHPEXCEL_ROOT . 'PHPExcel/Shared/PCLZip/pclzip.lib.php';
  6. /**
  7. * PHPExcel_Shared_ZipArchive
  8. *
  9. * Copyright (c) 2006 - 2015 PHPExcel
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *
  25. * @category PHPExcel
  26. * @package PHPExcel_Shared_ZipArchive
  27. * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel)
  28. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  29. * @version ##VERSION##, ##DATE##
  30. */
  31. class PHPExcel_Shared_ZipArchive
  32. {
  33. /** constants */
  34. const OVERWRITE = 'OVERWRITE';
  35. const CREATE = 'CREATE';
  36. /**
  37. * Temporary storage directory
  38. *
  39. * @var string
  40. */
  41. private $tempDir;
  42. /**
  43. * Zip Archive Stream Handle
  44. *
  45. * @var string
  46. */
  47. private $zip;
  48. /**
  49. * Open a new zip archive
  50. *
  51. * @param string $fileName Filename for the zip archive
  52. * @return boolean
  53. */
  54. public function open($fileName)
  55. {
  56. $this->tempDir = PHPExcel_Shared_File::sys_get_temp_dir();
  57. $this->zip = new PclZip($fileName);
  58. return true;
  59. }
  60. /**
  61. * Close this zip archive
  62. *
  63. */
  64. public function close()
  65. {
  66. }
  67. /**
  68. * Add a new file to the zip archive from a string of raw data.
  69. *
  70. * @param string $localname Directory/Name of the file to add to the zip archive
  71. * @param string $contents String of data to add to the zip archive
  72. */
  73. public function addFromString($localname, $contents)
  74. {
  75. $filenameParts = pathinfo($localname);
  76. $handle = fopen($this->tempDir.'/'.$filenameParts["basename"], "wb");
  77. fwrite($handle, $contents);
  78. fclose($handle);
  79. $res = $this->zip->add($this->tempDir.'/'.$filenameParts["basename"], PCLZIP_OPT_REMOVE_PATH, $this->tempDir, PCLZIP_OPT_ADD_PATH, $filenameParts["dirname"]);
  80. if ($res == 0) {
  81. throw new PHPExcel_Writer_Exception("Error zipping files : " . $this->zip->errorInfo(true));
  82. }
  83. unlink($this->tempDir.'/'.$filenameParts["basename"]);
  84. }
  85. /**
  86. * Find if given fileName exist in archive (Emulate ZipArchive locateName())
  87. *
  88. * @param string $fileName Filename for the file in zip archive
  89. * @return boolean
  90. */
  91. public function locateName($fileName)
  92. {
  93. $list = $this->zip->listContent();
  94. $listCount = count($list);
  95. $list_index = -1;
  96. for ($i = 0; $i < $listCount; ++$i) {
  97. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  98. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  99. $list_index = $i;
  100. break;
  101. }
  102. }
  103. return ($list_index > -1);
  104. }
  105. /**
  106. * Extract file from archive by given fileName (Emulate ZipArchive getFromName())
  107. *
  108. * @param string $fileName Filename for the file in zip archive
  109. * @return string $contents File string contents
  110. */
  111. public function getFromName($fileName)
  112. {
  113. $list = $this->zip->listContent();
  114. $listCount = count($list);
  115. $list_index = -1;
  116. for ($i = 0; $i < $listCount; ++$i) {
  117. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  118. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  119. $list_index = $i;
  120. break;
  121. }
  122. }
  123. $extracted = "";
  124. if ($list_index != -1) {
  125. $extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  126. } else {
  127. $filename = substr($fileName, 1);
  128. $list_index = -1;
  129. for ($i = 0; $i < $listCount; ++$i) {
  130. if (strtolower($list[$i]["filename"]) == strtolower($fileName) ||
  131. strtolower($list[$i]["stored_filename"]) == strtolower($fileName)) {
  132. $list_index = $i;
  133. break;
  134. }
  135. }
  136. $extracted = $this->zip->extractByIndex($list_index, PCLZIP_OPT_EXTRACT_AS_STRING);
  137. }
  138. if ((is_array($extracted)) && ($extracted != 0)) {
  139. $contents = $extracted[0]["content"];
  140. }
  141. return $contents;
  142. }
  143. }