ZIP.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Christopher Schäpers <kondou@ts.unde.re>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. * @author Stefan Weil <sw@weilnetz.de>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\Archive;
  33. use Icewind\Streams\CallbackWrapper;
  34. use Psr\Log\LoggerInterface;
  35. class ZIP extends Archive {
  36. /**
  37. * @var \ZipArchive zip
  38. */
  39. private $zip;
  40. /**
  41. * @var string
  42. */
  43. private $path;
  44. public function __construct(string $source) {
  45. $this->path = $source;
  46. $this->zip = new \ZipArchive();
  47. if ($this->zip->open($source, \ZipArchive::CREATE)) {
  48. } else {
  49. \OC::$server->get(LoggerInterface::class)->warning('Error while opening archive '.$source, ['app' => 'files_archive']);
  50. }
  51. }
  52. /**
  53. * add an empty folder to the archive
  54. * @param string $path
  55. * @return bool
  56. */
  57. public function addFolder(string $path): bool {
  58. return $this->zip->addEmptyDir($path);
  59. }
  60. /**
  61. * add a file to the archive
  62. * @param string $source either a local file or string data
  63. */
  64. public function addFile(string $path, string $source = ''): bool {
  65. if ($source and $source[0] == '/' and file_exists($source)) {
  66. $result = $this->zip->addFile($source, $path);
  67. } else {
  68. $result = $this->zip->addFromString($path, $source);
  69. }
  70. if ($result) {
  71. $this->zip->close();//close and reopen to save the zip
  72. $this->zip->open($this->path);
  73. }
  74. return $result;
  75. }
  76. /**
  77. * rename a file or folder in the archive
  78. */
  79. public function rename(string $source, string $dest): bool {
  80. $source = $this->stripPath($source);
  81. $dest = $this->stripPath($dest);
  82. return $this->zip->renameName($source, $dest);
  83. }
  84. /**
  85. * get the uncompressed size of a file in the archive
  86. */
  87. public function filesize(string $path): false|int|float {
  88. $stat = $this->zip->statName($path);
  89. return $stat['size'] ?? false;
  90. }
  91. /**
  92. * get the last modified time of a file in the archive
  93. * @return int|false
  94. */
  95. public function mtime(string $path) {
  96. return filemtime($this->path);
  97. }
  98. /**
  99. * get the files in a folder
  100. */
  101. public function getFolder(string $path): array {
  102. // FIXME: multiple calls on getFolder would traverse
  103. // the whole file list over and over again
  104. // maybe use a Generator or cache the list ?
  105. $files = $this->getFiles();
  106. $folderContent = [];
  107. $pathLength = strlen($path);
  108. foreach ($files as $file) {
  109. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  110. if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
  111. $folderContent[] = substr($file, $pathLength);
  112. }
  113. }
  114. }
  115. return $folderContent;
  116. }
  117. /**
  118. * Generator that returns metadata of all files
  119. *
  120. * @return \Generator<array>
  121. */
  122. public function getAllFilesStat() {
  123. $fileCount = $this->zip->numFiles;
  124. for ($i = 0;$i < $fileCount;$i++) {
  125. yield $this->zip->statIndex($i);
  126. }
  127. }
  128. /**
  129. * Return stat information for the given path
  130. *
  131. * @param string path path to get stat information on
  132. * @return ?array stat information or null if not found
  133. */
  134. public function getStat(string $path): ?array {
  135. $stat = $this->zip->statName($path);
  136. if (!$stat) {
  137. return null;
  138. }
  139. return $stat;
  140. }
  141. /**
  142. * get all files in the archive
  143. */
  144. public function getFiles(): array {
  145. $fileCount = $this->zip->numFiles;
  146. $files = [];
  147. for ($i = 0;$i < $fileCount;$i++) {
  148. $files[] = $this->zip->getNameIndex($i);
  149. }
  150. return $files;
  151. }
  152. /**
  153. * get the content of a file
  154. * @return string|false
  155. */
  156. public function getFile(string $path) {
  157. return $this->zip->getFromName($path);
  158. }
  159. /**
  160. * extract a single file from the archive
  161. */
  162. public function extractFile(string $path, string $dest): bool {
  163. $fp = $this->zip->getStream($path);
  164. if ($fp === false) {
  165. return false;
  166. }
  167. return file_put_contents($dest, $fp) !== false;
  168. }
  169. /**
  170. * extract the archive
  171. */
  172. public function extract(string $dest): bool {
  173. return $this->zip->extractTo($dest);
  174. }
  175. /**
  176. * check if a file or folder exists in the archive
  177. */
  178. public function fileExists(string $path): bool {
  179. return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
  180. }
  181. /**
  182. * remove a file or folder from the archive
  183. */
  184. public function remove(string $path): bool {
  185. if ($this->fileExists($path.'/')) {
  186. return $this->zip->deleteName($path.'/');
  187. } else {
  188. return $this->zip->deleteName($path);
  189. }
  190. }
  191. /**
  192. * get a file handler
  193. * @return bool|resource
  194. */
  195. public function getStream(string $path, string $mode) {
  196. if ($mode == 'r' or $mode == 'rb') {
  197. return $this->zip->getStream($path);
  198. } else {
  199. //since we can't directly get a writable stream,
  200. //make a temp copy of the file and put it back
  201. //in the archive when the stream is closed
  202. $lastPoint = strrpos($path, '.');
  203. if ($lastPoint !== false) {
  204. $ext = substr($path, $lastPoint);
  205. } else {
  206. $ext = '';
  207. }
  208. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  209. if ($this->fileExists($path)) {
  210. $this->extractFile($path, $tmpFile);
  211. }
  212. $handle = fopen($tmpFile, $mode);
  213. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  214. $this->writeBack($tmpFile, $path);
  215. });
  216. }
  217. }
  218. /**
  219. * write back temporary files
  220. */
  221. public function writeBack(string $tmpFile, string $path): void {
  222. $this->addFile($path, $tmpFile);
  223. unlink($tmpFile);
  224. }
  225. private function stripPath(string $path): string {
  226. if (!$path || $path[0] == '/') {
  227. return substr($path, 1);
  228. } else {
  229. return $path;
  230. }
  231. }
  232. }