Issue24Test.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\Util\CryptoUtil;
  5. class Issue24Test extends ZipTestCase
  6. {
  7. /**
  8. * This method is called before the first test of this test class is run.
  9. */
  10. public static function setUpBeforeClass()
  11. {
  12. stream_wrapper_register("dummyfs", DummyFileSystemStream::class);
  13. }
  14. /**
  15. * @throws ZipException
  16. */
  17. public function testDummyFS()
  18. {
  19. $fileContents = str_repeat(base64_encode(CryptoUtil::randomBytes(12000)), 100);
  20. // create zip file
  21. $zip = new ZipFile();
  22. $zip->addFromString(
  23. 'file.txt',
  24. $fileContents,
  25. ZipFile::METHOD_DEFLATED
  26. );
  27. $zip->saveAsFile($this->outputFilename);
  28. $zip->close();
  29. $this->assertCorrectZipArchive($this->outputFilename);
  30. $stream = fopen('dummyfs://localhost/' . $this->outputFilename, 'rb');
  31. $this->assertNotFalse($stream);
  32. $zip->openFromStream($stream);
  33. $this->assertEquals($zip->getListFiles(), ['file.txt']);
  34. $this->assertEquals($zip['file.txt'], $fileContents);
  35. $zip->close();
  36. }
  37. }
  38. /**
  39. * Try to load using dummy stream
  40. */
  41. class DummyFileSystemStream
  42. {
  43. /**
  44. * @var resource
  45. */
  46. private $fp;
  47. public function stream_open($path, $mode, $options, &$opened_path)
  48. {
  49. // echo "DummyFileSystemStream->stream_open($path, $mode, $options)" . PHP_EOL;
  50. $parsedUrl = parse_url($path);
  51. $path = $parsedUrl['path'];
  52. $this->fp = fopen($path, $mode);
  53. return true;
  54. }
  55. public function stream_read($count)
  56. {
  57. // echo "DummyFileSystemStream->stream_read($count)" . PHP_EOL;
  58. $position = ftell($this->fp);
  59. // echo "Loading chunk " . $position . " to " . ($position + $count - 1) . PHP_EOL;
  60. $ret = fread($this->fp, $count);
  61. // echo "String length: " . strlen($ret) . PHP_EOL;
  62. return $ret;
  63. }
  64. public function stream_tell()
  65. {
  66. // echo "DummyFileSystemStream->stream_tell()" . PHP_EOL;
  67. return ftell($this->fp);
  68. }
  69. public function stream_eof()
  70. {
  71. // echo "DummyFileSystemStream->stream_eof()" . PHP_EOL;
  72. $isfeof = feof($this->fp);
  73. return $isfeof;
  74. }
  75. public function stream_seek($offset, $whence)
  76. {
  77. // echo "DummyFileSystemStream->stream_seek($offset, $whence)" . PHP_EOL;
  78. fseek($this->fp, $offset, $whence);
  79. }
  80. public function stream_stat()
  81. {
  82. // echo "DummyFileSystemStream->stream_stat()" . PHP_EOL;
  83. return fstat($this->fp);
  84. }
  85. }