ZipRemoteFileTest.php 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace PhpZip;
  3. use PhpZip\Exception\ZipException;
  4. use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
  5. use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;
  6. /**
  7. * Test add remote files to zip archive
  8. */
  9. class ZipRemoteFileTest extends ZipTestCase
  10. {
  11. protected function setUp()
  12. {
  13. parent::setUp();
  14. }
  15. /**
  16. * @throws ZipException
  17. */
  18. public function testAddRemoteFileFromStream()
  19. {
  20. $zipFile = new ZipFile();
  21. $outputZip = $this->outputFilename;
  22. $fileUrl = 'https://raw.githubusercontent.com/Ne-Lexa/php-zip/master/README.md';
  23. $fp = @fopen($fileUrl, 'rb', false, stream_context_create([
  24. 'http' => [
  25. 'timeout' => 3,
  26. ]
  27. ]));
  28. if ($fp === false) {
  29. self::markTestSkipped(sprintf(
  30. "Could not fetch remote file: %s",
  31. $fileUrl
  32. ));
  33. return;
  34. }
  35. $fileName = 'remote-file-from-http-stream.md';
  36. $zipFile->addFromStream($fp, $fileName);
  37. $zipFile->saveAsFile($outputZip);
  38. $zipFile->close();
  39. $zipFile = new ZipFile();
  40. $zipFile->openFile($outputZip);
  41. $files = $zipFile->getListFiles();
  42. self::assertCount(1, $files);
  43. self::assertSame($fileName, $files[0]);
  44. $zipFile->close();
  45. }
  46. }