Archive.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christopher Schäpers <kondou@ts.unde.re>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  10. * @author Lukas Reschke <lukas@statuscode.ch>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <robin@icewind.nl>
  13. * @author Roeland Jago Douma <roeland@famdouma.nl>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program 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
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OC\Archive;
  31. abstract class Archive {
  32. abstract public function __construct(string $source);
  33. /**
  34. * add an empty folder to the archive
  35. */
  36. abstract public function addFolder(string $path): bool;
  37. /**
  38. * add a file to the archive
  39. * @param string $source either a local file or string data
  40. */
  41. abstract public function addFile(string $path, string $source = ''): bool;
  42. /**
  43. * rename a file or folder in the archive
  44. */
  45. abstract public function rename(string $source, string $dest): bool;
  46. /**
  47. * get the uncompressed size of a file in the archive
  48. */
  49. abstract public function filesize(string $path): false|int|float;
  50. /**
  51. * get the last modified time of a file in the archive
  52. * @return int|false
  53. */
  54. abstract public function mtime(string $path);
  55. /**
  56. * get the files in a folder
  57. * @param string $path
  58. * @return array
  59. */
  60. abstract public function getFolder(string $path): array;
  61. /**
  62. * get all files in the archive
  63. */
  64. abstract public function getFiles(): array;
  65. /**
  66. * get the content of a file
  67. * @return string|false
  68. */
  69. abstract public function getFile(string $path);
  70. /**
  71. * extract a single file from the archive
  72. */
  73. abstract public function extractFile(string $path, string $dest): bool;
  74. /**
  75. * extract the archive
  76. */
  77. abstract public function extract(string $dest): bool;
  78. /**
  79. * check if a file or folder exists in the archive
  80. */
  81. abstract public function fileExists(string $path): bool;
  82. /**
  83. * remove a file or folder from the archive
  84. */
  85. abstract public function remove(string $path): bool;
  86. /**
  87. * get a file handler
  88. * @return bool|resource
  89. */
  90. abstract public function getStream(string $path, string $mode);
  91. /**
  92. * add a folder and all its content
  93. */
  94. public function addRecursive(string $path, string $source): void {
  95. $dh = opendir($source);
  96. if (is_resource($dh)) {
  97. $this->addFolder($path);
  98. while (($file = readdir($dh)) !== false) {
  99. if ($file === '.' || $file === '..') {
  100. continue;
  101. }
  102. if (is_dir($source.'/'.$file)) {
  103. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  104. } else {
  105. $this->addFile($path.'/'.$file, $source.'/'.$file);
  106. }
  107. }
  108. }
  109. }
  110. }