Skip to content

Commit

Permalink
feature: add getWarnings function
Browse files Browse the repository at this point in the history
the function shows warnings in archive
  • Loading branch information
Gemorroj committed Nov 13, 2021
1 parent c0efadf commit 00aa175
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
29 changes: 29 additions & 0 deletions src/Archive7z.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,35 @@ public function isValid(): bool
return false !== \strpos($process->getOutput(), 'Everything is Ok');
}

/**
* List archive warnings.
*
* @throws \Symfony\Component\Process\Exception\ProcessFailedException
*
* @return string[]
*/
public function getWarnings(): array
{
$process = $this->makeProcess('t', $this->decorateCmdExtract());

$this->execute($process);

$output = $process->getOutput();

$pos = \strpos($output, \PHP_EOL.'--');
$header = \substr($output, 0, $pos);
$warningsHeader = \PHP_EOL.'WARNINGS:';
$warningsPos = \strrpos($header, $warningsHeader);
if (false === $warningsPos) {
return [];
}

$warningsStr = \substr($header, $warningsPos + \strlen($warningsHeader));
$warningsStr = \trim($warningsStr);

return \explode(\PHP_EOL, $warningsStr);
}

/**
* Exit codes
* 0 - Normal (no errors or warnings detected)
Expand Down
30 changes: 12 additions & 18 deletions tests/Archive7zTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public function extractProvider(): array
{
return [
['zip.7z'],
['warnings.zip'],
['7zip-18.05/test.7z'],
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
Expand Down Expand Up @@ -339,6 +340,7 @@ public function entryProvider(): array
{
return [
['zip.7z'],
['warnings.zip'],
['7zip-18.05/test.7z'],
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
Expand Down Expand Up @@ -483,6 +485,7 @@ public function delProvider(): array
{
return [
['zip.7z'], // 7-Zip 21.02+ swears now at this
//['warnings.zip'], // not supported
['7zip-18.05/test.7z'],
['7zip-18.05/test.tar'],
['7zip-18.05/test.wim'],
Expand Down Expand Up @@ -585,26 +588,17 @@ public function testIsValid(string $archiveName): void
self::assertTrue($valid->isValid());
}

public function testLongName(): void
public function testGetWarnings(): void
{
$obj = new Archive7z($this->fixturesDir.'/longName.zip');
$result = $obj->getEntries();
$obj = new Archive7z($this->fixturesDir.'/warnings.zip');
$warnings = $obj->getWarnings();

self::assertIsArray($result);
self::assertCount(3, $result);
foreach ($result as $entry) {
self::assertInstanceOf(Entry::class, $entry);
self::assertIsString($entry->getPath());
self::assertIsString($entry->getAttributes());
self::assertIsString($entry->getContent());
self::assertIsString($entry->getCrc());
self::assertIsString($entry->getEncrypted());
self::assertIsString($entry->getMethod());
self::assertIsString($entry->getModified());
self::assertIsString($entry->getPackedSize());
self::assertIsString($entry->getSize());
self::assertIsString($entry->getUnixPath());
}
self::assertCount(1, $warnings);
self::assertSame('There are data after the end of archive', $warnings[0]);

$obj = new Archive7z($this->fixturesDir.'/zip.7z');
$noWarnings = $obj->getWarnings();
self::assertEmpty($noWarnings);
}

/**
Expand Down
Binary file removed tests/fixtures/longName.zip
Binary file not shown.
Binary file added tests/fixtures/warnings.zip
Binary file not shown.

0 comments on commit 00aa175

Please sign in to comment.