Archive.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 Felix Moeller <mail@felixmoeller.de>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Robin Appelman <robin@icewind.nl>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. abstract class Archive {
  34. /**
  35. * @param $source
  36. */
  37. abstract function __construct($source);
  38. /**
  39. * add an empty folder to the archive
  40. * @param string $path
  41. * @return bool
  42. */
  43. abstract function addFolder($path);
  44. /**
  45. * add a file to the archive
  46. * @param string $path
  47. * @param string $source either a local file or string data
  48. * @return bool
  49. */
  50. abstract function addFile($path, $source='');
  51. /**
  52. * rename a file or folder in the archive
  53. * @param string $source
  54. * @param string $dest
  55. * @return bool
  56. */
  57. abstract function rename($source, $dest);
  58. /**
  59. * get the uncompressed size of a file in the archive
  60. * @param string $path
  61. * @return int
  62. */
  63. abstract function filesize($path);
  64. /**
  65. * get the last modified time of a file in the archive
  66. * @param string $path
  67. * @return int
  68. */
  69. abstract function mtime($path);
  70. /**
  71. * get the files in a folder
  72. * @param string $path
  73. * @return array
  74. */
  75. abstract function getFolder($path);
  76. /**
  77. * get all files in the archive
  78. * @return array
  79. */
  80. abstract function getFiles();
  81. /**
  82. * get the content of a file
  83. * @param string $path
  84. * @return string
  85. */
  86. abstract function getFile($path);
  87. /**
  88. * extract a single file from the archive
  89. * @param string $path
  90. * @param string $dest
  91. * @return bool
  92. */
  93. abstract function extractFile($path, $dest);
  94. /**
  95. * extract the archive
  96. * @param string $dest
  97. * @return bool
  98. */
  99. abstract function extract($dest);
  100. /**
  101. * check if a file or folder exists in the archive
  102. * @param string $path
  103. * @return bool
  104. */
  105. abstract function fileExists($path);
  106. /**
  107. * remove a file or folder from the archive
  108. * @param string $path
  109. * @return bool
  110. */
  111. abstract function remove($path);
  112. /**
  113. * get a file handler
  114. * @param string $path
  115. * @param string $mode
  116. * @return resource
  117. */
  118. abstract function getStream($path, $mode);
  119. /**
  120. * add a folder and all its content
  121. * @param string $path
  122. * @param string $source
  123. * @return boolean|null
  124. */
  125. function addRecursive($path, $source) {
  126. $dh = opendir($source);
  127. if(is_resource($dh)) {
  128. $this->addFolder($path);
  129. while (($file = readdir($dh)) !== false) {
  130. if($file=='.' or $file=='..') {
  131. continue;
  132. }
  133. if(is_dir($source.'/'.$file)) {
  134. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  135. }else{
  136. $this->addFile($path.'/'.$file, $source.'/'.$file);
  137. }
  138. }
  139. }
  140. }
  141. }