Skip to content

Commit

Permalink
Re-enable support for remote streams, fix Ne-Lexa#25
Browse files Browse the repository at this point in the history
  • Loading branch information
belgattitude committed Nov 2, 2018
1 parent 9934a86 commit e903642
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/PhpZip/Model/Entry/ZipNewEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ public function __construct($content = null)
public function getEntryContent()
{
if (is_resource($this->content)) {
rewind($this->content);
if (stream_get_meta_data($this->content)['seekable']) {
rewind($this->content);
}
return stream_get_contents($this->content);
}
return $this->content;
Expand Down
55 changes: 55 additions & 0 deletions tests/PhpZip/ZipRemoteFileTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace PhpZip;

use PhpZip\Exception\ZipException;
use PhpZip\Util\Iterator\IgnoreFilesFilterIterator;
use PhpZip\Util\Iterator\IgnoreFilesRecursiveFilterIterator;

/**
* Test add remote files to zip archive
*/
class ZipRemoteFileTest extends ZipTestCase
{

protected function setUp()
{
parent::setUp();
}

/**
* @throws ZipException
*/
public function testAddRemoteFileFromStream()
{
$zipFile = new ZipFile();
$outputZip = $this->outputFilename;
$fileUrl = 'https://raw.githubusercontent.com/Ne-Lexa/php-zip/master/README.md';
$fp = @fopen($fileUrl, 'rb', false, stream_context_create([
'http' => [
'timeout' => 3,
]
]));
if ($fp === false) {
self::markTestSkipped(sprintf(
"Could not fetch remote file: %s",
$fileUrl
));
return;
}

$fileName = 'remote-file-from-http-stream.md';
$zipFile->addFromStream($fp, $fileName);

$zipFile->saveAsFile($outputZip);
$zipFile->close();

$zipFile = new ZipFile();
$zipFile->openFile($outputZip);
$files = $zipFile->getListFiles();
self::assertCount(1, $files);
self::assertSame($fileName, $files[0]);
$zipFile->close();
}

}

0 comments on commit e903642

Please sign in to comment.