Archive.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Robin Appelman <robin@icewind.nl>
  12. * @author Roeland Jago Douma <roeland@famdouma.nl>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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. public abstract function __construct($source);
  36. /**
  37. * add an empty folder to the archive
  38. * @param string $path
  39. * @return bool
  40. */
  41. public abstract 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. public abstract 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. public abstract 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. public abstract 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. public abstract function mtime($path);
  68. /**
  69. * get the files in a folder
  70. * @param string $path
  71. * @return array
  72. */
  73. public abstract function getFolder($path);
  74. /**
  75. * get all files in the archive
  76. * @return array
  77. */
  78. public abstract function getFiles();
  79. /**
  80. * get the content of a file
  81. * @param string $path
  82. * @return string
  83. */
  84. public abstract 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. public abstract function extractFile($path, $dest);
  92. /**
  93. * extract the archive
  94. * @param string $dest
  95. * @return bool
  96. */
  97. public abstract function extract($dest);
  98. /**
  99. * check if a file or folder exists in the archive
  100. * @param string $path
  101. * @return bool
  102. */
  103. public abstract function fileExists($path);
  104. /**
  105. * remove a file or folder from the archive
  106. * @param string $path
  107. * @return bool
  108. */
  109. public abstract function remove($path);
  110. /**
  111. * get a file handler
  112. * @param string $path
  113. * @param string $mode
  114. * @return resource
  115. */
  116. public abstract 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. }