Skip to content

Commit

Permalink
Test streamWrapper extract (issue #24)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ne-Lexa committed Oct 20, 2018
1 parent d8bb1be commit e4e3a75
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/PhpZip/Stream/ZipInputStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,14 @@ public function readEntryContent(ZipEntry $entry)
throw new ZipUnsupportMethodException($entry->getName() .
" (compression method " . $method . " is not supported)");
}

if ($content === false) {
throw new ZipException(sprintf(
'Failed to get the contents of the zip entry "%s"',
$entry->getName()
));
}

if (!$skipCheckCrc) {
$localCrc = crc32($content);
$localCrc = PHP_INT_SIZE === 4 ? sprintf('%u', $localCrc) : $localCrc;
Expand Down
104 changes: 104 additions & 0 deletions tests/PhpZip/Issue24Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

namespace PhpZip;

use PhpZip\Exception\ZipException;
use PhpZip\Util\CryptoUtil;

class Issue24Test extends ZipTestCase
{
/**
* This method is called before the first test of this test class is run.
*/
public static function setUpBeforeClass()
{
stream_wrapper_register("dummyfs", DummyFileSystemStream::class);
}

/**
* @throws ZipException
*/
public function testDummyFS()
{
$fileContents = str_repeat(base64_encode(CryptoUtil::randomBytes(12000)), 100);

// create zip file
$zip = new ZipFile();
$zip->addFromString(
'file.txt',
$fileContents,
ZipFile::METHOD_DEFLATED
);
$zip->saveAsFile($this->outputFilename);
$zip->close();

$this->assertCorrectZipArchive($this->outputFilename);

$stream = fopen('dummyfs://localhost/' . $this->outputFilename, 'rb');
$this->assertNotFalse($stream);
$zip->openFromStream($stream);
$this->assertEquals($zip->getListFiles(), ['file.txt']);
$this->assertEquals($zip['file.txt'], $fileContents);
$zip->close();
}
}

/**
* Try to load using dummy stream
*/
class DummyFileSystemStream
{
/**
* @var resource
*/
private $fp;

function stream_open($path, $mode, $options, &$opened_path)
{
// echo "DummyFileSystemStream->stream_open($path, $mode, $options)" . PHP_EOL;

$parsedUrl = parse_url($path);
$path = $parsedUrl['path'];
$this->fp = fopen($path, $mode);

return true;
}

function stream_read($count)
{
// echo "DummyFileSystemStream->stream_read($count)" . PHP_EOL;
$position = ftell($this->fp);

// echo "Loading chunk " . $position . " to " . ($position + $count - 1) . PHP_EOL;
$ret = fread($this->fp, $count);

// echo "String length: " . strlen($ret) . PHP_EOL;

return $ret;
}

function stream_tell()
{
// echo "DummyFileSystemStream->stream_tell()" . PHP_EOL;
return ftell($this->fp);
}

function stream_eof()
{
// echo "DummyFileSystemStream->stream_eof()" . PHP_EOL;
$isfeof = feof($this->fp);
return $isfeof;
}

function stream_seek($offset, $whence)
{
// echo "DummyFileSystemStream->stream_seek($offset, $whence)" . PHP_EOL;
fseek($this->fp, $offset, $whence);
}

function stream_stat()
{
// echo "DummyFileSystemStream->stream_stat()" . PHP_EOL;
return fstat($this->fp);
}
}

0 comments on commit e4e3a75

Please sign in to comment.