Skip to content

Commit 54f43f3

Browse files
committed
feat: Support 7z files
1 parent 6262789 commit 54f43f3

File tree

13 files changed

+502
-0
lines changed

13 files changed

+502
-0
lines changed

.github/workflows/build.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ jobs:
3131
php-version: "${{ matrix.php-version }}"
3232
extensions: mbstring
3333

34+
- name: "Install unix packages"
35+
run: "sudo apt-get update && sudo apt-get install -y 7zip"
36+
3437
- name: "Cache dependencies"
3538
uses: "actions/cache@v4"
3639
with:

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
"dama/doctrine-test-bundle": "^8.0",
1919
"doctrine/dbal": "^3.4",
2020
"ergebnis/phpstan-rules": "^2.2.0",
21+
"gemorroj/archive7z": "^5.3",
2122
"jangregor/phpstan-prophecy": "^1.0",
2223
"mikey179/vfsstream": "^1.6.11",
2324
"monolog/monolog": "^2.3|^3.0",
@@ -52,6 +53,7 @@
5253
"doctrine/dbal": "For schema trait",
5354
"doctrine/event-manager": "For schema trait",
5455
"dama/doctrine-test-bundle": "For schema trait, when using DAMA Static Driver",
56+
"gemorroj/archive7z": "For 7z file support",
5557
"monolog/monolog": "For http client mock trait",
5658
"riverline/multipart-parser": "For multipart file uploads",
5759
"symfony/browser-kit": "For request trait",

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ parameters:
2929
enabled: false
3030
noExtends:
3131
classesAllowedToBeExtended:
32+
- Archive7z\Archive7z
3233
- Monolog\Handler\AbstractProcessingHandler
3334
- PHPUnit\Framework\Constraint\Constraint
3435
- RuntimeException

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<coverage/>
55
<php>
66
<ini name="error_reporting" value="-1"/>
7+
<ini name="date.timezone" value="UTC"/>
8+
<env name="TZ" value="UTC"/>
79
</php>
810
<testsuites>
911
<testsuite name="Project Test Suite">

src/SevenZipContents/Archive7z.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use Archive7z\Archive7z as BaseArchive7z;
8+
9+
use function escapeshellarg;
10+
use function exec;
11+
use function file_exists;
12+
use function is_string;
13+
14+
final class Archive7z extends BaseArchive7z
15+
{
16+
private const array EXECUTABLES = ['7z', '7zz', '7za'];
17+
18+
private static string|null $binary7z = null;
19+
20+
public function __construct(string $filename, float|null $timeout = 60.0)
21+
{
22+
parent::__construct($filename, self::getBinary7zFromPath(), $timeout);
23+
}
24+
25+
private static function getBinary7zFromPath(): string
26+
{
27+
if (self::$binary7z) {
28+
return self::$binary7z;
29+
}
30+
31+
$binary7z = null;
32+
foreach (self::EXECUTABLES as $executable) {
33+
$resultCode = 0;
34+
$binary7z = exec('which ' . escapeshellarg($executable), result_code: $resultCode); // @phpstan-ignore-line
35+
36+
if ($resultCode === 0 && is_string($binary7z) && $binary7z !== '' && file_exists($binary7z)) {
37+
break;
38+
}
39+
}
40+
41+
self::$binary7z = self::makeBinary7z($binary7z);
42+
43+
return self::$binary7z;
44+
}
45+
}

