system.archiver.inc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * @file
  4. * Archiver implementations provided by the system module.
  5. */
  6. /**
  7. * Archiver for .tar files.
  8. */
  9. class ArchiverTar implements ArchiverInterface {
  10. /**
  11. * The underlying Archive_Tar instance that does the heavy lifting.
  12. *
  13. * @var Archive_Tar
  14. */
  15. protected $tar;
  16. public function __construct($file_path) {
  17. $this->tar = new Archive_Tar($file_path);
  18. }
  19. public function add($file_path) {
  20. $this->tar->add($file_path);
  21. return $this;
  22. }
  23. public function remove($file_path) {
  24. // @todo Archive_Tar doesn't have a remove operation
  25. // so we'll have to simulate it somehow, probably by
  26. // creating a new archive with everything but the removed
  27. // file.
  28. return $this;
  29. }
  30. public function extract($path, Array $files = array()) {
  31. if ($files) {
  32. $this->tar->extractList($files, $path, '', FALSE, FALSE);
  33. }
  34. else {
  35. $this->tar->extract($path, FALSE, FALSE);
  36. }
  37. return $this;
  38. }
  39. public function listContents() {
  40. $files = array();
  41. foreach ($this->tar->listContent() as $file_data) {
  42. $files[] = $file_data['filename'];
  43. }
  44. return $files;
  45. }
  46. /**
  47. * Retrieve the tar engine itself.
  48. *
  49. * In some cases it may be necessary to directly access the underlying
  50. * Archive_Tar object for implementation-specific logic. This is for advanced
  51. * use only as it is not shared by other implementations of ArchiveInterface.
  52. *
  53. * @return
  54. * The Archive_Tar object used by this object.
  55. */
  56. public function getArchive() {
  57. return $this->tar;
  58. }
  59. }
  60. /**
  61. * Archiver for .zip files.
  62. *
  63. * @link http://php.net/zip
  64. */
  65. class ArchiverZip implements ArchiverInterface {
  66. /**
  67. * The underlying ZipArchive instance that does the heavy lifting.
  68. *
  69. * @var ZipArchive
  70. */
  71. protected $zip;
  72. public function __construct($file_path) {
  73. $this->zip = new ZipArchive();
  74. if ($this->zip->open($file_path) !== TRUE) {
  75. // @todo: This should be an interface-specific exception some day.
  76. throw new Exception(t('Cannot open %file_path', array('%file_path' => $file_path)));
  77. }
  78. }
  79. public function add($file_path) {
  80. $this->zip->addFile($file_path);
  81. return $this;
  82. }
  83. public function remove($file_path) {
  84. $this->zip->deleteName($file_path);
  85. return $this;
  86. }
  87. public function extract($path, Array $files = array()) {
  88. if ($files) {
  89. $this->zip->extractTo($path, $files);
  90. }
  91. else {
  92. $this->zip->extractTo($path);
  93. }
  94. return $this;
  95. }
  96. public function listContents() {
  97. $files = array();
  98. for ($i=0; $i < $this->zip->numFiles; $i++) {
  99. $files[] = $this->zip->getNameIndex($i);
  100. }
  101. return $files;
  102. }
  103. /**
  104. * Retrieve the zip engine itself.
  105. *
  106. * In some cases it may be necessary to directly access the underlying
  107. * ZipArchive object for implementation-specific logic. This is for advanced
  108. * use only as it is not shared by other implementations of ArchiveInterface.
  109. *
  110. * @return
  111. * The ZipArchive object used by this object.
  112. */
  113. public function getArchive() {
  114. return $this->zip;
  115. }
  116. }