archive.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Bart Visscher <bartv@thisnet.nl>
  5. * @author Christopher Schäpers <kondou@ts.unde.re>
  6. * @author Felix Moeller <mail@felixmoeller.de>
  7. * @author Joas Schilling <nickvergessen@owncloud.com>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Lukas Reschke <lukas@owncloud.com>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Remco Brenninkmeijer <requist1@starmail.nl>
  12. * @author Robin Appelman <icewind@owncloud.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. *
  15. * @copyright Copyright (c) 2015, ownCloud, Inc.
  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. * @param string $path
  35. * @return OC_Archive|void
  36. */
  37. public static function open($path) {
  38. $ext=substr($path, strrpos($path, '.'));
  39. switch($ext) {
  40. case '.zip':
  41. return new OC_Archive_ZIP($path);
  42. case '.gz':
  43. case '.bz':
  44. case '.bz2':
  45. case '.tgz':
  46. case '.tar':
  47. return new OC_Archive_TAR($path);
  48. }
  49. }
  50. /**
  51. * @param $source
  52. */
  53. abstract function __construct($source);
  54. /**
  55. * add an empty folder to the archive
  56. * @param string $path
  57. * @return bool
  58. */
  59. abstract function addFolder($path);
  60. /**
  61. * add a file to the archive
  62. * @param string $path
  63. * @param string $source either a local file or string data
  64. * @return bool
  65. */
  66. abstract function addFile($path, $source='');
  67. /**
  68. * rename a file or folder in the archive
  69. * @param string $source
  70. * @param string $dest
  71. * @return bool
  72. */
  73. abstract function rename($source, $dest);
  74. /**
  75. * get the uncompressed size of a file in the archive
  76. * @param string $path
  77. * @return int
  78. */
  79. abstract function filesize($path);
  80. /**
  81. * get the last modified time of a file in the archive
  82. * @param string $path
  83. * @return int
  84. */
  85. abstract function mtime($path);
  86. /**
  87. * get the files in a folder
  88. * @param string $path
  89. * @return array
  90. */
  91. abstract function getFolder($path);
  92. /**
  93. * get all files in the archive
  94. * @return array
  95. */
  96. abstract function getFiles();
  97. /**
  98. * get the content of a file
  99. * @param string $path
  100. * @return string
  101. */
  102. abstract function getFile($path);
  103. /**
  104. * extract a single file from the archive
  105. * @param string $path
  106. * @param string $dest
  107. * @return bool
  108. */
  109. abstract function extractFile($path, $dest);
  110. /**
  111. * extract the archive
  112. * @param string $dest
  113. * @return bool
  114. */
  115. abstract function extract($dest);
  116. /**
  117. * check if a file or folder exists in the archive
  118. * @param string $path
  119. * @return bool
  120. */
  121. abstract function fileExists($path);
  122. /**
  123. * remove a file or folder from the archive
  124. * @param string $path
  125. * @return bool
  126. */
  127. abstract function remove($path);
  128. /**
  129. * get a file handler
  130. * @param string $path
  131. * @param string $mode
  132. * @return resource
  133. */
  134. abstract function getStream($path, $mode);
  135. /**
  136. * add a folder and all its content
  137. * @param string $path
  138. * @param string $source
  139. * @return boolean|null
  140. */
  141. function addRecursive($path, $source) {
  142. $dh = opendir($source);
  143. if(is_resource($dh)) {
  144. $this->addFolder($path);
  145. while (($file = readdir($dh)) !== false) {
  146. if($file=='.' or $file=='..') {
  147. continue;
  148. }
  149. if(is_dir($source.'/'.$file)) {
  150. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  151. }else{
  152. $this->addFile($path.'/'.$file, $source.'/'.$file);
  153. }
  154. }
  155. }
  156. }
  157. }