src/SevenZipContents/FileInfo.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use DateTimeImmutable;
8+
use DateTimeZone;
9+
10+
use function array_pop;
11+
use function array_push;
12+
use function explode;
13+
use function implode;
14+
use function str_replace;
15+
use function substr;
16+
use function trim;
17+
18+
final readonly class FileInfo
19+
{
20+
private string $path;
21+
private DateTimeImmutable|null $lastModified;
22+
23+
public function __construct(
24+
string $path,
25+
private int $size,
26+
private int $compressedSize,
27+
private int $compression,
28+
string|null $lastModified,
29+
private string|null $crc,
30+
private string|null $comment,
31+
private bool $isDir,
32+
) {
33+
$this->path = $this->cleanPath($path);
34+
35+
$utc = new DateTimeZone('UTC');
36+
37+
$this->lastModified = $lastModified
38+
? DateTimeImmutable::createFromFormat('Y-m-d H:i:s', substr($lastModified, 0, 19), $utc)
39+
: null;
40+
}
41+
42+
public function getPath(): string
43+
{
44+
return $this->path;
45+
}
46+
47+
public function getSize(): int
48+
{
49+
if ($this->isDir) {
50+
return 0;
51+
}
52+
53+
return $this->size;
54+
}
55+
56+
public function getCompressedSize(): int
57+
{
58+
return $this->compressedSize;
59+
}
60+
61+
public function getCompression(): int
62+
{
63+
return $this->compression;
64+
}
65+
66+
public function getCrc(): string|null
67+
{
68+
return $this->crc;
69+
}
70+
71+
public function getComment(): string|null
72+
{
73+
return $this->comment;
74+
}
75+
76+
public function isDir(): bool
77+
{
78+
return $this->isDir;
79+
}
80+
81+
public function getLastModified(): DateTimeImmutable|null
82+
{
83+
return $this->lastModified;
84+
}
85+
86+
/**
87+
* Cleans up a path and removes relative parts, also strips leading slashes
88+
*/
89+
private function cleanPath(string $path): string
90+
{
91+
$path = str_replace('\\', '/', $path);
92+
$path = explode('/', $path);
93+
$newpath = [];
94+
foreach ($path as $p) {
95+
if ($p === '' || $p === '.') {
96+
continue;
97+
}
98+
99+
if ($p === '..') {
100+
array_pop($newpath);
101+
continue;
102+
}
103+
104+
array_push($newpath, $p);
105+
}
106+
107+
return trim(implode('/', $newpath), '/');
108+
}
109+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use RuntimeException;
8+
9+
use function sprintf;
10+
11+
final class InvalidArchive extends RuntimeException
12+
{
13+
public static function notAFile(mixed $path): self
14+
{
15+
return new self(sprintf('Path %s is not valid', $path));
16+
}
17+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use function is_file;
8+
9+
final class ZipContents
10+
{
11+
public function readFile(string $file): ZipInfo
12+
{
13+
if (!is_file($file)) {
14+
throw InvalidArchive::notAFile($file);
15+
}
16+
17+
$archive = new Archive7z($file);
18+
$info = $archive->getInfo();
19+
20+
$fileInfos = [];
21+
foreach ($archive->getEntries() as $entry) {
22+
$path = $entry->getPath();
23+
24+
$size = (int) $entry->getSize();
25+
$packedSize = (int) $entry->getPackedSize();
26+
$compression = $size && $packedSize
27+
? (int) ($packedSize * 100 / $size)
28+
: 0;
29+
30+
$fileInfos[$path] = new FileInfo(
31+
$path,
32+
$size,
33+
$packedSize,
34+
$compression,
35+
$entry->getModified(),
36+
$entry->getCrc(),
37+
$entry->getComment(),
38+
$entry->isDirectory(),
39+
);
40+
}
41+
42+
return new ZipInfo($info->getPhysicalSize(), $fileInfos);
43+
}
44+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use PHPUnit\Framework\Assert;
8+
use PHPUnit\Framework\TestCase;
9+
10+
/** @mixin TestCase */
11+
trait ZipContentsTrait
12+
{
13+
final protected static function read7zFile(string $path): ZipInfo
14+
{
15+
$zipContents = new ZipContents();
16+
17+
return $zipContents->readFile($path);
18+
}
19+
20+
final protected static function assert7zHasSize(int $expectedSize, ZipInfo $zip, string $message = ''): void
21+
{
22+
Assert::assertSame($expectedSize, $zip->getSize(), $message);
23+
}
24+
25+
final protected static function assert7zHasNumberOfFiles(
26+
int $expectedNumberOfFiles,
27+
ZipInfo $zip,
28+
string $message = '',
29+
): void {
30+
Assert::assertCount($expectedNumberOfFiles, $zip, $message);
31+
}
32+
33+
final protected static function assert7zHasFile(string $expectedPath, ZipInfo $zip, string $message = ''): void
34+
{
35+
Assert::assertTrue($zip->hasFile($expectedPath), $message);
36+
}
37+
38+
final protected static function assert7zHasFileWithSize(
39+
string $expectedPath,
40+
int $expectedSize,
41+
ZipInfo $zip,
42+
string $message = '',
43+
): void {
44+
self::assert7zHasFile($expectedPath, $zip, $message);
45+
46+
$file = $zip->getFile($expectedPath);
47+
48+
Assert::assertSame($expectedSize, $file?->getSize(), $message);
49+
}
50+
}

src/SevenZipContents/ZipInfo.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Brainbits\FunctionalTestHelpers\SevenZipContents;
6+
7+
use Countable;
8+
use Generator;
9+
use IteratorAggregate;
10+
11+
use function array_key_exists;
12+
use function array_values;
13+
use function count;
14+
15+
/** @implements IteratorAggregate<string, FileInfo> */
16+
final class ZipInfo implements Countable, IteratorAggregate
17+
{
18+
/** @param FileInfo[] $files */
19+
public function __construct(private int $size, private array $files)
20+
{
21+
}
22+
23+
public function getSize(): int
24+
{
25+
return $this->size;
26+
}
27+
28+
/** @return list<FileInfo> */
29+
public function getFiles(): array
30+
{
31+
return array_values($this->files);
32+
}
33+
34+
public function hasFile(string $path): bool
35+
{
36+
return array_key_exists($path, $this->files);
37+
}
38+
39+
public function getFile(string $path): FileInfo|null
40+
{
41+
if (!$this->hasFile($path)) {
42+
return null;
43+
}
44+
45+
return $this->files[$path];
46+
}
47+
48+
public function getIterator(): Generator
49+
{
50+
yield from $this->files;
51+
}
52+
53+
public function count(): int
54+
{
55+
return count($this->files);
56+
}
57+
}

0 commit comments

Comments
 (0)