Skip to content

Commit af1398d

Browse files
GromNaNFayez Naccache
and
Fayez Naccache
authored
Expose gzipped file contents (#4)
* Fetch stream from S3 and local inputs * Expose compressed contents as string instead of resource stream Co-authored-by: Fayez Naccache <[email protected]>
1 parent 9bc5605 commit af1398d

10 files changed

+38
-12
lines changed

Diff for: composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
},
2323
"require": {
2424
"php": ">=7.4",
25+
"ext-zlib": "*",
2526
"async-aws/s3": "^1.5",
2627
"psr/log": "^1.1|^2.0|^3.0"
2728
},
2829
"require-dev": {
30+
"ext-zip": "*",
2931
"symfony/phpunit-bridge": "^5.4|^6.0"
3032
}
3133
}

Diff for: src/Archive.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip;
54

65
use GromNaN\S3Zip\Input\InputInterface;
76

87
class Archive
98
{
109
private InputInterface $input;
10+
11+
/** @var array<int, File> */
1112
private array $filesByIndex;
13+
14+
/** @var array<string, File> */
1215
private array $filesByName;
1316

1417
public function __construct(InputInterface $input)
@@ -76,11 +79,17 @@ private function initCentralDirectory()
7679
}
7780
}
7881

82+
/**
83+
* @return string[]
84+
*/
7985
public function getFileNames(): array
8086
{
8187
return array_keys($this->filesByName);
8288
}
8389

90+
/**
91+
* @return array<int, File>
92+
*/
8493
public function getFiles(): array
8594
{
8695
return $this->filesByIndex;

Diff for: src/File.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip;
54

65
use GromNaN\S3Zip\Input\InputInterface;
@@ -14,6 +13,9 @@ class File
1413
private int $length;
1514
private array $options;
1615

16+
/**
17+
* @internal
18+
*/
1719
public function __construct(InputInterface $input, array $options)
1820
{
1921
$this->input = $input;
@@ -33,14 +35,26 @@ public function getIndex(): int
3335
return $this->options['index'];
3436
}
3537

38+
/**
39+
* Reads and extract file contents
40+
*/
3641
public function getContents($length = 0): string
42+
{
43+
return gzinflate($this->fetch(), $length);
44+
}
45+
46+
/**
47+
* Reads compressed file contents.
48+
* Binary result can be sent as gzipped HTTP response.
49+
*/
50+
public function fetch(): string
3751
{
3852
$chunk = $this->input->fetch($this->offset, $this->length, 'reading file '.$this->name);
3953

4054
$headerSize = 30
4155
+unpack('v', $chunk, 26)[1]
4256
+unpack('v', $chunk, 28)[1];
4357

44-
return gzinflate(substr($chunk, $headerSize), $length);
58+
return substr($chunk, $headerSize);
4559
}
4660
}

Diff for: src/Input/HttpInput.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Input;
54

65
use AsyncAws\S3\S3Client;

Diff for: src/Input/InputInterface.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Input;
54

65
interface InputInterface

Diff for: src/Input/LocalInput.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Input;
54

65
class LocalInput implements InputInterface
76
{
87
private string $filename;
8+
9+
/**
10+
* @var resource
11+
*/
912
private $handle;
1013

1114
public function __construct(string $filename)
1215
{
1316
$this->filename = $filename;
14-
$handle = fopen($filename, 'r');
15-
if (false === $filename) {
17+
$handle = fopen($filename, 'rb');
18+
if (false === $handle) {
1619
throw new \RuntimeException('File not found: '.$filename);
1720
}
1821
$this->handle = $handle;

Diff for: src/Input/S3Input.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Input;
54

65
use AsyncAws\S3\S3Client;

Diff for: tests/Integration/ArchiveTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Tests\Integration;
54

65
use GromNaN\S3Zip\Archive;
@@ -34,6 +33,10 @@ public function testArchive()
3433

3534
$this->assertInstanceOf(File::class, $files[0]);
3635
$this->assertSame($files[5], $archive->getFile($files[5]->getName()));
36+
37+
$contents = $files[3]->getContents();
38+
$compressedContents = $files[3]->fetch();
39+
$this->assertSame($contents, gzinflate($compressedContents));
3740
}
3841

3942
/**

Diff for: tests/Integration/LocalArchiveTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Tests\Integration;
54

65
use GromNaN\S3Zip\Input\InputInterface;

Diff for: tests/Integration/S3ArchiveTest.php

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace GromNaN\S3Zip\Tests\Integration;
54

65
use AsyncAws\S3\S3Client;

0 commit comments

Comments
 (0)