You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.6 KiB

3 years ago
  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. /**
  33. * @param $source
  34. */
  35. abstract public function __construct($source);
  36. /**
  37. * add an empty folder to the archive
  38. * @param string $path
  39. * @return bool
  40. */
  41. abstract public function addFolder($path);
  42. /**
  43. * add a file to the archive
  44. * @param string $path
  45. * @param string $source either a local file or string data
  46. * @return bool
  47. */
  48. abstract public function addFile($path, $source='');
  49. /**
  50. * rename a file or folder in the archive
  51. * @param string $source
  52. * @param string $dest
  53. * @return bool
  54. */
  55. abstract public function rename($source, $dest);
  56. /**
  57. * get the uncompressed size of a file in the archive
  58. * @param string $path
  59. * @return int
  60. */
  61. abstract public function filesize($path);
  62. /**
  63. * get the last modified time of a file in the archive
  64. * @param string $path
  65. * @return int
  66. */
  67. abstract public function mtime($path);
  68. /**
  69. * get the files in a folder
  70. * @param string $path
  71. * @return array
  72. */
  73. abstract public function getFolder($path);
  74. /**
  75. * get all files in the archive
  76. * @return array
  77. */
  78. abstract public function getFiles();
  79. /**
  80. * get the content of a file
  81. * @param string $path
  82. * @return string
  83. */
  84. abstract public function getFile($path);
  85. /**
  86. * extract a single file from the archive
  87. * @param string $path
  88. * @param string $dest
  89. * @return bool
  90. */
  91. abstract public function extractFile($path, $dest);
  92. /**
  93. * extract the archive
  94. * @param string $dest
  95. * @return bool
  96. */
  97. abstract public function extract($dest);
  98. /**
  99. * check if a file or folder exists in the archive
  100. * @param string $path
  101. * @return bool
  102. */
  103. abstract public function fileExists($path);
  104. /**
  105. * remove a file or folder from the archive
  106. * @param string $path
  107. * @return bool
  108. */
  109. abstract public function remove($path);
  110. /**
  111. * get a file handler
  112. * @param string $path
  113. * @param string $mode
  114. * @return resource
  115. */
  116. abstract public function getStream($path, $mode);
  117. /**
  118. * add a folder and all its content
  119. * @param string $path
  120. * @param string $source
  121. */
  122. public function addRecursive($path, $source) {
  123. $dh = opendir($source);
  124. if (is_resource($dh)) {
  125. $this->addFolder($path);
  126. while (($file = readdir($dh)) !== false) {
  127. if ($file === '.' || $file === '..') {
  128. continue;
  129. }
  130. if (is_dir($source.'/'.$file)) {
  131. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  132. } else {
  133. $this->addFile($path.'/'.$file, $source.'/'.$file);
  134. }
  135. }
  136. }
  137. }
  138. }