archive.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. abstract class OC_Archive{
  9. /**
  10. * open any of the supported archive types
  11. * @param string $path
  12. * @return OC_Archive|void
  13. */
  14. public static function open($path) {
  15. $ext=substr($path, strrpos($path, '.'));
  16. switch($ext) {
  17. case '.zip':
  18. return new OC_Archive_ZIP($path);
  19. case '.gz':
  20. case '.bz':
  21. case '.bz2':
  22. if(strpos($path, '.tar.')) {
  23. return new OC_Archive_TAR($path);
  24. }
  25. break;
  26. case '.tgz':
  27. return new OC_Archive_TAR($path);
  28. }
  29. }
  30. /**
  31. * @param $source
  32. */
  33. abstract function __construct($source);
  34. /**
  35. * add an empty folder to the archive
  36. * @param string $path
  37. * @return bool
  38. */
  39. abstract function addFolder($path);
  40. /**
  41. * add a file to the archive
  42. * @param string $path
  43. * @param string $source either a local file or string data
  44. * @return bool
  45. */
  46. abstract function addFile($path, $source='');
  47. /**
  48. * rename a file or folder in the archive
  49. * @param string $source
  50. * @param string $dest
  51. * @return bool
  52. */
  53. abstract function rename($source, $dest);
  54. /**
  55. * get the uncompressed size of a file in the archive
  56. * @param string $path
  57. * @return int
  58. */
  59. abstract function filesize($path);
  60. /**
  61. * get the last modified time of a file in the archive
  62. * @param string $path
  63. * @return int
  64. */
  65. abstract function mtime($path);
  66. /**
  67. * get the files in a folder
  68. * @param string $path
  69. * @return array
  70. */
  71. abstract function getFolder($path);
  72. /**
  73. * get all files in the archive
  74. * @return array
  75. */
  76. abstract function getFiles();
  77. /**
  78. * get the content of a file
  79. * @param string $path
  80. * @return string
  81. */
  82. abstract function getFile($path);
  83. /**
  84. * extract a single file from the archive
  85. * @param string $path
  86. * @param string $dest
  87. * @return bool
  88. */
  89. abstract function extractFile($path, $dest);
  90. /**
  91. * extract the archive
  92. * @param string $dest
  93. * @return bool
  94. */
  95. abstract function extract($dest);
  96. /**
  97. * check if a file or folder exists in the archive
  98. * @param string $path
  99. * @return bool
  100. */
  101. abstract function fileExists($path);
  102. /**
  103. * remove a file or folder from the archive
  104. * @param string $path
  105. * @return bool
  106. */
  107. abstract function remove($path);
  108. /**
  109. * get a file handler
  110. * @param string $path
  111. * @param string $mode
  112. * @return resource
  113. */
  114. abstract function getStream($path, $mode);
  115. /**
  116. * add a folder and all its content
  117. * @param string $path
  118. * @param string $source
  119. * @return boolean|null
  120. */
  121. function addRecursive($path, $source) {
  122. $dh = opendir($source);
  123. if(is_resource($dh)) {
  124. $this->addFolder($path);
  125. while (($file = readdir($dh)) !== false) {
  126. if($file=='.' or $file=='..') {
  127. continue;
  128. }
  129. if(is_dir($source.'/'.$file)) {
  130. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  131. }else{
  132. $this->addFile($path.'/'.$file, $source.'/'.$file);
  133. }
  134. }
  135. }
  136. }
  137. }