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 in an archive.

**Parameters**

- `$renameMap`: An associative array where keys are old names and values are new names.

**Returns**: the command output on success.

**Throws**

- `InvalidArgumentException`: If source path is not set.

**Example**

```php
$sevenZip->rename(['old_name.txt' => 'new_name.txt']);
```

### `reset(): SevenZip`

Resets the property values to their original state.
Expand Down
36 changes: 36 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 @@ -1086,6 +1086,42 @@
return $this->parseFileInfoOutput($output);
}

/**
* Renames files in an archive.
*
* @param array $renameMap An associative array where keys are old names and values are new names.
*
* @return string The output of the 7-Zip command.
* @throws \InvalidArgumentException If source path is not set.
*/
public function rename(array $renameMap): string
{
if (!$this->getSourcePath()) {
throw new \InvalidArgumentException(
"Archive file path (source) must be set",
);
}

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);
}

/**
* Test the integrity of an archive.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/SevenZipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,32 @@ public function testCompress(string $format): void
$this->assertFileExists(filename: $archive);
}

#[Covers('\Verseles\SevenZip\SevenZip::rename')]
#[DataProvider('compressAndExtractDataProvider')]
#[Depends('testCompress')]
public function testRename(string $format): void
{
$archive = $this->testDir . '/target/archive.' . $format;

// 7z doesn't support renaming in bzip2 format out of the box with `rn` command.
if (in_array($format, ['bzip2'])) {
$this->expectException(\RuntimeException::class);
}

$output = $this->sevenZip
->source(path: $archive)
->rename(['source/Avatart.svg' => 'source/Avatar_renamed.svg']);

if (!in_array($format, ['bzip2'])) {
$this->assertStringContainsString('Everything is Ok', $output);

// Revert back so extract test doesn't fail
$this->sevenZip
->source(path: $archive)
->rename(['source/Avatar_renamed.svg' => 'source/Avatart.svg']);
}
}

#[Covers('\Verseles\SevenZip\SevenZip::verify')]
#[DataProvider('compressAndExtractDataProvider')]
#[Depends('testCompress')]
Expand Down
Loading