diff --git a/README.md b/README.md index fc1d5ce..514e87d 100644 --- a/README.md +++ b/README.md @@ -643,6 +643,26 @@ Removes a compression flag. **Returns**: The current instance of the SevenZip class. +### `rename(array $renameMap): string` + +Renames files inside an archive. + +**Parameters** + +- `$renameMap`: An associative array mapping old file names to new file names. + +**Returns**: The output from the 7-Zip command. + +**Throws** + +- `InvalidArgumentException`: If the archive path is not set or rename map is empty. + +**Example** + +```php +$sevenZip->rename(['old_name.txt' => 'new_name.txt']); +``` + ### `reset(): SevenZip` Resets the property values to their original state. diff --git a/src/SevenZip.php b/src/SevenZip.php index 67eecbb..d29f71b 100644 --- a/src/SevenZip.php +++ b/src/SevenZip.php @@ -1463,6 +1463,46 @@ public function verify(): string return $this->runCommand($command); } + /** + * Rename files in an archive. + * + * @param array $renameMap An associative array mapping old file names to new file names. + * + * @return string The output of the 7-Zip command. + * @throws \InvalidArgumentException If source path is not set or rename map is empty. + */ + public function rename(array $renameMap): string + { + if (!$this->getSourcePath()) { + throw new \InvalidArgumentException( + "Archive file path (source) must be set", + ); + } + + if (empty($renameMap)) { + throw new \InvalidArgumentException("Rename map cannot be empty"); + } + + if ($this->getPassword()) { + $this->addFlag("p", $this->getPassword(), glued: true); + } + + $command = [ + $this->sevenZipPath, + "rn", + ...$this->flagrize($this->getAlwaysFlags()), + ...$this->flagrize($this->getCustomFlags()), + $this->getSourcePath(), + ]; + + foreach ($renameMap as $oldName => $newName) { + $command[] = $oldName; + $command[] = $newName; + } + + return $this->runCommand($command); + } + public function getFlag(string $flag): mixed { return $this->customFlags[$flag] ?? null; diff --git a/tests/SevenZipTest.php b/tests/SevenZipTest.php index 824e221..e78ed83 100644 --- a/tests/SevenZipTest.php +++ b/tests/SevenZipTest.php @@ -567,5 +567,44 @@ public function testDeleteSourceAfterExtract($tarPath) } + #[Covers('\Verseles\SevenZip\SevenZip::rename')] + public function testRename(): void + { + $format = 'zip'; + $directory = $this->testDir . '/source/*'; + $archive = $this->testDir . '/target/rename_archive.' . $format; + + // Compress + $this->sevenZip + ->format($format) + ->faster() + ->source($directory) + ->target($archive) + ->compress(); + + $this->assertFileExists($archive); + + // Rename Avatart.svg to RenamedAvatar.svg + $this->sevenZip + ->source($archive) + ->rename(['Avatart.svg' => 'RenamedAvatar.svg']); + + $fileList = $this->sevenZip->source($archive)->fileList(); + + $foundOld = false; + $foundNew = false; + + foreach ($fileList as $file) { + if ($file['path'] === 'Avatart.svg') { + $foundOld = true; + } + if ($file['path'] === 'RenamedAvatar.svg') { + $foundNew = true; + } + } + + $this->assertFalse($foundOld, 'Old file name should not be in the archive'); + $this->assertTrue($foundNew, 'New file name should be in the archive'); + } }