From 5259728443ae7803a025bf34af6977a70517c70d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Fri, 18 Feb 2022 20:55:44 +1100 Subject: [PATCH] Improve .zip functionality --- .../ClientPortal/InvoiceController.php | 55 +++++++++++--- composer.json | 3 +- composer.lock | 75 ++++++++++++++++++- 3 files changed, 120 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/ClientPortal/InvoiceController.php b/app/Http/Controllers/ClientPortal/InvoiceController.php index b6789e8dfdd8..9a38d07f0ee5 100644 --- a/app/Http/Controllers/ClientPortal/InvoiceController.php +++ b/app/Http/Controllers/ClientPortal/InvoiceController.php @@ -228,21 +228,54 @@ class InvoiceController extends Controller }, basename($file), ['Content-Type' => 'application/pdf']); } - // enable output of HTTP headers - $options = new Archive(); - $options->setSendHttpHeaders(true); + return $this->buildZip($invoices); - // create a new zipstream object - $zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options); + // // enable output of HTTP headers + // $options = new Archive(); + // $options->setSendHttpHeaders(true); - foreach ($invoices as $invoice) { + // // create a new zipstream object + // $zip = new ZipStream(date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip', $options); - #add it to the zip - $zip->addFile(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true))); + // foreach ($invoices as $invoice) { + + // #add it to the zip + // $zip->addFile(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true))); + + // } + + // // finish the zip stream + // $zip->finish(); + } + + + private function buildZip($invoices) + { + // create new archive + $zipFile = new \PhpZip\ZipFile(); + try{ + + foreach ($invoices as $invoice) { + + #add it to the zip + $zipFile->addFromString(basename($invoice->pdf_file_path()), file_get_contents($invoice->pdf_file_path(null, 'url', true))); + + } + + $filename = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip'; + $filepath = sys_get_temp_dir() . '/' . $filename; + + $zipFile->saveAsFile($filepath) // save the archive to a file + ->close(); // close archive + + return response()->download($filepath, $filename)->deleteFileAfterSend(true); } - - // finish the zip stream - $zip->finish(); + catch(\PhpZip\Exception\ZipException $e){ + // handle exception + } + finally{ + $zipFile->close(); + } } } diff --git a/composer.json b/composer.json index 877931817fe1..fb87b944e72f 100644 --- a/composer.json +++ b/composer.json @@ -67,6 +67,7 @@ "livewire/livewire": "^2.6", "maennchen/zipstream-php": "^1.2", "mollie/mollie-api-php": "^2.36", + "nelexa/zip": "^4.0", "nwidart/laravel-modules": "^8.0", "omnipay/paypal": "^3.0", "payfast/payfast-php-sdk": "^1.1", @@ -149,4 +150,4 @@ }, "minimum-stability": "dev", "prefer-stable": true -} \ No newline at end of file +} diff --git a/composer.lock b/composer.lock index e8ef2564f977..ecfd6023ba9a 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f430e1dc06c72464f547ba0bb71226cf", + "content-hash": "ebb191c91f4011a448605a0fd725faff", "packages": [ { "name": "apimatic/jsonmapper", @@ -5582,6 +5582,79 @@ ], "time": "2021-07-05T08:18:36+00:00" }, + { + "name": "nelexa/zip", + "version": "4.0.1", + "source": { + "type": "git", + "url": "https://github.com/Ne-Lexa/php-zip.git", + "reference": "a18f80db509b1b6e9798e2745dc100759107f50c" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Ne-Lexa/php-zip/zipball/a18f80db509b1b6e9798e2745dc100759107f50c", + "reference": "a18f80db509b1b6e9798e2745dc100759107f50c", + "shasum": "" + }, + "require": { + "ext-zlib": "*", + "php": "^7.4 || ^8.0", + "psr/http-message": "*", + "symfony/finder": "*" + }, + "require-dev": { + "ext-bz2": "*", + "ext-dom": "*", + "ext-fileinfo": "*", + "ext-iconv": "*", + "ext-openssl": "*", + "ext-xml": "*", + "friendsofphp/php-cs-fixer": "^3.4.0", + "guzzlehttp/psr7": "^1.6", + "phpunit/phpunit": "^9", + "symfony/http-foundation": "*", + "symfony/var-dumper": "*", + "vimeo/psalm": "^4.6" + }, + "suggest": { + "ext-bz2": "Needed to support BZIP2 compression", + "ext-fileinfo": "Needed to get mime-type file", + "ext-iconv": "Needed to support convert zip entry name to requested character encoding", + "ext-openssl": "Needed to support encrypt zip entries or use ext-mcrypt" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpZip\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Ne-Lexa", + "email": "alexey@nelexa.ru", + "role": "Developer" + } + ], + "description": "PhpZip is a php-library for extended work with ZIP-archives. Open, create, update, delete, extract and get info tool. Supports appending to existing ZIP files, WinZip AES encryption, Traditional PKWARE Encryption, BZIP2 compression, external file attributes and ZIP64 extensions. Alternative ZipArchive. It does not require php-zip extension.", + "homepage": "https://github.com/Ne-Lexa/php-zip", + "keywords": [ + "archive", + "extract", + "unzip", + "winzip", + "zip", + "ziparchive" + ], + "support": { + "issues": "https://github.com/Ne-Lexa/php-zip/issues", + "source": "https://github.com/Ne-Lexa/php-zip/tree/4.0.1" + }, + "time": "2021-12-12T09:50:45+00:00" + }, { "name": "nesbot/carbon", "version": "2.56.0",