Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
40 changes: 40 additions & 0 deletions src/SevenZip.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<?php

namespace Verseles\SevenZip;
Expand Down Expand Up @@ -1463,6 +1463,46 @@
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;
Expand Down
39 changes: 39 additions & 0 deletions tests/SevenZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

}
Loading