Commit 467065a6 authored by Richard Weinhold's avatar Richard Weinhold 🎩

adds simple File\Zip::add() method

- ...to dynamically call addFile() or addDirectory() methods
parent aee4c169
Pipeline #5383 passed with stages
in 16 seconds
......@@ -263,9 +263,9 @@ $image->edit(function (Intervention\Image $image): Intervention\Image {
```php
$zip = new File\Zip(new Storage\Disk('archive.zip'));
$zip->addFile(new File(new Storage\Disk('file.json')));
$zip->addFile(new File(new Storage\Memory('some file-content')), 'anotherfile.txt');
$zip->addDirectory(new Directory(new Storage\Disk(__DIR__, 'data-dir')));
$zip->add(new File(new Storage\Disk('file.json'))); // or $zip->addFile(...)
$zip->add(new File(new Storage\Memory('some file-content')), 'anotherfile.txt'); // or $zip->addFile(...)
$zip->add(new Directory(new Storage\Disk(__DIR__, 'data-dir'))); // or $zip->addDirectory(...)
$zip->commit();
$extractDir = $zip->extractTo(new Storage\Disk\Temp);
......
......@@ -11,6 +11,7 @@ use ricwein\FileSystem\Exceptions\RuntimeException;
use ricwein\FileSystem\Exceptions\FileNotFoundException;
use ricwein\FileSystem\Exceptions\UnexpectedValueException;
use ricwein\FileSystem\File;
use ricwein\FileSystem\FileSystem;
use ricwein\FileSystem\Helper\MimeType;
use ricwein\FileSystem\Helper\Constraint;
use ricwein\FileSystem\Helper\DirectoryIterator;
......@@ -256,6 +257,36 @@ class Zip extends File
return $destinationDir;
}
/**
* add file or directory to zip-archive
* @param FileSystem $file
* @param string|null $asNode
* @return self
*/
public function add(FileSystem $file, ?string $asNode = null): self
{
if ($file instanceof Directory) {
return $this->addDirectory($file, $asNode ?? '/');
} else {
return $this->addFile($file, $asNode);
}
}
/**
* add file or directory storage to zip-archive
* @param Storage $storage
* @param string|null $asNode
* @return self
*/
public function addStorage(Storage $storage, ?string $asNode = null): self
{
if ($storage->isDir()) {
return $this->addDirectoryStorage($storage, $asNode ?? '/');
} else {
return $this->addFileStorage($storage, $asNode);
}
}
/**
* adds directory to zip-archive
* @param Directory $directory
......
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