1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
|
<?php
declare(strict_types=1);
namespace ZipStreamTest;
use GuzzleHttp\Psr7\Response;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use ReflectionClass;
use ZipArchive;
use ZipStream\File;
use ZipStream\Option\Archive as ArchiveOptions;
use ZipStream\Option\File as FileOptions;
use ZipStream\Option\Method;
use ZipStream\Stream;
use ZipStream\ZipStream;
/**
* Test Class for the Main ZipStream CLass
*/
class ZipStreamTest extends TestCase
{
public const OSX_ARCHIVE_UTILITY =
'/System/Library/CoreServices/Applications/Archive Utility.app/Contents/MacOS/Archive Utility';
public function testFileNotFoundException(): void
{
$this->expectException(\ZipStream\Exception\FileNotFoundException::class);
// Get ZipStream Object
$zip = new ZipStream();
// Trigger error by adding a file which doesn't exist
$zip->addFileFromPath('foobar.php', '/foo/bar/foobar.php');
}
public function testFileNotReadableException(): void
{
// create new virtual filesystem
$root = vfsStream::setup('vfs');
// create a virtual file with no permissions
$file = vfsStream::newFile('foo.txt', 0)->at($root)->setContent('bar');
$zip = new ZipStream();
$this->expectException(\ZipStream\Exception\FileNotReadableException::class);
$zip->addFileFromPath('foo.txt', $file->url());
}
public function testDostime(): void
{
// Allows testing of protected method
$class = new ReflectionClass(File::class);
$method = $class->getMethod('dostime');
$method->setAccessible(true);
$this->assertSame($method->invoke(null, 1416246368), 1165069764);
// January 1 1980 - DOS Epoch.
$this->assertSame($method->invoke(null, 315532800), 2162688);
// January 1 1970 -> January 1 1980 due to minimum DOS Epoch. @todo Throw Exception?
$this->assertSame($method->invoke(null, 0), 2162688);
}
public function testAddFile(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$zip->addFile('sample.txt', 'Sample String Data');
$zip->addFile('test/sample.txt', 'More Simple Sample Data');
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
}
public function testAddFileUtf8NameComment(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$name = 'árvíztűrő tükörfúrógép.txt';
$content = 'Sample String Data';
$comment =
'Filename has every special characters ' .
'from Hungarian language in lowercase. ' .
'In uppercase: ÁÍŰŐÜÖÚÓÉ';
$fileOptions = new FileOptions();
$fileOptions->setComment($comment);
$zip->addFile($name, $content, $fileOptions);
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame([$name], $files);
$this->assertStringEqualsFile($tmpDir . '/' . $name, $content);
$zipArch = new ZipArchive();
$zipArch->open($tmp);
$this->assertSame($comment, $zipArch->getCommentName($name));
}
public function testAddFileUtf8NameNonUtfComment(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$name = 'á.txt';
$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');
$fileOptions = new FileOptions();
$fileOptions->setComment($comment);
$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
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$name = mb_convert_encoding('á.txt', 'ISO-8859-2', 'UTF-8');
$content = 'any';
$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->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));
}
public function testAddFileWithStorageMethod(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());
$zip->addFile('sample.txt', 'Sample String Data', $fileOptions);
$zip->addFile('test/sample.txt', 'More Simple Sample Data');
$zip->finish();
fclose($stream);
$zipArch = new ZipArchive();
$zipArch->open($tmp);
$sample1 = $zipArch->statName('sample.txt');
$sample12 = $zipArch->statName('test/sample.txt');
$this->assertSame($sample1['comp_method'], Method::STORE);
$this->assertSame($sample12['comp_method'], Method::DEFLATE);
$zipArch->close();
}
public function testDecompressFileWithMacUnarchiver(): void
{
if (!file_exists(self::OSX_ARCHIVE_UTILITY)) {
$this->markTestSkipped('The Mac OSX Archive Utility is not available.');
}
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$folder = uniqid('', true);
$zip->addFile($folder . '/sample.txt', 'Sample Data');
$zip->finish();
fclose($stream);
exec(escapeshellarg(self::OSX_ARCHIVE_UTILITY) . ' ' . escapeshellarg($tmp), $output, $returnStatus);
$this->assertSame(0, $returnStatus);
$this->assertCount(0, $output);
$this->assertFileExists(dirname($tmp) . '/' . $folder . '/sample.txt');
$this->assertStringEqualsFile(dirname($tmp) . '/' . $folder . '/sample.txt', 'Sample Data');
}
public function testAddFileFromPath(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
[$tmpExample, $streamExample] = $this->getTmpFileStream();
fwrite($streamExample, 'Sample String Data');
fclose($streamExample);
$zip->addFileFromPath('sample.txt', $tmpExample);
[$tmpExample, $streamExample] = $this->getTmpFileStream();
fwrite($streamExample, 'More Simple Sample Data');
fclose($streamExample);
$zip->addFileFromPath('test/sample.txt', $tmpExample);
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
}
public function testAddFileFromPathWithStorageMethod(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());
[$tmpExample, $streamExample] = $this->getTmpFileStream();
fwrite($streamExample, 'Sample String Data');
fclose($streamExample);
$zip->addFileFromPath('sample.txt', $tmpExample, $fileOptions);
[$tmpExample, $streamExample] = $this->getTmpFileStream();
fwrite($streamExample, 'More Simple Sample Data');
fclose($streamExample);
$zip->addFileFromPath('test/sample.txt', $tmpExample);
$zip->finish();
fclose($stream);
$zipArch = new ZipArchive();
$zipArch->open($tmp);
$sample1 = $zipArch->statName('sample.txt');
$this->assertSame(Method::STORE, $sample1['comp_method']);
$sample2 = $zipArch->statName('test/sample.txt');
$this->assertSame(Method::DEFLATE, $sample2['comp_method']);
$zipArch->close();
}
public function testAddLargeFileFromPath(): void
{
$methods = [Method::DEFLATE(), Method::STORE()];
$falseTrue = [false, true];
foreach ($methods as $method) {
foreach ($falseTrue as $zeroHeader) {
foreach ($falseTrue as $zip64) {
if ($zeroHeader && $method->equals(Method::DEFLATE())) {
continue;
}
$this->addLargeFileFileFromPath($method, $zeroHeader, $zip64);
}
}
}
}
public function testAddFileFromStream(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
// In this test we can't use temporary stream to feed data
// because zlib.deflate filter gives empty string before PHP 7
// it works fine with file stream
$streamExample = fopen(__FILE__, 'rb');
$zip->addFileFromStream('sample.txt', $streamExample);
// fclose($streamExample);
$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());
$streamExample2 = fopen('php://temp', 'wb+');
fwrite($streamExample2, 'More Simple Sample Data');
rewind($streamExample2); // move the pointer back to the beginning of file.
$zip->addFileFromStream('test/sample.txt', $streamExample2, $fileOptions);
// fclose($streamExample2);
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
$this->assertStringEqualsFile(__FILE__, file_get_contents($tmpDir . '/sample.txt'));
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
}
public function testAddFileFromStreamWithStorageMethod(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());
$streamExample = fopen('php://temp', 'wb+');
fwrite($streamExample, 'Sample String Data');
rewind($streamExample); // move the pointer back to the beginning of file.
$zip->addFileFromStream('sample.txt', $streamExample, $fileOptions);
// fclose($streamExample);
$streamExample2 = fopen('php://temp', 'bw+');
fwrite($streamExample2, 'More Simple Sample Data');
rewind($streamExample2); // move the pointer back to the beginning of file.
$zip->addFileFromStream('test/sample.txt', $streamExample2);
// fclose($streamExample2);
$zip->finish();
fclose($stream);
$zipArch = new ZipArchive();
$zipArch->open($tmp);
$sample1 = $zipArch->statName('sample.txt');
$this->assertSame(Method::STORE, $sample1['comp_method']);
$sample2 = $zipArch->statName('test/sample.txt');
$this->assertSame(Method::DEFLATE, $sample2['comp_method']);
$zipArch->close();
}
public function testAddFileFromPsr7Stream(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$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();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$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->assertStringEqualsFile($tmpDir . '/sample.json', $body);
}
public function testAddFileFromPsr7StreamWithFileSizeSet(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$zip = new ZipStream(null, $options);
$body = 'Sample String Data';
$fileSize = strlen($body);
// Add fake padding
$fakePadding = "\0\0\0\0\0\0";
$response = new Response(200, [], $body . $fakePadding);
$fileOptions = new FileOptions();
$fileOptions->setMethod(Method::STORE());
$fileOptions->setSize($fileSize);
$zip->addFileFromPsr7Stream('sample.json', $response->getBody(), $fileOptions);
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.json'], $files);
$this->assertStringEqualsFile($tmpDir . '/sample.json', $body);
}
public function testCreateArchiveWithFlushOptionSet(): void
{
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$options->setFlushOutput(true);
$zip = new ZipStream(null, $options);
$zip->addFile('sample.txt', 'Sample String Data');
$zip->addFile('test/sample.txt', 'More Simple Sample Data');
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$files = $this->getRecursiveFileList($tmpDir);
$this->assertSame(['sample.txt', 'test' . DIRECTORY_SEPARATOR . 'sample.txt'], $files);
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
$this->assertStringEqualsFile($tmpDir . '/test/sample.txt', 'More Simple Sample Data');
}
public function testCreateArchiveWithOutputBufferingOffAndFlushOptionSet(): void
{
// WORKAROUND (1/2): remove phpunit's output buffer in order to run test without any buffering
ob_end_flush();
$this->assertSame(0, ob_get_level());
[$tmp, $stream] = $this->getTmpFileStream();
$options = new ArchiveOptions();
$options->setOutputStream($stream);
$options->setFlushOutput(true);
$zip = new ZipStream(null, $options);
$zip->addFile('sample.txt', 'Sample String Data');
$zip->finish();
fclose($stream);
$tmpDir = $this->validateAndExtractZip($tmp);
$this->assertStringEqualsFile($tmpDir . '/sample.txt', 'Sample String Data');
// WORKAROUND (2/2): add back output buffering so that PHPUnit doesn't complain that it is missing
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}");
}
}
|