2 declare(strict_types=1);
20 const OSX_ARCHIVE_UTILITY =
21 '/System/Library/CoreServices/Applications/Archive Utility.app/Contents/MacOS/Archive Utility';
30 $zip->addFileFromPath(
'foobar.php',
'/foo/bar/foobar.php');
36 $root = vfsStream::setup(
'vfs');
38 $file = vfsStream::newFile(
'foo.txt', 0000)->at(
$root)->setContent(
'bar');
41 $zip->addFileFromPath(
'foo.txt', $file->url());
47 $class = new \ReflectionClass(File::class);
48 $method = $class->getMethod(
'dostime');
49 $method->setAccessible(
true);
51 $this->assertSame($method->invoke(null, 1416246368), 1165069764);
54 $this->assertSame($method->invoke(null, 315532800), 2162688);
57 $this->assertSame($method->invoke(null, 0), 2162688);
62 [$tmp,
$stream] = $this->getTmpFileStream();
69 $zip->addFile(
'sample.txt',
'Sample String Data');
70 $zip->addFile(
'test/sample.txt',
'More Simple Sample Data');
75 $tmpDir = $this->validateAndExtractZip($tmp);
78 $this->assertEquals([
'sample.txt',
'test/sample.txt'],
$files);
80 $this->assertStringEqualsFile(
$tmpDir .
'/sample.txt',
'Sample String Data');
81 $this->assertStringEqualsFile(
$tmpDir .
'/test/sample.txt',
'More Simple Sample Data');
89 $tmp = tempnam(sys_get_temp_dir(),
'zipstreamtest');
103 $zipArch = new \ZipArchive;
104 $res = $zipArch->open($tmp);
107 $this->fail(
"Failed to open {$tmp}. Code: $res");
112 $this->assertEquals(0, $zipArch->status);
113 $this->assertEquals(0, $zipArch->statusSys);
123 $tmp = tempnam(sys_get_temp_dir(),
'zipstreamtest');
125 mkdir($tmp)
or $this->fail(
'Failed to make directory');
137 $path = (string)realpath($path);
140 $pathLen = strlen($path);
141 foreach (
$files as $file) {
142 $filePath = $file->getRealPath();
143 if (!is_dir($filePath)) {
144 $data[] = substr($filePath, $pathLen + 1);
155 [$tmp,
$stream] = $this->getTmpFileStream();
162 $name =
'árvíztűrő tükörfúrógép.txt';
163 $content =
'Sample String Data';
165 'Filename has every special characters ' .
166 'from Hungarian language in lowercase. ' .
167 'In uppercase: ÁÍŰŐÜÖÚÓÉ';
169 $fileOptions =
new FileOptions();
172 $zip->addFile(
$name, $content, $fileOptions);
176 $tmpDir = $this->validateAndExtractZip($tmp);
180 $this->assertStringEqualsFile(
$tmpDir .
'/' .
$name, $content);
182 $zipArch = new \ZipArchive();
183 $zipArch->open($tmp);
184 $this->assertEquals(
$comment, $zipArch->getCommentName($name));
191 $stream = $this->getTmpFileStream()[1];
202 $fileOptions =
new FileOptions();
203 $fileOptions->setComment(mb_convert_encoding(
$comment,
'ISO-8859-2',
'UTF-8'));
205 $zip->addFile(
$name, $content, $fileOptions);
212 $stream = $this->getTmpFileStream()[1];
223 $fileOptions =
new FileOptions();
226 $zip->addFile(mb_convert_encoding(
$name,
'ISO-8859-2',
'UTF-8'), $content, $fileOptions);
231 [$tmp,
$stream] = $this->getTmpFileStream();
238 $fileOptions =
new FileOptions();
241 $zip->addFile(
'sample.txt',
'Sample String Data', $fileOptions);
242 $zip->addFile(
'test/sample.txt',
'More Simple Sample Data');
246 $zipArch = new \ZipArchive();
247 $zipArch->open($tmp);
249 $sample1 = $zipArch->statName(
'sample.txt');
250 $sample12 = $zipArch->statName(
'test/sample.txt');
259 if (!file_exists(self::OSX_ARCHIVE_UTILITY)) {
260 $this->markTestSkipped(
'The Mac OSX Archive Utility is not available.');
263 [$tmp,
$stream] = $this->getTmpFileStream();
270 $folder = uniqid(
'',
true);
272 $zip->addFile($folder .
'/sample.txt',
'Sample Data');
276 exec(escapeshellarg(self::OSX_ARCHIVE_UTILITY) .
' ' . escapeshellarg($tmp),
$output, $returnStatus);
278 $this->assertEquals(0, $returnStatus);
279 $this->assertCount(0,
$output);
281 $this->assertFileExists(dirname($tmp) .
'/' . $folder .
'/sample.txt');
282 $this->assertStringEqualsFile(dirname($tmp) .
'/' . $folder .
'/sample.txt',
'Sample Data');
287 [$tmp,
$stream] = $this->getTmpFileStream();
294 [$tmpExample, $streamExample] = $this->getTmpFileStream();
295 fwrite($streamExample,
'Sample String Data');
296 fclose($streamExample);
297 $zip->addFileFromPath(
'sample.txt', $tmpExample);
299 [$tmpExample, $streamExample] = $this->getTmpFileStream();
300 fwrite($streamExample,
'More Simple Sample Data');
301 fclose($streamExample);
302 $zip->addFileFromPath(
'test/sample.txt', $tmpExample);
307 $tmpDir = $this->validateAndExtractZip($tmp);
310 $this->assertEquals(array(
'sample.txt',
'test/sample.txt'),
$files);
312 $this->assertStringEqualsFile(
$tmpDir .
'/sample.txt',
'Sample String Data');
313 $this->assertStringEqualsFile(
$tmpDir .
'/test/sample.txt',
'More Simple Sample Data');
318 [$tmp,
$stream] = $this->getTmpFileStream();
325 $fileOptions =
new FileOptions();
328 [$tmpExample, $streamExample] = $this->getTmpFileStream();
329 fwrite($streamExample,
'Sample String Data');
330 fclose($streamExample);
331 $zip->addFileFromPath(
'sample.txt', $tmpExample, $fileOptions);
333 [$tmpExample, $streamExample] = $this->getTmpFileStream();
334 fwrite($streamExample,
'More Simple Sample Data');
335 fclose($streamExample);
336 $zip->addFileFromPath(
'test/sample.txt', $tmpExample);
341 $zipArch = new \ZipArchive();
342 $zipArch->open($tmp);
344 $sample1 = $zipArch->statName(
'sample.txt');
347 $sample2 = $zipArch->statName(
'test/sample.txt');
356 $falseTrue = [
false,
true];
357 foreach ($methods as $method) {
358 foreach ($falseTrue as $zeroHeader) {
359 foreach ($falseTrue as $zip64) {
363 $this->addLargeFileFileFromPath($method, $zeroHeader, $zip64);
371 [$tmp,
$stream] = $this->getTmpFileStream();
375 $options->setLargeFileMethod($method);
377 $options->setZeroHeader($zeroHeader);
382 [$tmpExample, $streamExample] = $this->getTmpFileStream();
383 for (
$i = 0;
$i <= 10000;
$i++) {
384 fwrite($streamExample, sha1((
string)
$i));
385 if ($i % 100 === 0) {
386 fwrite($streamExample,
"\n");
389 fclose($streamExample);
390 $shaExample = sha1_file($tmpExample);
391 $zip->addFileFromPath(
'sample.txt', $tmpExample);
397 $tmpDir = $this->validateAndExtractZip($tmp);
400 $this->assertEquals(array(
'sample.txt'),
$files);
402 $this->assertEquals(sha1_file(
$tmpDir .
'/sample.txt'), $shaExample,
"SHA-1 Mismatch Method: {$method}");
407 [$tmp,
$stream] = $this->getTmpFileStream();
417 $streamExample =
fopen(__FILE__,
'rb');
418 $zip->addFileFromStream(
'sample.txt', $streamExample);
421 $fileOptions =
new FileOptions();
424 $streamExample2 =
fopen(
'php://temp',
'wb+');
425 fwrite($streamExample2,
'More Simple Sample Data');
426 rewind($streamExample2);
427 $zip->addFileFromStream(
'test/sample.txt', $streamExample2, $fileOptions);
433 $tmpDir = $this->validateAndExtractZip($tmp);
436 $this->assertEquals(array(
'sample.txt',
'test/sample.txt'),
$files);
438 $this->assertStringEqualsFile(__FILE__, file_get_contents(
$tmpDir .
'/sample.txt'));
439 $this->assertStringEqualsFile(
$tmpDir .
'/test/sample.txt',
'More Simple Sample Data');
444 [$tmp,
$stream] = $this->getTmpFileStream();
451 $fileOptions =
new FileOptions();
454 $streamExample =
fopen(
'php://temp',
'wb+');
455 fwrite($streamExample,
'Sample String Data');
456 rewind($streamExample);
457 $zip->addFileFromStream(
'sample.txt', $streamExample, $fileOptions);
460 $streamExample2 =
fopen(
'php://temp',
'bw+');
461 fwrite($streamExample2,
'More Simple Sample Data');
462 rewind($streamExample2);
463 $zip->addFileFromStream(
'test/sample.txt', $streamExample2);
469 $zipArch = new \ZipArchive();
470 $zipArch->open($tmp);
472 $sample1 = $zipArch->statName(
'sample.txt');
475 $sample2 = $zipArch->statName(
'test/sample.txt');
483 [$tmp,
$stream] = $this->getTmpFileStream();
490 $body =
'Sample String Data';
493 $fileOptions =
new FileOptions();
496 $zip->addFileFromPsr7Stream(
'sample.json',
$response->getBody(), $fileOptions);
500 $tmpDir = $this->validateAndExtractZip($tmp);
503 $this->assertEquals(array(
'sample.json'),
$files);
504 $this->assertStringEqualsFile(
$tmpDir .
'/sample.json', $body);
509 [$tmp,
$stream] = $this->getTmpFileStream();
516 $body =
'Sample String Data';
517 $fileSize = strlen($body);
519 $fakePadding =
"\0\0\0\0\0\0";
522 $fileOptions =
new FileOptions();
524 $fileOptions->setSize($fileSize);
525 $zip->addFileFromPsr7Stream(
'sample.json',
$response->getBody(), $fileOptions);
529 $tmpDir = $this->validateAndExtractZip($tmp);
532 $this->assertEquals(array(
'sample.json'),
$files);
533 $this->assertStringEqualsFile(
$tmpDir .
'/sample.json', $body);
538 [$tmp,
$stream] = $this->getTmpFileStream();
546 $zip->addFile(
'sample.txt',
'Sample String Data');
547 $zip->addFile(
'test/sample.txt',
'More Simple Sample Data');
552 $tmpDir = $this->validateAndExtractZip($tmp);
555 $this->assertEquals([
'sample.txt',
'test/sample.txt'],
$files);
557 $this->assertStringEqualsFile(
$tmpDir .
'/sample.txt',
'Sample String Data');
558 $this->assertStringEqualsFile(
$tmpDir .
'/test/sample.txt',
'More Simple Sample Data');
565 $this->assertEquals(0, ob_get_level());
567 [$tmp,
$stream] = $this->getTmpFileStream();
575 $zip->addFile(
'sample.txt',
'Sample String Data');
580 $tmpDir = $this->validateAndExtractZip($tmp);
581 $this->assertStringEqualsFile(
$tmpDir .
'/sample.txt',
'Sample String Data');
testAddFileNonUtf8NameUtfComment()
testAddLargeFileFromPath()
testAddFileFromPsr7StreamWithFileSizeSet()
validateAndExtractZip($tmp)
testAddFileFromStreamWithStorageMethod()
testFileNotReadableException()
testDecompressFileWithMacUnarchiver()
addLargeFileFileFromPath($method, $zeroHeader, $zip64)
$stream
PHP stream implementation.
testAddFileUtf8NameNonUtfComment()
testAddFileWithStorageMethod()
getRecursiveFileList(string $path)
testAddFileUtf8NameComment()
foreach($_POST as $key=> $value) $res
testAddFileFromPathWithStorageMethod()
testAddFileFromPsr7Stream()
testCreateArchiveWithOutputBufferingOffAndFlushOptionSet()
testCreateArchiveWithFlushOptionSet()
testFileNotFoundException()
PSR-7 response implementation.