Commit dc564c89 authored by Richard Weinhold's avatar Richard Weinhold 🎩

changes the File\Zip::addDirectory() default behavior

- Zip::addDirectory() now adds the directories content into
  a new archive-node named after the original folder
- the old behavior was renmand into a own method called:
  Zip::addDirectoryContent()
parent 3eff3da0
Pipeline #5344 passed with stages
in 12 seconds
......@@ -216,21 +216,47 @@ class Zip extends File
}
/**
* adds directory to zip-archive
* @param Directory $directory
* @param string $toNode adds content of directory to a sub-directory, or the root of the zip-archive
* @param callable|null $filter DirectoryIterator-Filter in format: function(Storage $file): bool
* @return self
*/
public function addDirectory(Directory $directory, ?callable $filter = null): self
public function addDirectory(Directory $directory, string $toNode = '/', ?callable $filter = null): self
{
return $this->addDirectoryStorage($directory->storage(), $filter);
return $this->addDirectoryStorage($directory->storage(), $toNode, $filter);
}
/**
* @param Storage $storage
* @param string $toNode adds content of directory to a sub-directory, or the root of the zip-archive
* @param callable|null $filter DirectoryIterator-Filter in format: function(Storage $file): bool
* @return self
*/
public function addDirectoryStorage(Storage $storage, ?callable $filter = null): self
public function addDirectoryStorage(Storage $storage, string $toNode = '/', ?callable $filter = null): self
{
return $this->addDirectoryStorageContent($storage, rtrim($toNode, '/') . '/' . basename($storage->path()->raw) . '/', $filter);
}
/**
* adds directory-content to zip-archive
* @param Directory $directory
* @param string $toNode adds content of directory to a sub-directory, or the root of the zip-archive
* @param callable|null $filter DirectoryIterator-Filter in format: function(Storage $file): bool
* @return self
*/
public function addDirectoryContent(Directory $directory, string $toNode = '/', ?callable $filter = null): self
{
return $this->addDirectoryStorageContent($directory->storage(), $toNode, $filter);
}
/**
* @param Storage $storage
* @param string $toNode adds content of directory to a sub-directory, or the root of the zip-archive
* @param callable|null $filter DirectoryIterator-Filter in format: function(Storage $file): bool
* @return self
*/
public function addDirectoryStorageContent(Storage $storage, string $toNode = '/', ?callable $filter = null): self
{
// prepare recursive directory-iterator
$iterator = new DirectoryIterator($storage, true);
......@@ -247,6 +273,9 @@ class Zip extends File
// relative file-path for in-ziparchive-name:
$filepath = str_ireplace((rtrim($storage->path()->real, '/') . '/'), '', $fileStorage->path()->real);
// append given directory-name:
$filepath = ltrim(trim($toNode, '/') . '/' . $filepath, '/');
$this->addFileStorage($fileStorage, $filepath);
}
......@@ -317,6 +346,7 @@ class Zip extends File
private function addFileFromDisk(Storage\Disk $file, ?string $name = null): string
{
$name = $name ?? $file->path()->filename;
$name = str_replace(['\\', DIRECTORY_SEPARATOR], '/', $name);
if (!$this->archive->addFile($file->path()->real, $name)) {
throw new RuntimeException(sprintf('failed to add File "%s" from Disk (%s) to ZipArchive', $name, $file->path()->real), 500);
......@@ -343,6 +373,8 @@ class Zip extends File
}
}
$name = str_replace(['\\', DIRECTORY_SEPARATOR], '/', $name);
if (!$this->archive->addFromString($name, $file->readFile())) {
throw new RuntimeException('failed to add File from Memory to ZipArchive', 500);
}
......@@ -359,6 +391,7 @@ class Zip extends File
private function addFileFromFlysystem(Storage\Flysystem $file, ?string $name = null): string
{
$name = $name ?? basename($file->path());
$name = str_replace(['\\', DIRECTORY_SEPARATOR], '/', $name);
if (!$this->archive->addFromString($name, $file->readFile())) {
throw new RuntimeException('failed to add File from Flysystem to ZipArchive', 500);
......
......@@ -99,14 +99,14 @@ class ZipTest extends TestCase
$extractDir = new Directory(new Storage\Disk\Temp);
$zip->extractTo($extractDir->storage());
$this->assertSame(iterator_count($dir->list()->files()), iterator_count($extractDir->list()->files()));
$this->assertSame(iterator_count($dir->list(true)->files()), iterator_count($extractDir->list(true)->files()));
$sourceFiles = [];
foreach ($dir->list()->files() as $file) {
foreach ($dir->list(true)->files() as $file) {
$sourceFiles[$file->path()->filename] = $file->getHash();
}
foreach ($extractDir->list()->files() as $file) {
foreach ($extractDir->list(true)->files() as $file) {
$this->assertSame($sourceFiles[$file->path()->filename], $file->getHash());
}
}
......@@ -136,6 +136,9 @@ class ZipTest extends TestCase
}
}
/**
* @return void
*/
public function testMixedArchive()
{
$zip = new File\Zip(new Storage\Disk\Temp);
......@@ -163,5 +166,8 @@ class ZipTest extends TestCase
foreach ($extractDir->list(true)->files() as $file) {
$this->assertSame($sourceFiles[$file->path()->filepath], $file->getHash());
}
fwrite(STDERR, print_r(['zip-path' => $zip->path()->real], true));
$zip->removeOnFree(false);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment