EpubFile.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /** @noinspection PhpComposerExtensionStubsInspection */
  3. namespace PhpZip\Tests\Internal\Epub;
  4. use PhpZip\Constants\ZipPlatform;
  5. use PhpZip\Exception\ZipEntryNotFoundException;
  6. use PhpZip\Exception\ZipException;
  7. use PhpZip\IO\ZipReader;
  8. use PhpZip\IO\ZipWriter;
  9. use PhpZip\Model\ImmutableZipContainer;
  10. use PhpZip\Model\ZipContainer;
  11. use PhpZip\Model\ZipEntry;
  12. use PhpZip\ZipFile;
  13. /**
  14. * Class EpubFile.
  15. *
  16. * @property EpubZipContainer $zipContainer
  17. */
  18. class EpubFile extends ZipFile
  19. {
  20. /**
  21. * @return ZipWriter
  22. */
  23. protected function createZipWriter()
  24. {
  25. return new EpubWriter($this->zipContainer);
  26. }
  27. /**
  28. * @param resource $inputStream
  29. * @param array $options
  30. *
  31. * @return ZipReader
  32. */
  33. protected function createZipReader($inputStream, array $options = [])
  34. {
  35. return new EpubReader($inputStream, $options);
  36. }
  37. /**
  38. * @param ImmutableZipContainer|null $sourceContainer
  39. *
  40. * @return ZipContainer
  41. */
  42. protected function createZipContainer(ImmutableZipContainer $sourceContainer = null)
  43. {
  44. return new EpubZipContainer($sourceContainer);
  45. }
  46. /**
  47. * @param ZipEntry $zipEntry
  48. */
  49. protected function addZipEntry(ZipEntry $zipEntry)
  50. {
  51. $zipEntry->setCreatedOS(ZipPlatform::OS_DOS);
  52. $zipEntry->setExtractedOS(ZipPlatform::OS_UNIX);
  53. parent::addZipEntry($zipEntry);
  54. }
  55. /**
  56. * @throws ZipEntryNotFoundException
  57. *
  58. * @return string
  59. */
  60. public function getMimeType()
  61. {
  62. return $this->zipContainer->getMimeType();
  63. }
  64. public function getEpubInfo()
  65. {
  66. return new EpubInfo($this->getEntryContents($this->getRootFile()));
  67. }
  68. /**
  69. * @throws ZipException
  70. *
  71. * @return string
  72. */
  73. public function getRootFile()
  74. {
  75. $entryName = 'META-INF/container.xml';
  76. $contents = $this->getEntryContents($entryName);
  77. $doc = new \DOMDocument();
  78. $doc->loadXML($contents);
  79. $xpath = new \DOMXPath($doc);
  80. $rootFile = $xpath->evaluate('string(//@full-path)');
  81. if ($rootFile === '') {
  82. throw new ZipException('Incorrect ' . $entryName . ' file format');
  83. }
  84. return $rootFile;
  85. }
  86. }