Archive.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * @return int|false
  49. */
  50. abstract public function filesize(string $path);
  51. /**
  52. * get the last modified time of a file in the archive
  53. * @return int|false
  54. */
  55. abstract public function mtime(string $path);
  56. /**
  57. * get the files in a folder
  58. * @param string $path
  59. * @return array
  60. */
  61. abstract public function getFolder(string $path): array;
  62. /**
  63. * get all files in the archive
  64. */
  65. abstract public function getFiles(): array;
  66. /**
  67. * get the content of a file
  68. * @return string|false
  69. */
  70. abstract public function getFile(string $path);
  71. /**
  72. * extract a single file from the archive
  73. */
  74. abstract public function extractFile(string $path, string $dest): bool;
  75. /**
  76. * extract the archive
  77. */
  78. abstract public function extract(string $dest): bool;
  79. /**
  80. * check if a file or folder exists in the archive
  81. */
  82. abstract public function fileExists(string $path): bool;
  83. /**
  84. * remove a file or folder from the archive
  85. */
  86. abstract public function remove(string $path): bool;
  87. /**
  88. * get a file handler
  89. * @return bool|resource
  90. */
  91. abstract public function getStream(string $path, string $mode);
  92. /**
  93. * add a folder and all its content
  94. */
  95. public function addRecursive(string $path, string $source): void {
  96. $dh = opendir($source);
  97. if (is_resource($dh)) {
  98. $this->addFolder($path);
  99. while (($file = readdir($dh)) !== false) {
  100. if ($file === '.' || $file === '..') {
  101. continue;
  102. }
  103. if (is_dir($source.'/'.$file)) {
  104. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  105. } else {
  106. $this->addFile($path.'/'.$file, $source.'/'.$file);
  107. }
  108. }
  109. }
  110. }
  111. }