Archive.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC\Archive;
  8. abstract class Archive {
  9. abstract public function __construct(string $source);
  10. /**
  11. * add an empty folder to the archive
  12. */
  13. abstract public function addFolder(string $path): bool;
  14. /**
  15. * add a file to the archive
  16. * @param string $source either a local file or string data
  17. */
  18. abstract public function addFile(string $path, string $source = ''): bool;
  19. /**
  20. * rename a file or folder in the archive
  21. */
  22. abstract public function rename(string $source, string $dest): bool;
  23. /**
  24. * get the uncompressed size of a file in the archive
  25. */
  26. abstract public function filesize(string $path): false|int|float;
  27. /**
  28. * get the last modified time of a file in the archive
  29. * @return int|false
  30. */
  31. abstract public function mtime(string $path);
  32. /**
  33. * get the files in a folder
  34. * @param string $path
  35. * @return array
  36. */
  37. abstract public function getFolder(string $path): array;
  38. /**
  39. * get all files in the archive
  40. */
  41. abstract public function getFiles(): array;
  42. /**
  43. * get the content of a file
  44. * @return string|false
  45. */
  46. abstract public function getFile(string $path);
  47. /**
  48. * extract a single file from the archive
  49. */
  50. abstract public function extractFile(string $path, string $dest): bool;
  51. /**
  52. * extract the archive
  53. */
  54. abstract public function extract(string $dest): bool;
  55. /**
  56. * check if a file or folder exists in the archive
  57. */
  58. abstract public function fileExists(string $path): bool;
  59. /**
  60. * remove a file or folder from the archive
  61. */
  62. abstract public function remove(string $path): bool;
  63. /**
  64. * get a file handler
  65. * @return bool|resource
  66. */
  67. abstract public function getStream(string $path, string $mode);
  68. /**
  69. * add a folder and all its content
  70. */
  71. public function addRecursive(string $path, string $source): void {
  72. $dh = opendir($source);
  73. if (is_resource($dh)) {
  74. $this->addFolder($path);
  75. while (($file = readdir($dh)) !== false) {
  76. if ($file === '.' || $file === '..') {
  77. continue;
  78. }
  79. if (is_dir($source.'/'.$file)) {
  80. $this->addRecursive($path.'/'.$file, $source.'/'.$file);
  81. } else {
  82. $this->addFile($path.'/'.$file, $source.'/'.$file);
  83. }
  84. }
  85. }
  86. }
  87. }