diff --git a/src/Archive7z.php b/src/Archive7z.php index 4cc960f..86ec748 100644 --- a/src/Archive7z.php +++ b/src/Archive7z.php @@ -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) diff --git a/tests/Archive7zTest.php b/tests/Archive7zTest.php index d1e16d4..fd8cdd4 100644 --- a/tests/Archive7zTest.php +++ b/tests/Archive7zTest.php @@ -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'], @@ -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'], @@ -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'], @@ -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); } /** diff --git a/tests/fixtures/longName.zip b/tests/fixtures/longName.zip deleted file mode 100644 index 9a9ca1e..0000000 Binary files a/tests/fixtures/longName.zip and /dev/null differ diff --git a/tests/fixtures/warnings.zip b/tests/fixtures/warnings.zip new file mode 100644 index 0000000..da1f5f2 Binary files /dev/null and b/tests/fixtures/warnings.zip differ