Skip to content

Commit

Permalink
fix: add physicalSize for tgz archives
Browse files Browse the repository at this point in the history
  • Loading branch information
Gemorroj committed Mar 18, 2023
1 parent 54f0522 commit 5743569
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 9 deletions.
5 changes: 2 additions & 3 deletions src/Archive7z.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ public function getInfo(): Info
$out = \explode(\PHP_EOL, $process->getOutput());

$parser = new Parser($out);
$data = $parser->parseHeader();

return new Info($data);
return new Info($parser);
}

/**
Expand All @@ -367,7 +366,7 @@ public function getInfo(): Info
public function addEntry(string $path, bool $storePath = false): void
{
$args = [];
$args[] = '-mx='.(int) $this->compressionLevel;
$args[] = '-mx='.$this->compressionLevel;

if ($storePath) {
$args[] = '-spf';
Expand Down
17 changes: 13 additions & 4 deletions src/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
Path = test.zip
Type = zip
Physical Size = 165038
Path = test.tgz
Type = gzip
Headers Size = 19
*/
class Info
{
Expand All @@ -40,14 +44,19 @@ class Info

private ?string $codePage = null;

/**
* @param array<string, string> $data parsed data
*/
public function __construct(array $data)
public function __construct(Parser $parser)
{
$data = $parser->parseHeader();

foreach ($data as $k => $v) {
$this->setData($k, $v);
}

if (!isset($this->physicalSize)) {
$info = $parser->parseInfo();
\preg_match('/\d+ file, (\d+) bytes/', $info, $match);
$this->physicalSize = (int) $match[1];
}
}

private function setData(string $key, string $value): void
Expand Down
15 changes: 15 additions & 0 deletions src/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ public function __construct(array $data)
$this->data = $data;
}

public function parseInfo(): string
{
$data = '';

foreach ($this->data as $value) {
if ($value === $this->headTokenStart) {
break;
}

$data .= $value."\n";
}

return $data;
}

/**
* @return array<string, string>
*/
Expand Down
31 changes: 29 additions & 2 deletions tests/Archive7zTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ public function extractProvider(): array
];
}

/**
* @return string[][]
*/
public function basicProvider(): array
{
return [
['zip.7z'],
['warnings.zip'],
['7zip-18.05/test.7z'],
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
['7zip-18.05/test.zip'],
['7zip-18.05/test.tgz'],
['totalcommander-9.21a/test.tar'],
['totalcommander-9.21a/test.zip'],
['winrar-5.61/test.zip'],
['winrar-5.61/test4.rar'],
['winrar-5.61/test5.rar'],
['linux/zip-0.3/test.zip'],
['linux/p7zip-16.02/test.7z'],
['linux/p7zip-16.02/test.tar'],
['linux/p7zip-16.02/test.zip'],
];
}

/**
* @dataProvider extractProvider
*/
Expand Down Expand Up @@ -332,6 +357,7 @@ public function entryProvider(): array
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
['7zip-18.05/test.zip'],
['7zip-18.05/test.tgz'],
['totalcommander-9.21a/test.tar'],
['totalcommander-9.21a/test.zip'],
['winrar-5.61/test.zip'],
Expand Down Expand Up @@ -468,6 +494,7 @@ public function delProvider(): array
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
['7zip-18.05/test.zip'],
// ['7zip-18.05/test.tgz'],
['totalcommander-9.21a/test.tar'],
['totalcommander-9.21a/test.zip'],
['winrar-5.61/test.zip'],
Expand Down Expand Up @@ -558,7 +585,7 @@ public function testRenameEntryPasswd(string $fixtureArchiveName): void
}

/**
* @dataProvider extractProvider
* @dataProvider basicProvider
*/
public function testIsValid(string $archiveName): void
{
Expand Down Expand Up @@ -765,7 +792,7 @@ public function testCreateMixedSolidArchive(): void
}

/**
* @dataProvider extractProvider
* @dataProvider basicProvider
*/
public function testInfo(string $archiveName): void
{
Expand Down
Binary file added tests/fixtures/7zip-18.05/test.tgz
Binary file not shown.

0 comments on commit 5743569

Please sign in to comment.