archive.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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
  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. abstract function __construct($source);
  31. /**
  32. * add an empty folder to the archive
  33. * @param string path
  34. * @return bool
  35. */
  36. abstract function addFolder($path);
  37. /**
  38. * add a file to the archive
  39. * @param string path
  40. * @param string source either a local file or string data
  41. * @return bool
  42. */
  43. abstract function addFile($path, $source='');
  44. /**
  45. * rename a file or folder in the archive
  46. * @param string source
  47. * @param string dest
  48. * @return bool
  49. */
  50. abstract function rename($source, $dest);
  51. /**
  52. * get the uncompressed size of a file in the archive
  53. * @param string path
  54. * @return int
  55. */
  56. abstract function filesize($path);
  57. /**
  58. * get the last modified time of a file in the archive
  59. * @param string path
  60. * @return int
  61. */
  62. abstract function mtime($path);
  63. /**
  64. * get the files in a folder
  65. * @param path
  66. * @return array
  67. */
  68. abstract function getFolder($path);
  69. /**
  70. * get all files in the archive
  71. * @return array
  72. */
  73. abstract function getFiles();
  74. /**
  75. * get the content of a file
  76. * @param string path
  77. * @return string
  78. */
  79. abstract function getFile($path);
  80. /**
  81. * extract a single file from the archive
  82. * @param string path
  83. * @param string dest
  84. * @return bool
  85. */
  86. abstract function extractFile($path, $dest);
  87. /**
  88. * extract the archive
  89. * @param string path
  90. * @param string dest
  91. * @return bool
  92. */
  93. abstract function extract($dest);
  94. /**
  95. * check if a file or folder exists in the archive
  96. * @param string path
  97. * @return bool
  98. */
  99. abstract function fileExists($path);
  100. /**
  101. * remove a file or folder from the archive
  102. * @param string path
  103. * @return bool
  104. */
  105. abstract function remove($path);
  106. /**
  107. * get a file handler
  108. * @param string path
  109. * @param string mode
  110. * @return resource
  111. */
  112. abstract function getStream($path, $mode);
  113. /**
  114. * add a folder and all its content
  115. * @param string $path
  116. * @param string source
  117. * @return bool
  118. */
  119. function addRecursive($path, $source) {
  120. if($dh=opendir($source)) {
  121. $this->addFolder($path);
  122. while($file=readdir($dh)) {
  123. if($file=='.' or $file=='..') {
  124. continue;
  125. }
  126. if(is_dir($source.'/'.$file)) {
  127. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  128. }else{
  129. $this->addFile($path.'/'.$file, $source.'/'.$file);
  130. }
  131. }
  132. }
  133. }
  134. }