ZIP.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Archive;
  8. use Icewind\Streams\CallbackWrapper;
  9. use Psr\Log\LoggerInterface;
  10. class ZIP extends Archive {
  11. /**
  12. * @var \ZipArchive zip
  13. */
  14. private $zip;
  15. /**
  16. * @var string
  17. */
  18. private $path;
  19. public function __construct(string $source) {
  20. $this->path = $source;
  21. $this->zip = new \ZipArchive();
  22. if ($this->zip->open($source, \ZipArchive::CREATE)) {
  23. } else {
  24. \OC::$server->get(LoggerInterface::class)->warning('Error while opening archive '.$source, ['app' => 'files_archive']);
  25. }
  26. }
  27. /**
  28. * add an empty folder to the archive
  29. * @param string $path
  30. * @return bool
  31. */
  32. public function addFolder(string $path): bool {
  33. return $this->zip->addEmptyDir($path);
  34. }
  35. /**
  36. * add a file to the archive
  37. * @param string $source either a local file or string data
  38. */
  39. public function addFile(string $path, string $source = ''): bool {
  40. if ($source and $source[0] == '/' and file_exists($source)) {
  41. $result = $this->zip->addFile($source, $path);
  42. } else {
  43. $result = $this->zip->addFromString($path, $source);
  44. }
  45. if ($result) {
  46. $this->zip->close();//close and reopen to save the zip
  47. $this->zip->open($this->path);
  48. }
  49. return $result;
  50. }
  51. /**
  52. * rename a file or folder in the archive
  53. */
  54. public function rename(string $source, string $dest): bool {
  55. $source = $this->stripPath($source);
  56. $dest = $this->stripPath($dest);
  57. return $this->zip->renameName($source, $dest);
  58. }
  59. /**
  60. * get the uncompressed size of a file in the archive
  61. */
  62. public function filesize(string $path): false|int|float {
  63. $stat = $this->zip->statName($path);
  64. return $stat['size'] ?? false;
  65. }
  66. /**
  67. * get the last modified time of a file in the archive
  68. * @return int|false
  69. */
  70. public function mtime(string $path) {
  71. return filemtime($this->path);
  72. }
  73. /**
  74. * get the files in a folder
  75. */
  76. public function getFolder(string $path): array {
  77. // FIXME: multiple calls on getFolder would traverse
  78. // the whole file list over and over again
  79. // maybe use a Generator or cache the list ?
  80. $files = $this->getFiles();
  81. $folderContent = [];
  82. $pathLength = strlen($path);
  83. foreach ($files as $file) {
  84. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  85. if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
  86. $folderContent[] = substr($file, $pathLength);
  87. }
  88. }
  89. }
  90. return $folderContent;
  91. }
  92. /**
  93. * Generator that returns metadata of all files
  94. *
  95. * @return \Generator<array>
  96. */
  97. public function getAllFilesStat() {
  98. $fileCount = $this->zip->numFiles;
  99. for ($i = 0;$i < $fileCount;$i++) {
  100. yield $this->zip->statIndex($i);
  101. }
  102. }
  103. /**
  104. * Return stat information for the given path
  105. *
  106. * @param string path path to get stat information on
  107. * @return ?array stat information or null if not found
  108. */
  109. public function getStat(string $path): ?array {
  110. $stat = $this->zip->statName($path);
  111. if (!$stat) {
  112. return null;
  113. }
  114. return $stat;
  115. }
  116. /**
  117. * get all files in the archive
  118. */
  119. public function getFiles(): array {
  120. $fileCount = $this->zip->numFiles;
  121. $files = [];
  122. for ($i = 0;$i < $fileCount;$i++) {
  123. $files[] = $this->zip->getNameIndex($i);
  124. }
  125. return $files;
  126. }
  127. /**
  128. * get the content of a file
  129. * @return string|false
  130. */
  131. public function getFile(string $path) {
  132. return $this->zip->getFromName($path);
  133. }
  134. /**
  135. * extract a single file from the archive
  136. */
  137. public function extractFile(string $path, string $dest): bool {
  138. $fp = $this->zip->getStream($path);
  139. if ($fp === false) {
  140. return false;
  141. }
  142. return file_put_contents($dest, $fp) !== false;
  143. }
  144. /**
  145. * extract the archive
  146. */
  147. public function extract(string $dest): bool {
  148. return $this->zip->extractTo($dest);
  149. }
  150. /**
  151. * check if a file or folder exists in the archive
  152. */
  153. public function fileExists(string $path): bool {
  154. return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
  155. }
  156. /**
  157. * remove a file or folder from the archive
  158. */
  159. public function remove(string $path): bool {
  160. if ($this->fileExists($path.'/')) {
  161. return $this->zip->deleteName($path.'/');
  162. } else {
  163. return $this->zip->deleteName($path);
  164. }
  165. }
  166. /**
  167. * get a file handler
  168. * @return bool|resource
  169. */
  170. public function getStream(string $path, string $mode) {
  171. if ($mode == 'r' or $mode == 'rb') {
  172. return $this->zip->getStream($path);
  173. } else {
  174. //since we can't directly get a writable stream,
  175. //make a temp copy of the file and put it back
  176. //in the archive when the stream is closed
  177. $lastPoint = strrpos($path, '.');
  178. if ($lastPoint !== false) {
  179. $ext = substr($path, $lastPoint);
  180. } else {
  181. $ext = '';
  182. }
  183. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  184. if ($this->fileExists($path)) {
  185. $this->extractFile($path, $tmpFile);
  186. }
  187. $handle = fopen($tmpFile, $mode);
  188. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  189. $this->writeBack($tmpFile, $path);
  190. });
  191. }
  192. }
  193. /**
  194. * write back temporary files
  195. */
  196. public function writeBack(string $tmpFile, string $path): void {
  197. $this->addFile($path, $tmpFile);
  198. unlink($tmpFile);
  199. }
  200. private function stripPath(string $path): string {
  201. if (!$path || $path[0] == '/') {
  202. return substr($path, 1);
  203. } else {
  204. return $path;
  205. }
  206. }
  207. }