archive.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 Thomas Müller <thomas.mueller@tmit.eu>
  15. *
  16. * @license AGPL-3.0
  17. *
  18. * This code is free software: you can redistribute it and/or modify
  19. * it under the terms of the GNU Affero General Public License, version 3,
  20. * as published by the Free Software Foundation.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU Affero General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU Affero General Public License, version 3,
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>
  29. *
  30. */
  31. abstract class OC_Archive{
  32. /**
  33. * Open any of the supported archive types
  34. *
  35. * @param string $path
  36. * @return OC_Archive|void
  37. */
  38. public static function open($path) {
  39. $mime = \OC::$server->getMimeTypeDetector()->detect($path);
  40. switch($mime) {
  41. case 'application/zip':
  42. return new OC_Archive_ZIP($path);
  43. case 'application/x-gzip':
  44. return new OC_Archive_TAR($path);
  45. case 'application/x-bzip2':
  46. return new OC_Archive_TAR($path);
  47. }
  48. }
  49. /**
  50. * @param $source
  51. */
  52. abstract function __construct($source);
  53. /**
  54. * add an empty folder to the archive
  55. * @param string $path
  56. * @return bool
  57. */
  58. abstract function addFolder($path);
  59. /**
  60. * add a file to the archive
  61. * @param string $path
  62. * @param string $source either a local file or string data
  63. * @return bool
  64. */
  65. abstract function addFile($path, $source='');
  66. /**
  67. * rename a file or folder in the archive
  68. * @param string $source
  69. * @param string $dest
  70. * @return bool
  71. */
  72. abstract function rename($source, $dest);
  73. /**
  74. * get the uncompressed size of a file in the archive
  75. * @param string $path
  76. * @return int
  77. */
  78. abstract function filesize($path);
  79. /**
  80. * get the last modified time of a file in the archive
  81. * @param string $path
  82. * @return int
  83. */
  84. abstract function mtime($path);
  85. /**
  86. * get the files in a folder
  87. * @param string $path
  88. * @return array
  89. */
  90. abstract function getFolder($path);
  91. /**
  92. * get all files in the archive
  93. * @return array
  94. */
  95. abstract function getFiles();
  96. /**
  97. * get the content of a file
  98. * @param string $path
  99. * @return string
  100. */
  101. abstract function getFile($path);
  102. /**
  103. * extract a single file from the archive
  104. * @param string $path
  105. * @param string $dest
  106. * @return bool
  107. */
  108. abstract function extractFile($path, $dest);
  109. /**
  110. * extract the archive
  111. * @param string $dest
  112. * @return bool
  113. */
  114. abstract function extract($dest);
  115. /**
  116. * check if a file or folder exists in the archive
  117. * @param string $path
  118. * @return bool
  119. */
  120. abstract function fileExists($path);
  121. /**
  122. * remove a file or folder from the archive
  123. * @param string $path
  124. * @return bool
  125. */
  126. abstract function remove($path);
  127. /**
  128. * get a file handler
  129. * @param string $path
  130. * @param string $mode
  131. * @return resource
  132. */
  133. abstract function getStream($path, $mode);
  134. /**
  135. * add a folder and all its content
  136. * @param string $path
  137. * @param string $source
  138. * @return boolean|null
  139. */
  140. function addRecursive($path, $source) {
  141. $dh = opendir($source);
  142. if(is_resource($dh)) {
  143. $this->addFolder($path);
  144. while (($file = readdir($dh)) !== false) {
  145. if($file=='.' or $file=='..') {
  146. continue;
  147. }
  148. if(is_dir($source.'/'.$file)) {
  149. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  150. }else{
  151. $this->addFile($path.'/'.$file, $source.'/'.$file);
  152. }
  153. }
  154. }
  155. }
  156. }