Tar.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Drupal\Core\Archiver;
  3. /**
  4. * Defines a archiver implementation for .tar files.
  5. */
  6. class Tar implements ArchiverInterface {
  7. /**
  8. * The underlying ArchiveTar instance that does the heavy lifting.
  9. *
  10. * @var \Drupal\Core\Archiver\ArchiveTar
  11. */
  12. protected $tar;
  13. /**
  14. * Constructs a Tar object.
  15. *
  16. * @param string $file_path
  17. * The full system path of the archive to manipulate. Only local files
  18. * are supported. If the file does not yet exist, it will be created if
  19. * appropriate.
  20. *
  21. * @throws \Drupal\Core\Archiver\ArchiverException
  22. */
  23. public function __construct($file_path) {
  24. $this->tar = new ArchiveTar($file_path);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. public function add($file_path) {
  30. $this->tar->add($file_path);
  31. return $this;
  32. }
  33. /**
  34. * {@inheritdoc}
  35. */
  36. public function remove($file_path) {
  37. // @todo Archive_Tar doesn't have a remove operation
  38. // so we'll have to simulate it somehow, probably by
  39. // creating a new archive with everything but the removed
  40. // file.
  41. return $this;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function extract($path, array $files = []) {
  47. if ($files) {
  48. $this->tar->extractList($files, $path);
  49. }
  50. else {
  51. $this->tar->extract($path);
  52. }
  53. return $this;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function listContents() {
  59. $files = [];
  60. foreach ($this->tar->listContent() as $file_data) {
  61. $files[] = $file_data['filename'];
  62. }
  63. return $files;
  64. }
  65. /**
  66. * Retrieves the tar engine itself.
  67. *
  68. * In some cases it may be necessary to directly access the underlying
  69. * Archive_Tar object for implementation-specific logic. This is for advanced
  70. * use only as it is not shared by other implementations of ArchiveInterface.
  71. *
  72. * @return Archive_Tar
  73. * The Archive_Tar object used by this object.
  74. */
  75. public function getArchive() {
  76. return $this->tar;
  77. }
  78. }