ZipArchive.php 5.1 KB

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