|
@@ -1,21 +1,15 @@
|
|
|
<?php
|
|
<?php
|
|
|
-
|
|
|
|
|
declare(strict_types=1);
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace ZipStreamTest;
|
|
namespace ZipStreamTest;
|
|
|
|
|
|
|
|
-use GuzzleHttp\Psr7\Response;
|
|
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
|
+use GuzzleHttp\Psr7\Response;
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
-use RecursiveDirectoryIterator;
|
|
|
|
|
-use RecursiveIteratorIterator;
|
|
|
|
|
-use ReflectionClass;
|
|
|
|
|
-use ZipArchive;
|
|
|
|
|
use ZipStream\File;
|
|
use ZipStream\File;
|
|
|
use ZipStream\Option\Archive as ArchiveOptions;
|
|
use ZipStream\Option\Archive as ArchiveOptions;
|
|
|
use ZipStream\Option\File as FileOptions;
|
|
use ZipStream\Option\File as FileOptions;
|
|
|
use ZipStream\Option\Method;
|
|
use ZipStream\Option\Method;
|
|
|
-use ZipStream\Stream;
|
|
|
|
|
use ZipStream\ZipStream;
|
|
use ZipStream\ZipStream;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -23,7 +17,7 @@ use ZipStream\ZipStream;
|
|
|
*/
|
|
*/
|
|
|
class ZipStreamTest extends TestCase
|
|
class ZipStreamTest extends TestCase
|
|
|
{
|
|
{
|
|
|
- public const OSX_ARCHIVE_UTILITY =
|
|
|
|
|
|
|
+ const OSX_ARCHIVE_UTILITY =
|
|
|
'/System/Library/CoreServices/Applications/Archive Utility.app/Contents/MacOS/Archive Utility';
|
|
'/System/Library/CoreServices/Applications/Archive Utility.app/Contents/MacOS/Archive Utility';
|
|
|
|
|
|
|
|
public function testFileNotFoundException(): void
|
|
public function testFileNotFoundException(): void
|
|
@@ -41,7 +35,7 @@ class ZipStreamTest extends TestCase
|
|
|
// create new virtual filesystem
|
|
// create new virtual filesystem
|
|
|
$root = vfsStream::setup('vfs');
|
|
$root = vfsStream::setup('vfs');
|
|
|
// create a virtual file with no permissions
|
|
// create a virtual file with no permissions
|
|
|
- $file = vfsStream::newFile('foo.txt', 0)->at($root)->setContent('bar');
|
|
|
|
|
|
|
+ $file = vfsStream::newFile('foo.txt', 0000)->at($root)->setContent('bar');
|
|
|
$zip = new ZipStream();
|
|
$zip = new ZipStream();
|
|
|
$this->expectException(\ZipStream\Exception\FileNotReadableException::class);
|
|
$this->expectException(\ZipStream\Exception\FileNotReadableException::class);
|
|
|
$zip->addFileFromPath('foo.txt', $file->url());
|
|
$zip->addFileFromPath('foo.txt', $file->url());
|
|
@@ -50,7 +44,7 @@ class ZipStreamTest extends TestCase
|
|
|
public function testDostime(): void
|
|
public function testDostime(): void
|
|
|
{
|
|
{
|
|
|
// Allows testing of protected method
|
|
// Allows testing of protected method
|
|
|
- $class = new ReflectionClass(File::class);
|
|
|
|
|
|
|
+ $class = new \ReflectionClass(File::class);
|
|
|
$method = $class->getMethod('dostime');
|
|
$method = $class->getMethod('dostime');
|
|
|
$method->setAccessible(true);
|
|
$method->setAccessible(true);
|
|
|
|
|
|
|
@@ -81,12 +75,81 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(['sample.txt', 'test/sample.txt'], $files);
|
|
|
|
|
|
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @return array
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function getTmpFileStream(): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
|
|
|
|
|
+ $stream = fopen($tmp, 'wb+');
|
|
|
|
|
+
|
|
|
|
|
+ return array($tmp, $stream);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param string $tmp
|
|
|
|
|
+ * @return string
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function validateAndExtractZip($tmp): string
|
|
|
|
|
+ {
|
|
|
|
|
+ $tmpDir = $this->getTmpDir();
|
|
|
|
|
+
|
|
|
|
|
+ $zipArch = new \ZipArchive;
|
|
|
|
|
+ $res = $zipArch->open($tmp);
|
|
|
|
|
+
|
|
|
|
|
+ if ($res !== true) {
|
|
|
|
|
+ $this->fail("Failed to open {$tmp}. Code: $res");
|
|
|
|
|
+
|
|
|
|
|
+ return $tmpDir;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertEquals(0, $zipArch->status);
|
|
|
|
|
+ $this->assertEquals(0, $zipArch->statusSys);
|
|
|
|
|
+
|
|
|
|
|
+ $zipArch->extractTo($tmpDir);
|
|
|
|
|
+ $zipArch->close();
|
|
|
|
|
+
|
|
|
|
|
+ return $tmpDir;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ protected function getTmpDir(): string
|
|
|
|
|
+ {
|
|
|
|
|
+ $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
|
|
|
|
|
+ unlink($tmp);
|
|
|
|
|
+ mkdir($tmp) or $this->fail('Failed to make directory');
|
|
|
|
|
+
|
|
|
|
|
+ return $tmp;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @param string $path
|
|
|
|
|
+ * @return string[]
|
|
|
|
|
+ */
|
|
|
|
|
+ protected function getRecursiveFileList(string $path): array
|
|
|
|
|
+ {
|
|
|
|
|
+ $data = array();
|
|
|
|
|
+ $path = (string)realpath($path);
|
|
|
|
|
+ $files = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
|
|
|
|
|
+
|
|
|
|
|
+ $pathLen = strlen($path);
|
|
|
|
|
+ foreach ($files as $file) {
|
|
|
|
|
+ $filePath = $file->getRealPath();
|
|
|
|
|
+ if (!is_dir($filePath)) {
|
|
|
|
|
+ $data[] = substr($filePath, $pathLen + 1);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ sort($data);
|
|
|
|
|
+
|
|
|
|
|
+ return $data;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function testAddFileUtf8NameComment(): void
|
|
public function testAddFileUtf8NameComment(): void
|
|
|
{
|
|
{
|
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
@@ -113,17 +176,19 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame([$name], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(array($name), $files);
|
|
|
$this->assertStringEqualsFile($tmpDir . '/' . $name, $content);
|
|
$this->assertStringEqualsFile($tmpDir . '/' . $name, $content);
|
|
|
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
|
|
+ $zipArch = new \ZipArchive();
|
|
|
$zipArch->open($tmp);
|
|
$zipArch->open($tmp);
|
|
|
- $this->assertSame($comment, $zipArch->getCommentName($name));
|
|
|
|
|
|
|
+ $this->assertEquals($comment, $zipArch->getCommentName($name));
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function testAddFileUtf8NameNonUtfComment(): void
|
|
public function testAddFileUtf8NameNonUtfComment(): void
|
|
|
{
|
|
{
|
|
|
- [$tmp, $stream] = $this->getTmpFileStream();
|
|
|
|
|
|
|
+ $this->expectException(\ZipStream\Exception\EncodingException::class);
|
|
|
|
|
+
|
|
|
|
|
+ $stream = $this->getTmpFileStream()[1];
|
|
|
|
|
|
|
|
$options = new ArchiveOptions();
|
|
$options = new ArchiveOptions();
|
|
|
$options->setOutputStream($stream);
|
|
$options->setOutputStream($stream);
|
|
@@ -132,66 +197,33 @@ class ZipStreamTest extends TestCase
|
|
|
|
|
|
|
|
$name = 'á.txt';
|
|
$name = 'á.txt';
|
|
|
$content = 'any';
|
|
$content = 'any';
|
|
|
- $comment = mb_convert_encoding('á', 'ISO-8859-2', 'UTF-8');
|
|
|
|
|
-
|
|
|
|
|
- // @see https://libzip.org/documentation/zip_file_get_comment.html
|
|
|
|
|
- //
|
|
|
|
|
- // mb_convert_encoding hasn't CP437.
|
|
|
|
|
- // nearly CP850 (DOS-Latin-1)
|
|
|
|
|
- $guessComment = mb_convert_encoding($comment, 'UTF-8', 'CP850');
|
|
|
|
|
|
|
+ $comment = 'á';
|
|
|
|
|
|
|
|
$fileOptions = new FileOptions();
|
|
$fileOptions = new FileOptions();
|
|
|
- $fileOptions->setComment($comment);
|
|
|
|
|
|
|
+ $fileOptions->setComment(mb_convert_encoding($comment, 'ISO-8859-2', 'UTF-8'));
|
|
|
|
|
|
|
|
$zip->addFile($name, $content, $fileOptions);
|
|
$zip->addFile($name, $content, $fileOptions);
|
|
|
- $zip->finish();
|
|
|
|
|
- fclose($stream);
|
|
|
|
|
-
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
- $zipArch->open($tmp);
|
|
|
|
|
- $this->assertSame($guessComment, $zipArch->getCommentName($name));
|
|
|
|
|
- $this->assertSame($comment, $zipArch->getCommentName($name, ZipArchive::FL_ENC_RAW));
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function testAddFileNonUtf8NameUtfComment(): void
|
|
public function testAddFileNonUtf8NameUtfComment(): void
|
|
|
{
|
|
{
|
|
|
- [$tmp, $stream] = $this->getTmpFileStream();
|
|
|
|
|
|
|
+ $this->expectException(\ZipStream\Exception\EncodingException::class);
|
|
|
|
|
+
|
|
|
|
|
+ $stream = $this->getTmpFileStream()[1];
|
|
|
|
|
|
|
|
$options = new ArchiveOptions();
|
|
$options = new ArchiveOptions();
|
|
|
$options->setOutputStream($stream);
|
|
$options->setOutputStream($stream);
|
|
|
|
|
|
|
|
$zip = new ZipStream(null, $options);
|
|
$zip = new ZipStream(null, $options);
|
|
|
|
|
|
|
|
- $name = mb_convert_encoding('á.txt', 'ISO-8859-2', 'UTF-8');
|
|
|
|
|
|
|
+ $name = 'á.txt';
|
|
|
$content = 'any';
|
|
$content = 'any';
|
|
|
$comment = 'á';
|
|
$comment = 'á';
|
|
|
|
|
|
|
|
- // @see https://libzip.org/documentation/zip_get_name.html
|
|
|
|
|
- //
|
|
|
|
|
- // mb_convert_encoding hasn't CP437.
|
|
|
|
|
- // nearly CP850 (DOS-Latin-1)
|
|
|
|
|
- $guessName = mb_convert_encoding($name, 'UTF-8', 'CP850');
|
|
|
|
|
-
|
|
|
|
|
$fileOptions = new FileOptions();
|
|
$fileOptions = new FileOptions();
|
|
|
$fileOptions->setComment($comment);
|
|
$fileOptions->setComment($comment);
|
|
|
|
|
|
|
|
- $zip->addFile($name, $content, $fileOptions);
|
|
|
|
|
- $zip->finish();
|
|
|
|
|
- fclose($stream);
|
|
|
|
|
-
|
|
|
|
|
- $tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
-
|
|
|
|
|
- $files = $this->getRecursiveFileList($tmpDir);
|
|
|
|
|
-
|
|
|
|
|
- $this->assertNotSame([$name], $files);
|
|
|
|
|
- $this->assertSame([$guessName], $files);
|
|
|
|
|
- $this->assertStringEqualsFile($tmpDir . '/' . $guessName, $content);
|
|
|
|
|
-
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
- $zipArch->open($tmp);
|
|
|
|
|
- $this->assertSame($guessName, $zipArch->getNameIndex(0));
|
|
|
|
|
- $this->assertSame($name, $zipArch->getNameIndex(0, ZipArchive::FL_ENC_RAW));
|
|
|
|
|
- $this->assertSame($comment, $zipArch->getCommentName($guessName));
|
|
|
|
|
|
|
+ $zip->addFile(mb_convert_encoding($name, 'ISO-8859-2', 'UTF-8'), $content, $fileOptions);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
public function testAddFileWithStorageMethod(): void
|
|
public function testAddFileWithStorageMethod(): void
|
|
@@ -211,13 +243,13 @@ class ZipStreamTest extends TestCase
|
|
|
$zip->finish();
|
|
$zip->finish();
|
|
|
fclose($stream);
|
|
fclose($stream);
|
|
|
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
|
|
+ $zipArch = new \ZipArchive();
|
|
|
$zipArch->open($tmp);
|
|
$zipArch->open($tmp);
|
|
|
|
|
|
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
|
$sample12 = $zipArch->statName('test/sample.txt');
|
|
$sample12 = $zipArch->statName('test/sample.txt');
|
|
|
- $this->assertSame($sample1['comp_method'], Method::STORE);
|
|
|
|
|
- $this->assertSame($sample12['comp_method'], Method::DEFLATE);
|
|
|
|
|
|
|
+ $this->assertEquals($sample1['comp_method'], Method::STORE);
|
|
|
|
|
+ $this->assertEquals($sample12['comp_method'], Method::DEFLATE);
|
|
|
|
|
|
|
|
$zipArch->close();
|
|
$zipArch->close();
|
|
|
}
|
|
}
|
|
@@ -243,7 +275,7 @@ class ZipStreamTest extends TestCase
|
|
|
|
|
|
|
|
exec(escapeshellarg(self::OSX_ARCHIVE_UTILITY) . ' ' . escapeshellarg($tmp), $output, $returnStatus);
|
|
exec(escapeshellarg(self::OSX_ARCHIVE_UTILITY) . ' ' . escapeshellarg($tmp), $output, $returnStatus);
|
|
|
|
|
|
|
|
- $this->assertSame(0, $returnStatus);
|
|
|
|
|
|
|
+ $this->assertEquals(0, $returnStatus);
|
|
|
$this->assertCount(0, $output);
|
|
$this->assertCount(0, $output);
|
|
|
|
|
|
|
|
$this->assertFileExists(dirname($tmp) . '/' . $folder . '/sample.txt');
|
|
$this->assertFileExists(dirname($tmp) . '/' . $folder . '/sample.txt');
|
|
@@ -275,7 +307,7 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(array('sample.txt', 'test/sample.txt'), $files);
|
|
|
|
|
|
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
@@ -306,14 +338,14 @@ class ZipStreamTest extends TestCase
|
|
|
$zip->finish();
|
|
$zip->finish();
|
|
|
fclose($stream);
|
|
fclose($stream);
|
|
|
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
|
|
+ $zipArch = new \ZipArchive();
|
|
|
$zipArch->open($tmp);
|
|
$zipArch->open($tmp);
|
|
|
|
|
|
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
|
- $this->assertSame(Method::STORE, $sample1['comp_method']);
|
|
|
|
|
|
|
+ $this->assertEquals(Method::STORE, $sample1['comp_method']);
|
|
|
|
|
|
|
|
$sample2 = $zipArch->statName('test/sample.txt');
|
|
$sample2 = $zipArch->statName('test/sample.txt');
|
|
|
- $this->assertSame(Method::DEFLATE, $sample2['comp_method']);
|
|
|
|
|
|
|
+ $this->assertEquals(Method::DEFLATE, $sample2['comp_method']);
|
|
|
|
|
|
|
|
$zipArch->close();
|
|
$zipArch->close();
|
|
|
}
|
|
}
|
|
@@ -334,6 +366,42 @@ class ZipStreamTest extends TestCase
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ protected function addLargeFileFileFromPath($method, $zeroHeader, $zip64): void
|
|
|
|
|
+ {
|
|
|
|
|
+ [$tmp, $stream] = $this->getTmpFileStream();
|
|
|
|
|
+
|
|
|
|
|
+ $options = new ArchiveOptions();
|
|
|
|
|
+ $options->setOutputStream($stream);
|
|
|
|
|
+ $options->setLargeFileMethod($method);
|
|
|
|
|
+ $options->setLargeFileSize(5);
|
|
|
|
|
+ $options->setZeroHeader($zeroHeader);
|
|
|
|
|
+ $options->setEnableZip64($zip64);
|
|
|
|
|
+
|
|
|
|
|
+ $zip = new ZipStream(null, $options);
|
|
|
|
|
+
|
|
|
|
|
+ [$tmpExample, $streamExample] = $this->getTmpFileStream();
|
|
|
|
|
+ for ($i = 0; $i <= 10000; $i++) {
|
|
|
|
|
+ fwrite($streamExample, sha1((string)$i));
|
|
|
|
|
+ if ($i % 100 === 0) {
|
|
|
|
|
+ fwrite($streamExample, "\n");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ fclose($streamExample);
|
|
|
|
|
+ $shaExample = sha1_file($tmpExample);
|
|
|
|
|
+ $zip->addFileFromPath('sample.txt', $tmpExample);
|
|
|
|
|
+ unlink($tmpExample);
|
|
|
|
|
+
|
|
|
|
|
+ $zip->finish();
|
|
|
|
|
+ fclose($stream);
|
|
|
|
|
+
|
|
|
|
|
+ $tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
+
|
|
|
|
|
+ $files = $this->getRecursiveFileList($tmpDir);
|
|
|
|
|
+ $this->assertEquals(array('sample.txt'), $files);
|
|
|
|
|
+
|
|
|
|
|
+ $this->assertEquals(sha1_file($tmpDir . '/sample.txt'), $shaExample, "SHA-1 Mismatch Method: {$method}");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public function testAddFileFromStream(): void
|
|
public function testAddFileFromStream(): void
|
|
|
{
|
|
{
|
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
@@ -365,7 +433,7 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(array('sample.txt', 'test/sample.txt'), $files);
|
|
|
|
|
|
|
|
$this->assertStringEqualsFile(__FILE__, file_get_contents($tmpDir . '/sample.txt'));
|
|
$this->assertStringEqualsFile(__FILE__, file_get_contents($tmpDir . '/sample.txt'));
|
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
@@ -398,14 +466,14 @@ class ZipStreamTest extends TestCase
|
|
|
$zip->finish();
|
|
$zip->finish();
|
|
|
fclose($stream);
|
|
fclose($stream);
|
|
|
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
|
|
+ $zipArch = new \ZipArchive();
|
|
|
$zipArch->open($tmp);
|
|
$zipArch->open($tmp);
|
|
|
|
|
|
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
$sample1 = $zipArch->statName('sample.txt');
|
|
|
- $this->assertSame(Method::STORE, $sample1['comp_method']);
|
|
|
|
|
|
|
+ $this->assertEquals(Method::STORE, $sample1['comp_method']);
|
|
|
|
|
|
|
|
$sample2 = $zipArch->statName('test/sample.txt');
|
|
$sample2 = $zipArch->statName('test/sample.txt');
|
|
|
- $this->assertSame(Method::DEFLATE, $sample2['comp_method']);
|
|
|
|
|
|
|
+ $this->assertEquals(Method::DEFLATE, $sample2['comp_method']);
|
|
|
|
|
|
|
|
$zipArch->close();
|
|
$zipArch->close();
|
|
|
}
|
|
}
|
|
@@ -432,34 +500,7 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.json'], $files);
|
|
|
|
|
- $this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- public function testAddFileFromPsr7StreamWithOutputToPsr7Stream(): void
|
|
|
|
|
- {
|
|
|
|
|
- [$tmp, $resource] = $this->getTmpFileStream();
|
|
|
|
|
- $psr7OutputStream = new Stream($resource);
|
|
|
|
|
-
|
|
|
|
|
- $options = new ArchiveOptions();
|
|
|
|
|
- $options->setOutputStream($psr7OutputStream);
|
|
|
|
|
-
|
|
|
|
|
- $zip = new ZipStream(null, $options);
|
|
|
|
|
-
|
|
|
|
|
- $body = 'Sample String Data';
|
|
|
|
|
- $response = new Response(200, [], $body);
|
|
|
|
|
-
|
|
|
|
|
- $fileOptions = new FileOptions();
|
|
|
|
|
- $fileOptions->setMethod(Method::STORE());
|
|
|
|
|
-
|
|
|
|
|
- $zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
|
|
|
|
|
- $zip->finish();
|
|
|
|
|
- $psr7OutputStream->close();
|
|
|
|
|
-
|
|
|
|
|
- $tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
- $files = $this->getRecursiveFileList($tmpDir);
|
|
|
|
|
-
|
|
|
|
|
- $this->assertSame(['sample.json'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(array('sample.json'), $files);
|
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -488,7 +529,7 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.json'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(array('sample.json'), $files);
|
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -511,7 +552,7 @@ class ZipStreamTest extends TestCase
|
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
$tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
|
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
$files = $this->getRecursiveFileList($tmpDir);
|
|
|
- $this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
|
|
|
|
|
|
|
+ $this->assertEquals(['sample.txt', 'test/sample.txt'], $files);
|
|
|
|
|
|
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
|
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
|
|
@@ -521,7 +562,7 @@ class ZipStreamTest extends TestCase
|
|
|
{
|
|
{
|
|
|
// WORKAROUND (1/2): remove phpunit's output buffer in order to run test without any buffering
|
|
// WORKAROUND (1/2): remove phpunit's output buffer in order to run test without any buffering
|
|
|
ob_end_flush();
|
|
ob_end_flush();
|
|
|
- $this->assertSame(0, ob_get_level());
|
|
|
|
|
|
|
+ $this->assertEquals(0, ob_get_level());
|
|
|
|
|
|
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
[$tmp, $stream] = $this->getTmpFileStream();
|
|
|
|
|
|
|
@@ -542,109 +583,4 @@ class ZipStreamTest extends TestCase
|
|
|
// WORKAROUND (2/2): add back output buffering so that PHPUnit doesn't complain that it is missing
|
|
// WORKAROUND (2/2): add back output buffering so that PHPUnit doesn't complain that it is missing
|
|
|
ob_start();
|
|
ob_start();
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @return array
|
|
|
|
|
- */
|
|
|
|
|
- protected function getTmpFileStream(): array
|
|
|
|
|
- {
|
|
|
|
|
- $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
|
|
|
|
|
- $stream = fopen($tmp, 'wb+');
|
|
|
|
|
-
|
|
|
|
|
- return [$tmp, $stream];
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @param string $tmp
|
|
|
|
|
- * @return string
|
|
|
|
|
- */
|
|
|
|
|
- protected function validateAndExtractZip($tmp): string
|
|
|
|
|
- {
|
|
|
|
|
- $tmpDir = $this->getTmpDir();
|
|
|
|
|
-
|
|
|
|
|
- $zipArch = new ZipArchive();
|
|
|
|
|
- $res = $zipArch->open($tmp);
|
|
|
|
|
-
|
|
|
|
|
- if ($res !== true) {
|
|
|
|
|
- $this->fail("Failed to open {$tmp}. Code: $res");
|
|
|
|
|
-
|
|
|
|
|
- return $tmpDir;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- $this->assertSame(0, $zipArch->status);
|
|
|
|
|
- $this->assertSame(0, $zipArch->statusSys);
|
|
|
|
|
-
|
|
|
|
|
- $zipArch->extractTo($tmpDir);
|
|
|
|
|
- $zipArch->close();
|
|
|
|
|
-
|
|
|
|
|
- return $tmpDir;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected function getTmpDir(): string
|
|
|
|
|
- {
|
|
|
|
|
- $tmp = tempnam(sys_get_temp_dir(), 'zipstreamtest');
|
|
|
|
|
- unlink($tmp);
|
|
|
|
|
- mkdir($tmp) or $this->fail('Failed to make directory');
|
|
|
|
|
-
|
|
|
|
|
- return $tmp;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- /**
|
|
|
|
|
- * @param string $path
|
|
|
|
|
- * @return string[]
|
|
|
|
|
- */
|
|
|
|
|
- protected function getRecursiveFileList(string $path): array
|
|
|
|
|
- {
|
|
|
|
|
- $data = [];
|
|
|
|
|
- $path = (string)realpath($path);
|
|
|
|
|
- $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
|
|
|
|
|
-
|
|
|
|
|
- $pathLen = strlen($path);
|
|
|
|
|
- foreach ($files as $file) {
|
|
|
|
|
- $filePath = $file->getRealPath();
|
|
|
|
|
- if (!is_dir($filePath)) {
|
|
|
|
|
- $data[] = substr($filePath, $pathLen + 1);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- sort($data);
|
|
|
|
|
-
|
|
|
|
|
- return $data;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- protected function addLargeFileFileFromPath($method, $zeroHeader, $zip64): void
|
|
|
|
|
- {
|
|
|
|
|
- [$tmp, $stream] = $this->getTmpFileStream();
|
|
|
|
|
-
|
|
|
|
|
- $options = new ArchiveOptions();
|
|
|
|
|
- $options->setOutputStream($stream);
|
|
|
|
|
- $options->setLargeFileMethod($method);
|
|
|
|
|
- $options->setLargeFileSize(5);
|
|
|
|
|
- $options->setZeroHeader($zeroHeader);
|
|
|
|
|
- $options->setEnableZip64($zip64);
|
|
|
|
|
-
|
|
|
|
|
- $zip = new ZipStream(null, $options);
|
|
|
|
|
-
|
|
|
|
|
- [$tmpExample, $streamExample] = $this->getTmpFileStream();
|
|
|
|
|
- for ($i = 0; $i <= 10000; $i++) {
|
|
|
|
|
- fwrite($streamExample, sha1((string)$i));
|
|
|
|
|
- if ($i % 100 === 0) {
|
|
|
|
|
- fwrite($streamExample, "\n");
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- fclose($streamExample);
|
|
|
|
|
- $shaExample = sha1_file($tmpExample);
|
|
|
|
|
- $zip->addFileFromPath('sample.txt', $tmpExample);
|
|
|
|
|
- unlink($tmpExample);
|
|
|
|
|
-
|
|
|
|
|
- $zip->finish();
|
|
|
|
|
- fclose($stream);
|
|
|
|
|
-
|
|
|
|
|
- $tmpDir = $this->validateAndExtractZip($tmp);
|
|
|
|
|
-
|
|
|
|
|
- $files = $this->getRecursiveFileList($tmpDir);
|
|
|
|
|
- $this->assertSame(['sample.txt'], $files);
|
|
|
|
|
-
|
|
|
|
|
- $this->assertSame(sha1_file($tmpDir . '/sample.txt'), $shaExample, "SHA-1 Mismatch Method: {$method}");
|
|
|
|
|
- }
|
|
|
|
|
}
|
|
}
|