EpubFile.php 2.3 KB

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