ZIP.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. * @return int|false
  87. */
  88. public function filesize(string $path) {
  89. $stat = $this->zip->statName($path);
  90. return $stat['size'] ?? false;
  91. }
  92. /**
  93. * get the last modified time of a file in the archive
  94. * @return int|false
  95. */
  96. public function mtime(string $path) {
  97. return filemtime($this->path);
  98. }
  99. /**
  100. * get the files in a folder
  101. */
  102. public function getFolder(string $path): array {
  103. // FIXME: multiple calls on getFolder would traverse
  104. // the whole file list over and over again
  105. // maybe use a Generator or cache the list ?
  106. $files = $this->getFiles();
  107. $folderContent = [];
  108. $pathLength = strlen($path);
  109. foreach ($files as $file) {
  110. if (substr($file, 0, $pathLength) == $path and $file != $path) {
  111. if (strrpos(substr($file, 0, -1), '/') <= $pathLength) {
  112. $folderContent[] = substr($file, $pathLength);
  113. }
  114. }
  115. }
  116. return $folderContent;
  117. }
  118. /**
  119. * Generator that returns metadata of all files
  120. *
  121. * @return \Generator<array>
  122. */
  123. public function getAllFilesStat() {
  124. $fileCount = $this->zip->numFiles;
  125. for ($i = 0;$i < $fileCount;$i++) {
  126. yield $this->zip->statIndex($i);
  127. }
  128. }
  129. /**
  130. * Return stat information for the given path
  131. *
  132. * @param string path path to get stat information on
  133. * @return ?array stat information or null if not found
  134. */
  135. public function getStat(string $path): ?array {
  136. $stat = $this->zip->statName($path);
  137. if (!$stat) {
  138. return null;
  139. }
  140. return $stat;
  141. }
  142. /**
  143. * get all files in the archive
  144. */
  145. public function getFiles(): array {
  146. $fileCount = $this->zip->numFiles;
  147. $files = [];
  148. for ($i = 0;$i < $fileCount;$i++) {
  149. $files[] = $this->zip->getNameIndex($i);
  150. }
  151. return $files;
  152. }
  153. /**
  154. * get the content of a file
  155. * @return string|false
  156. */
  157. public function getFile(string $path) {
  158. return $this->zip->getFromName($path);
  159. }
  160. /**
  161. * extract a single file from the archive
  162. */
  163. public function extractFile(string $path, string $dest): bool {
  164. $fp = $this->zip->getStream($path);
  165. if ($fp === false) {
  166. return false;
  167. }
  168. return file_put_contents($dest, $fp) !== false;
  169. }
  170. /**
  171. * extract the archive
  172. */
  173. public function extract(string $dest): bool {
  174. return $this->zip->extractTo($dest);
  175. }
  176. /**
  177. * check if a file or folder exists in the archive
  178. */
  179. public function fileExists(string $path): bool {
  180. return ($this->zip->locateName($path) !== false) or ($this->zip->locateName($path.'/') !== false);
  181. }
  182. /**
  183. * remove a file or folder from the archive
  184. */
  185. public function remove(string $path): bool {
  186. if ($this->fileExists($path.'/')) {
  187. return $this->zip->deleteName($path.'/');
  188. } else {
  189. return $this->zip->deleteName($path);
  190. }
  191. }
  192. /**
  193. * get a file handler
  194. * @return bool|resource
  195. */
  196. public function getStream(string $path, string $mode) {
  197. if ($mode == 'r' or $mode == 'rb') {
  198. return $this->zip->getStream($path);
  199. } else {
  200. //since we can't directly get a writable stream,
  201. //make a temp copy of the file and put it back
  202. //in the archive when the stream is closed
  203. $lastPoint = strrpos($path, '.');
  204. if ($lastPoint !== false) {
  205. $ext = substr($path, $lastPoint);
  206. } else {
  207. $ext = '';
  208. }
  209. $tmpFile = \OC::$server->getTempManager()->getTemporaryFile($ext);
  210. if ($this->fileExists($path)) {
  211. $this->extractFile($path, $tmpFile);
  212. }
  213. $handle = fopen($tmpFile, $mode);
  214. return CallbackWrapper::wrap($handle, null, null, function () use ($path, $tmpFile) {
  215. $this->writeBack($tmpFile, $path);
  216. });
  217. }
  218. }
  219. /**
  220. * write back temporary files
  221. */
  222. public function writeBack(string $tmpFile, string $path): void {
  223. $this->addFile($path, $tmpFile);
  224. unlink($tmpFile);
  225. }
  226. private function stripPath(string $path): string {
  227. if (!$path || $path[0] == '/') {
  228. return substr($path, 1);
  229. } else {
  230. return $path;
  231. }
  232. }
  233. }