From 4a3bf3d39121fde920139d06b8af3340e4d0c1df Mon Sep 17 00:00:00 2001 From: Keith Date: Sat, 15 Nov 2014 12:50:54 +0000 Subject: [PATCH] Add addString() create file in zip by content and filename - Fix #23 --- README.md | 4 +++ .../Zipper/Repositories/ZipRepository.php | 12 +++++++++ src/Chumper/Zipper/Zipper.php | 27 +++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/README.md b/README.md index 3af9d4c..99c2a68 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,10 @@ get the content of a file in the zip. This will return the content or false. add a string or an array of files to the zip You can name files or folder, all files in the folder then will be added. +**addString($filename, $content)** + +add a file to the zip by specifying a name and content as strings + **getStatus()** diff --git a/src/Chumper/Zipper/Repositories/ZipRepository.php b/src/Chumper/Zipper/Repositories/ZipRepository.php index 75a270e..3175fce 100644 --- a/src/Chumper/Zipper/Repositories/ZipRepository.php +++ b/src/Chumper/Zipper/Repositories/ZipRepository.php @@ -41,6 +41,18 @@ public function addFile($pathToFile, $pathInArchive) { $this->archive->addFile($pathToFile, $pathInArchive); } + + /** + * Add a file to the opened Archive using its contents + * + * @param $name + * @param $content + * @return void + */ + public function addFromString($name, $content) + { + $this->archive->addFromString($name, $content); + } /** * Remove a file permanently from the Archive diff --git a/src/Chumper/Zipper/Zipper.php b/src/Chumper/Zipper/Zipper.php index 66eea5e..4887fa8 100644 --- a/src/Chumper/Zipper/Zipper.php +++ b/src/Chumper/Zipper/Zipper.php @@ -158,6 +158,21 @@ public function add($pathToAdd) return $this; } + + /** + * Add a file to the zip using its contents + * + * @param $filename string The name of the file to create + * @param $content string The file contents + * @return $this Zipper instance + */ + public function addString($filename, $content) + { + $this->addFromString($filename, $content); + + return $this; + } + /** * Gets the status of the zip. @@ -366,6 +381,18 @@ private function addFile($pathToAdd) $this->repository->addFile($pathToAdd, $this->getInternalPath() . $file_name); } + + /** + * Add the file to the zip from content + * + * @param $filename + * @param $content + */ + private function addFromString($filename, $content) + { + $this->repository->addFromString($this->getInternalPath() . $filename, $content); + } + /** * @param $path