├── .chipperci.yml ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── config └── zipstream.php └── src ├── Builder.php ├── Contracts └── FileContract.php ├── Events ├── ZipSizePredictionFailed.php ├── ZipStreamed.php └── ZipStreaming.php ├── Exceptions ├── FilenameMissingException.php ├── NotWritableException.php └── UnsupportedSourceDiskException.php ├── Facades └── Zip.php ├── Models ├── File.php ├── HttpFile.php ├── LocalFile.php ├── S3File.php └── TempFile.php ├── OutputStream.php ├── Queue.php └── ZipStreamServiceProvider.php /.chipperci.yml: -------------------------------------------------------------------------------- 1 | version: 1 2 | 3 | environment: 4 | php: 8.3 # 7.1, 7.2, 7.3, 7.4, 8.0, 8.1 5 | 6 | pipeline: 7 | - name: Setup 8 | cmd: | 9 | composer install --no-interaction --prefer-dist --optimize-autoloader 10 | 11 | - name: Run Tests 12 | cmd: phpunit 13 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | Contributions are **welcome** and will be fully **credited**. 4 | 5 | Please read and understand the contribution guide before creating an issue or pull request. 6 | 7 | ## Etiquette 8 | 9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code 10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be 11 | extremely unfair for them to suffer abuse or anger for their hard work. 12 | 13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the 14 | world that developers are civilized and selfless people. 15 | 16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient 17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used. 18 | 19 | ## Viability 20 | 21 | When requesting or submitting new features, first consider whether it might be useful to others. Open 22 | source projects are used by many developers, who may have entirely different needs to your own. Think about 23 | whether or not your feature is likely to be used by other users of the project. 24 | 25 | ## Procedure 26 | 27 | Before filing an issue: 28 | 29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident. 30 | - Check to make sure your feature suggestion isn't already present within the project. 31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress. 32 | - Check the pull requests tab to ensure that the feature isn't already in progress. 33 | 34 | Before submitting a pull request: 35 | 36 | - Check the codebase to ensure that your feature doesn't already exist. 37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix. 38 | 39 | ## Requirements 40 | 41 | If the project maintainer has any additional requirements, you will find them listed here. 42 | 43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer). 44 | 45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests. 46 | 47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date. 48 | 49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option. 50 | 51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests. 52 | 53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting. 54 | 55 | **Happy coding**! 56 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Joseph Szobody 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Streaming Zips with Laravel 2 | 3 | [![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-zipstream.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-zipstream) 4 | [![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-zipstream.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-zipstream) 5 | [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md) 6 | ![Build Status](https://img.shields.io/endpoint?url=https://app.chipperci.com/projects/5cc95e3c-628f-48c6-815c-1f16621c9514/status/master&style=flat-square) 7 | 8 | A fast and simple streaming zip file downloader for Laravel. 9 | 10 | - Builds zip files from local or S3 file sources, or any other PSR7 stream. 11 | - Provides a direct download stream to your user. The zip download begins immediately even though the zip is still being created. No need to save the zip to disk first. 12 | - Calculates the zip filesize up front for the `Content-Length` header. The user gets an accurate download time estimate in their browser. 13 | - Built on top of the excellent [ZipStream-PHP](https://github.com/maennchen/ZipStream-PHP) library. 14 | 15 | ## Upgrading 16 | 17 | Upgrading from version 4? Make sure to look at the release notes for version 5. There are some breaking changes. 18 | 19 | https://github.com/stechstudio/laravel-zipstream/releases/tag/5.0 20 | 21 | ## Quickstart 22 | 23 | #### 1. Install the package 24 | 25 | ```php 26 | composer require stechstudio/laravel-zipstream 27 | ``` 28 | 29 | The service provider and facade will be automatically wired up. 30 | 31 | #### 2. In a controller method call the `create` method on the `Zip` facade 32 | 33 | ```php 34 | use STS\ZipStream\Facades\Zip; 35 | 36 | class ZipController { 37 | 38 | public function build() 39 | { 40 | return Zip::create("package.zip", [ 41 | "/path/to/Some File.pdf", 42 | "/path/to/Export.xlsx" 43 | ]); 44 | } 45 | } 46 | ``` 47 | 48 | That's it! A `StreamedResponse` will be returned and the zip contents built and streamed out. The user's browser will begin downloading a `package.zip` file immediately. 49 | 50 | ## Customize the internal zip path for a file 51 | 52 | By default, any files you add will be stored in the root of the zip, with their original filenames. 53 | 54 | You can customize the filename and even create sub-folders within the zip by providing your files array with key/value pairs: 55 | 56 | ```php 57 | Zip::create("package.zip", [ 58 | 59 | // Will be stored as `Some File.pdf` in the zip 60 | "/path/to/Some File.pdf", 61 | 62 | // Will be stored as `Export.xlsx` in the zip 63 | "/path/to/data.xlsx" => 'Export.xlsx', 64 | 65 | // Will create a `log` subfolder in the zip and be stored as `log/details.txt` 66 | "/path/to/log.txt" => "log/details.txt" 67 | 68 | ]); 69 | ``` 70 | 71 | ## Fluent usage 72 | 73 | You can also provide your files one at a time: 74 | 75 | ```php 76 | Zip::create("package.zip") 77 | ->add("/path/to/Some File.pdf") 78 | ->add("/path/to/data.xlsx", 'Export.xlsx') 79 | ->add("/path/to/log.txt", "log/details.txt"); 80 | ``` 81 | 82 | ## Add HTTP file sources 83 | 84 | You can add HTTP URLs as the source filepath. Note that zip filesize can only be calculated up front if the HTTP source provides a `Content-Length` header, not all URLs do. 85 | 86 | ```php 87 | Zip::create("package.zip") 88 | ->add("https://...", "myfile.pdf"); 89 | ``` 90 | 91 | ## Add raw file data 92 | 93 | You can provide raw data instead of a filepath: 94 | 95 | ```php 96 | Zip::create("package.zip") 97 | ->addRaw("...file contents...", "hello.txt"); 98 | ``` 99 | 100 | ## Add from storage disk 101 | 102 | You can add files from a storage disk. Use `addFromDisk` and provide the disk name or disk instance as the first argument: 103 | 104 | ```php 105 | Zip::create("package.zip") 106 | ->addFromDisk("local", "file.txt", "My File.txt") 107 | ->addFromDisk(Storage::disk("tmp"), "important.txt") 108 | ``` 109 | 110 | ## Support for S3 111 | 112 | ### Install AWS sdk and configure S3 113 | 114 | You can stream files from S3 into your zip. 115 | 116 | 1. Install the `aws/aws-sdk-php` package 117 | 118 | 2. Set up an AWS IAM user with `s3:GetObject` permission for the S3 bucket and objects you intend to zip up. 119 | 120 | 3. Store your credentials as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` in your .env file. 121 | 122 | ### Add S3 files to your zip 123 | 124 | Provide `s3://` paths when creating the zip: 125 | 126 | ```php 127 | Zip::create("package.zip") 128 | ->add("s3://bucket-name/path/to/object.pdf", "Something.pdf"); 129 | ``` 130 | 131 | By default, this package will try to create an S3 client using the same .env file credentials that Laravel uses. If needed, you can wire up a custom S3 client to the `zipstream.s3client` container key. Or you can even pass in your own S3 client when adding a file to the zip. To do this, you'll need to create an `S3File` model instance yourself so that you can provide the client, like this: 132 | 133 | ```php 134 | use STS\ZipStream\Models\S3File; 135 | 136 | // Create your own client however necessary 137 | $s3 = new Aws\S3\S3Client(); 138 | 139 | Zip::create("package.zip")->add( 140 | S3File::make("s3://bucket-name/path/to/object.pdf")->setS3Client($s3) 141 | ); 142 | ``` 143 | 144 | Instead of specifying an absolute `s3://` path, you can use `addFromDisk` and specify a disk that uses the `s3` driver: 145 | 146 | ```php 147 | Zip::create("package.zip") 148 | ->addFromDisk("s3", "object.pdf", "Something.pdf"); 149 | ``` 150 | 151 | In this case the S3 client from the storage disk will be used. 152 | 153 | ## Specify your own filesizes 154 | 155 | It can be expensive retrieving filesizes for some file sources such as S3 or HTTP. These require dedicated calls, and can add up to a lot of time if you are zipping up many files. If you store filesizes in your database and have them available, you can drastically improve performance by providing filesizes when you add files. You'll need to make your own File models instead of adding paths directly to the zip. 156 | 157 | Let's say you have a collection of Eloquent `$files`, are looping through and building a zip. If you have a `filesize` attribute available, it would look something like this: 158 | 159 | ```php 160 | use STS\ZipStream\Models\File; 161 | 162 | // Retrieve file records from the database 163 | $files = ...; 164 | 165 | $zip = Zip::create("package.zip"); 166 | 167 | foreach($files AS $file) { 168 | $zip->add( 169 | File::make($file->path, $file->name)->setFilesize($file->size) 170 | ); 171 | } 172 | ``` 173 | 174 | Or if you are adding from an S3 disk: 175 | 176 | ```php 177 | $zip->add( 178 | File::makeFromDisk('s3', $file->key, $file->name)->setFilesize($file->size) 179 | ); 180 | ```` 181 | 182 | ## Zip size prediction 183 | 184 | By default, this package attempts to predict the final zip size and sends a `Content-Length` header up front. This means users will see accurate progress on their download, even though the zip is being streamed out as it is created! 185 | 186 | This only works if files are not compressed. 187 | 188 | If you have issues with the zip size prediction you can disable it with `ZIPSTREAM_PREDICT_SIZE=false` in your .env file. 189 | 190 | ## Configure compression 191 | 192 | By default, this package uses _no_ compression. Why? 193 | 194 | 1) This makes building the zips super fast, and is light on your CPU 195 | 2) This makes it possible to predict the final zip size as mentioned above. 196 | 197 | If you want to compress your zip files set `ZIPSTREAM_COMPRESSION_METHOD=deflate` in your .env file. Just realize this will disable the `Content-Length` header. 198 | 199 | ## Configure conflict strategy 200 | 201 | If two or more files are added to the zip with the same zip path, you can use `ZIPSTREAM_CONFLICT_STRATEGY` to configure how the conflict is handled: 202 | 203 | - `ZIPSTREAM_CONFLICT_STRATEGY=skip`: Keep the initial file, skip adding the conflicting file (default) 204 | - `ZIPSTREAM_CONFLICT_STRATEGY=replace`: Keep the latest file, overwrite previous files at the same path 205 | - `ZIPSTREAM_CONFLICT_STRATEGY=rename`: Append a number to the conflicting file name, e.g. `file.txt` becomes `file_1.txt` 206 | 207 | Note: filenames are compared case-insensitive. `FILE.txt` and `file.TXT` are considered conflicting. If you are working only on a case-sensitive filesystem you can set `ZIPSTREAM_CASE_INSENSITIVE_CONFLICTS=false`. Don't do this if you have Windows users opening your zips! 208 | 209 | ## Save Zip to disk 210 | 211 | Even though the primary goal of this package is to enable zip downloads without saving to disk, there may be times you'd like to generate a zip on disk as well. And you might as well make use of this package to do so. 212 | 213 | Use the `saveTo` method to write the entire zip to disk immediately. Note that this expects a folder path, the zip name will be appended. 214 | 215 | ```php 216 | Zip::create("package.zip") 217 | // ... add files ... 218 | ->saveTo("/path/to/folder"); 219 | ``` 220 | 221 | And yes, if you've properly setup and configured S3 you can even save to an S3 bucket/path. 222 | 223 | ```php 224 | Zip::create("package.zip") 225 | // ... add files ... 226 | ->saveTo("s3://bucket-name/path/to/folder"); 227 | ``` 228 | 229 | Or you can save to a disk: 230 | 231 | ```php 232 | Zip::create("package.zip") 233 | // ... add files ... 234 | ->saveToDisk("s3", "folder"); 235 | ``` 236 | 237 | > [!WARNING] 238 | > S3 has a limit of 5GB when objects are saved using a stream the way we are doing. Saving your zip to S3 will fail if it is over this limit. If this is an issue for your use-case, we recommend saving to local disk first and then separately using the S3 SDK to store the file using multipart. 239 | 240 | ## Caching zip while still streaming download 241 | 242 | What if you have a lot of users requesting the same zip payload? It might be nice to stream out the zip while _also_ caching it to disk for the future. 243 | 244 | Use the `cache` method to provide a cache path. Note this should be the entire path including filename. 245 | 246 | ```php 247 | Zip::create("package.zip") 248 | // ... add files ... 249 | ->cache("/path/to/folder/some-unique-cache-name.zip"); 250 | ``` 251 | 252 | Or you can cache to a disk: 253 | 254 | ```php 255 | Zip::create("package.zip") 256 | // ... add files ... 257 | ->cacheToDisk("s3", "folder/some-unique-cache-name.zip"); 258 | ``` 259 | 260 | You might use an internal DB id for your cache name, so that the next time a user requests a zip download you can determine if one is already built and just hand it back. 261 | 262 | ## Events 263 | 264 | - `STS\ZipStream\Events\ZipStreaming`: Dispatched when a new zip stream begins processing 265 | - `STS\ZipStream\Events\ZipStreamed`: Dispatched when a zip finishes streaming 266 | 267 | ## Filename sanitization 268 | 269 | By default, this package will try to translate any non-ascii character in filename or folder's name to ascii. For example, if your filename is `中文_にほんご_Ч_Ɯ_☺_someascii.txt`. It will become `__C___someascii.txt` using Laravel's `Str::ascii($path)`. 270 | 271 | If you need to preserve non-ascii characters, you can disable this feature with an `.env` setting: 272 | 273 | ```env 274 | ZIPSTREAM_ASCII_FILENAMES=false 275 | ``` 276 | 277 | ## License 278 | 279 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information. 280 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "stechstudio/laravel-zipstream", 3 | "description": "A fast and simple streaming zip file downloader for Laravel.", 4 | "keywords": [ 5 | "stechstudio", 6 | "laravel-zipstream" 7 | ], 8 | "homepage": "https://github.com/stechstudio/laravel-zipstream", 9 | "license": "MIT", 10 | "type": "library", 11 | "authors": [ 12 | { 13 | "name": "Joseph Szobody", 14 | "email": "joseph@stechstudio.com" 15 | } 16 | ], 17 | "require": { 18 | "php": "^8.1", 19 | "guzzlehttp/guzzle": "^7.0", 20 | "guzzlehttp/psr7": "^2.6", 21 | "illuminate/support": "^10.0|^11.0|^12.0", 22 | "league/flysystem-path-prefixing": "^3.28", 23 | "maennchen/zipstream-php": "^v3.0", 24 | "psr/http-message": "^2.0", 25 | "spatie/laravel-package-tools": "^1.16" 26 | }, 27 | "require-dev": { 28 | "league/flysystem-aws-s3-v3": "^3.28", 29 | "orchestra/testbench": "^8.0|^9.0|^10.0", 30 | "phpunit/phpunit": "^9.0|^10.0|^11.5.3" 31 | }, 32 | "autoload": { 33 | "psr-4": { 34 | "STS\\ZipStream\\": "src" 35 | } 36 | }, 37 | "autoload-dev": { 38 | "psr-4": { 39 | "STS\\ZipStream\\Tests\\": "tests" 40 | } 41 | }, 42 | "scripts": { 43 | "test": "vendor/bin/phpunit", 44 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage" 45 | }, 46 | "config": { 47 | "sort-packages": true 48 | }, 49 | "extra": { 50 | "laravel": { 51 | "providers": [ 52 | "STS\\ZipStream\\ZipStreamServiceProvider" 53 | ], 54 | "aliases": { 55 | "Zip": "STS\\ZipStream\\Facades\\Zip" 56 | } 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /config/zipstream.php: -------------------------------------------------------------------------------- 1 | env('ZIPSTREAM_PREDICT_SIZE', true), 7 | 8 | // Compression method used only if we don't (or can't) predict the zip size 9 | 'compression_method' => env('ZIPSTREAM_COMPRESSION_METHOD', 'store'), 10 | 11 | // Remove all non-ascii characters from filenames 12 | 'ascii_filenames' => env('ZIPSTREAM_ASCII_FILENAMES', true), 13 | 14 | // What to do when a file with the same zip path is added twice. Options are 'skip', 'replace', 'rename' 15 | 'conflict_strategy' => env('ZIPSTREAM_CONFLICT_STRATEGY', 'skip'), 16 | 17 | // Don't allow 'Text.txt' and 'text.TXT' by default 18 | 'case_insensitive_conflicts' => env('ZIPSTREAM_CASE_INSENSITIVE_CONFLICTS', true), 19 | 20 | // AWS configs for S3 files 21 | 'aws' => [ 22 | 'credentials' => [ 23 | 'key' => env('AWS_ACCESS_KEY_ID'), 24 | 'secret' => env('AWS_SECRET_ACCESS_KEY') 25 | ], 26 | 'version' => 'latest', 27 | 'endpoint' => env('AWS_ENDPOINT'), 28 | 'use_path_style_endpoint' => env('ZIPSTREAM_AWS_PATH_STYLE_ENDPOINT', env('AWS_USE_PATH_STYLE_ENDPOINT', false)), 29 | 'region' => env('ZIPSTREAM_AWS_REGION', env('AWS_DEFAULT_REGION', 'us-east-1')) 30 | ], 31 | 32 | // https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/guide_credentials_anonymous.html 33 | 'aws_anonymous_client' => env('AWS_ANONYMOUS', false) 34 | ]; 35 | -------------------------------------------------------------------------------- /src/Builder.php: -------------------------------------------------------------------------------- 1 | queue = new Queue(); 41 | 42 | foreach ($files as $key => $value) { 43 | if (is_string($key)) { 44 | $this->add($key, $value); 45 | } else { 46 | $this->add($value); 47 | } 48 | } 49 | } 50 | 51 | public function create(?string $name = null, array $files = []): self 52 | { 53 | return (new self($files))->setName($name); 54 | } 55 | 56 | public function setName(?string $name): self 57 | { 58 | $this->outputName = Str::finish($name, ".zip"); 59 | 60 | return $this; 61 | } 62 | 63 | public function has($zipPath): bool 64 | { 65 | return $this->queue->has(trim($zipPath, '/')); 66 | } 67 | 68 | public function add($source, ?string $zipPath = null): self 69 | { 70 | if (!$source instanceof FileContract) { 71 | $source = File::make($source, $zipPath); 72 | } 73 | 74 | $this->queue->addItem($source); 75 | 76 | return $this; 77 | } 78 | 79 | public function addFromDisk($disk, $source, ?string $zipPath): self 80 | { 81 | return $this->add(File::makeFromDisk($disk, $source, $zipPath)); 82 | } 83 | 84 | public function addRaw($content, string $zipPath): self 85 | { 86 | return $this->add(new TempFile($content, $zipPath)); 87 | } 88 | 89 | public function setMeta(array $meta): self 90 | { 91 | $this->meta = collect($meta); 92 | 93 | return $this; 94 | } 95 | 96 | public function getMeta(): Collection 97 | { 98 | return $this->meta ?? collect(); 99 | } 100 | 101 | public function setComment(string $comment): self 102 | { 103 | $this->comment = $comment; 104 | 105 | return $this; 106 | } 107 | 108 | public function getComment(): string 109 | { 110 | return $this->comment; 111 | } 112 | 113 | public function cache($output): self 114 | { 115 | $this->cacheOutputStream = match (true) { 116 | $output instanceof StreamInterface => $output, 117 | $output instanceof FileContract => $output->getWritableStream(), 118 | is_string($output) => File::makeWriteable($output)->getWritableStream(), 119 | default => throw new InvalidArgumentException('Invalid cache output provided'), 120 | }; 121 | 122 | return $this; 123 | } 124 | 125 | public function cacheToDisk($disk, $output): self 126 | { 127 | return $this->cache(File::makeWriteableFromDisk($disk, $output)); 128 | } 129 | 130 | public function saveTo($output): int 131 | { 132 | $this->outputStream = match (true) { 133 | $output instanceof OutputStream => $output, 134 | $output instanceof StreamInterface => new OutputStream($output), 135 | $output instanceof FileContract => $output->getWritableStream(), 136 | is_string($output) => File::makeWriteable(Str::finish($output, "/").$this->getOutputName())->getWritableStream(), 137 | default => throw new InvalidArgumentException('Invalid output provided'), 138 | }; 139 | 140 | return $this->process(); 141 | } 142 | 143 | public function saveToDisk($disk, $path): int 144 | { 145 | return $this->saveTo(File::makeWriteableFromDisk($disk, Str::finish($path, "/").$this->getOutputName())); 146 | } 147 | 148 | public function process(): int 149 | { 150 | $zip = $this->prepare(); 151 | 152 | if ($this->canPredictZipSize()) { 153 | $size = $zip->finish(); 154 | header('Content-Length: '.$size); 155 | header('X-Accel-Buffering: no'); 156 | 157 | event(new ZipStreaming($this, $zip, $size)); 158 | 159 | $zip->executeSimulation(); 160 | } else { 161 | event(new ZipStreaming($this, $zip)); 162 | 163 | $size = $zip->finish(); 164 | } 165 | 166 | if (isset($this->cacheOutputStream)) { 167 | $this->cacheOutputStream->close(); 168 | } 169 | 170 | $this->bytesSent = $size; 171 | 172 | event(new ZipStreamed($this, $zip, $size)); 173 | 174 | if (isset($this->afterProcessing)) { 175 | ($this->afterProcessing)($this, $zip, $size); 176 | } 177 | 178 | return $size; 179 | } 180 | 181 | public function then(Closure $callback): self 182 | { 183 | $this->afterProcessing = $callback; 184 | 185 | return $this; 186 | } 187 | 188 | public function canPredictZipSize(): bool 189 | { 190 | return config('zipstream.predict_size') 191 | && config('zipstream.compression_method') === 'store' 192 | && $this->queue->every->canPredictZipDataSize(); 193 | } 194 | 195 | public function getFingerprint(): string 196 | { 197 | return md5( 198 | $this->queue->map->getFingerprint()->sort()->implode('') 199 | . $this->getOutputName() 200 | . $this->getComment() 201 | . serialize($this->getMeta()->sort()->toArray()) 202 | ); 203 | } 204 | 205 | public function getOutputName(): string 206 | { 207 | return $this->outputName ?? 'download.zip'; 208 | } 209 | 210 | public function getFinalSize(): int 211 | { 212 | return $this->bytesSent; 213 | } 214 | 215 | public function response(): StreamedResponse 216 | { 217 | return new StreamedResponse(function () { 218 | $this->process(); 219 | }, 200); 220 | } 221 | 222 | public function toResponse($request): StreamedResponse 223 | { 224 | return $this->response(); 225 | } 226 | 227 | protected function prepare(): ZipStream 228 | { 229 | $zip = new ZipStream( 230 | operationMode: $this->canPredictZipSize() ? OperationMode::SIMULATE_STRICT : OperationMode::NORMAL, 231 | comment: $this->getComment(), 232 | outputStream: $this->getOutputStream(), 233 | outputName: $this->getOutputName(), 234 | flushOutput: true, 235 | ); 236 | 237 | $this->queue->each->prepare($zip); 238 | 239 | return $zip; 240 | } 241 | 242 | protected function getOutputStream(): StreamInterface 243 | { 244 | if (!isset($this->outputStream)) { 245 | $this->outputStream = new OutputStream(fopen('php://output', 'wb')); 246 | } 247 | 248 | if (isset($this->cacheOutputStream)) { 249 | $this->outputStream->cacheTo($this->cacheOutputStream); 250 | } 251 | 252 | return $this->outputStream; 253 | } 254 | } -------------------------------------------------------------------------------- /src/Contracts/FileContract.php: -------------------------------------------------------------------------------- 1 | zip = $zip; 26 | $this->expected = $expected; 27 | $this->actual = $actual; 28 | } 29 | } -------------------------------------------------------------------------------- /src/Events/ZipStreamed.php: -------------------------------------------------------------------------------- 1 | source = $source; 37 | $this->zipPath = $zipPath ?? $this->getDefaultZipPath(); 38 | $this->options = $options; 39 | } 40 | 41 | public static function make(string $source, ?string $zipPath = null): static 42 | { 43 | if (Str::startsWith($source, "s3://")) { 44 | return new S3File($source, $zipPath); 45 | } 46 | 47 | if (Str::startsWith($source, "http") && filter_var($source, FILTER_VALIDATE_URL)) { 48 | return new HttpFile($source, $zipPath); 49 | } 50 | 51 | if (Str::startsWith($source, "/") || preg_match('/^\w:[\/\\\\]/', $source) || file_exists($source)) { 52 | return new LocalFile($source, $zipPath); 53 | } 54 | 55 | return new TempFile($source, $zipPath); 56 | } 57 | 58 | /** 59 | * @throws UnsupportedSourceDiskException 60 | */ 61 | public static function makeFromDisk($disk, string $source, ?string $zipPath = null): static 62 | { 63 | if(!$disk instanceof FilesystemAdapter) { 64 | $disk = Storage::disk($disk); 65 | } 66 | 67 | if($disk instanceof AwsS3V3Adapter) { 68 | return S3File::make( 69 | "s3://" . Arr::get($disk->getConfig(), "bucket") . "/" . $disk->path($source), 70 | $zipPath 71 | )->setS3Client($disk->getClient()); 72 | } 73 | 74 | if($disk->getAdapter() instanceof LocalFilesystemAdapter) { 75 | return new LocalFile( 76 | $disk->path($source), 77 | $zipPath 78 | ); 79 | } 80 | 81 | throw new UnsupportedSourceDiskException("Unsupported disk type"); 82 | } 83 | 84 | public static function makeWriteable(string $source): S3File|LocalFile 85 | { 86 | if (Str::startsWith($source, "s3://")) { 87 | return new S3File($source); 88 | } 89 | 90 | return new LocalFile($source); 91 | } 92 | 93 | public static function makeWriteableFromDisk($disk, string $source): S3File|LocalFile 94 | { 95 | if(!$disk instanceof FilesystemAdapter) { 96 | $disk = Storage::disk($disk); 97 | } 98 | 99 | if($disk instanceof AwsS3V3Adapter) { 100 | return S3File::make( 101 | "s3://" . Arr::get($disk->getConfig(), "bucket") . "/" . $disk->path($source) 102 | )->setS3Client($disk->getClient()); 103 | } 104 | 105 | return new LocalFile( 106 | $disk->path($source) 107 | ); 108 | } 109 | 110 | public function getName(): string 111 | { 112 | return basename($this->getZipPath()); 113 | } 114 | 115 | public function getSource(): string 116 | { 117 | return $this->source; 118 | } 119 | 120 | protected function getDefaultZipPath(): string 121 | { 122 | return basename($this->getSource()); 123 | } 124 | 125 | public function getZipPath(): string 126 | { 127 | $path = ltrim(preg_replace('|/{2,}|', '/', $this->zipPath), '/'); 128 | 129 | return config('zipstream.ascii_filenames') 130 | ? Str::ascii($path) 131 | : $path; 132 | } 133 | 134 | public function setZipPath(string $zipPath): self 135 | { 136 | $this->zipPath = $zipPath; 137 | 138 | return $this; 139 | } 140 | 141 | public function setComment(string $comment): self 142 | { 143 | $this->comment = $comment; 144 | 145 | return $this; 146 | } 147 | 148 | public function getComment(): string 149 | { 150 | return $this->comment; 151 | } 152 | 153 | public function getReadableStream(): StreamInterface 154 | { 155 | if (!isset($this->readStream)) { 156 | $this->readStream = $this->buildReadableStream(); 157 | } 158 | 159 | return $this->readStream; 160 | } 161 | 162 | public function getWritableStream(): OutputStream 163 | { 164 | if (!isset($this->writeStream)) { 165 | $this->writeStream = $this->buildWritableStream(); 166 | } 167 | 168 | return $this->writeStream; 169 | } 170 | 171 | abstract protected function buildReadableStream(): StreamInterface; 172 | 173 | abstract protected function buildWritableStream(): OutputStream; 174 | 175 | public function getFilesize(): int 176 | { 177 | if (!isset($this->filesize)) { 178 | $this->filesize = $this->calculateFilesize(); 179 | } 180 | 181 | return $this->filesize; 182 | } 183 | 184 | public function setFilesize(int $filesize): self 185 | { 186 | $this->filesize = $filesize; 187 | 188 | return $this; 189 | } 190 | 191 | abstract public function canPredictZipDataSize(): bool; 192 | 193 | abstract protected function calculateFilesize(): int; 194 | 195 | public function getFingerprint(): string 196 | { 197 | return md5($this->getSource().$this->getZipPath().$this->getFilesize().$this->getComment()); 198 | } 199 | 200 | public function setOption($name, $value): static 201 | { 202 | $this->options[$name] = $value; 203 | 204 | return $this; 205 | } 206 | 207 | public function getOption($name, $default = null) 208 | { 209 | return Arr::get($this->options, $name, $default); 210 | } 211 | 212 | public function compressionMethod() 213 | { 214 | $default = config('zipstream.compression_method') === 'deflate' 215 | ? CompressionMethod::DEFLATE 216 | : CompressionMethod::STORE; 217 | 218 | return $this->getOption('compressionMethod', $default); 219 | } 220 | 221 | public function prepare(ZipStream $zip): void 222 | { 223 | $zip->addFileFromCallback( 224 | fileName: $this->getZipPath(), 225 | callback: fn () => $this->getReadableStream(), 226 | comment: $this->getComment(), 227 | compressionMethod: $this->compressionMethod(), 228 | exactSize: $this->getFilesize() 229 | ); 230 | } 231 | } 232 | -------------------------------------------------------------------------------- /src/Models/HttpFile.php: -------------------------------------------------------------------------------- 1 | getHeaders(); 19 | 20 | if (!array_key_exists(self::HEADER_CONTENT_LENGTH, $headers)) { 21 | return false; 22 | } 23 | 24 | if(is_array($headers[self::HEADER_CONTENT_LENGTH])){ 25 | return end($headers[self::HEADER_CONTENT_LENGTH]); 26 | } 27 | 28 | return $headers[self::HEADER_CONTENT_LENGTH]; 29 | } 30 | 31 | protected function buildReadableStream(): StreamInterface 32 | { 33 | return Utils::streamFor(fopen($this->getSource(), 'r')); 34 | } 35 | 36 | protected function buildWritableStream(): OutputStream 37 | { 38 | throw new NotWritableException(); 39 | } 40 | 41 | public function canPredictZipDataSize(): bool 42 | { 43 | return (isset($this->filesize) || array_key_exists(self::HEADER_CONTENT_LENGTH, $this->getHeaders())); 44 | } 45 | 46 | protected function getHeaders(): array 47 | { 48 | if (!isset($this->headers)) { 49 | $this->headers = array_change_key_case(get_headers($this->getSource(), 1)); 50 | } 51 | 52 | return $this->headers; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /src/Models/LocalFile.php: -------------------------------------------------------------------------------- 1 | getSource()); 14 | } 15 | 16 | protected function buildReadableStream(): StreamInterface 17 | { 18 | return Utils::streamFor(fopen($this->getSource(), 'r')); 19 | } 20 | 21 | protected function buildWritableStream(): OutputStream 22 | { 23 | if(!is_dir(dirname($this->getSource()))) { 24 | mkdir(dirname($this->getSource()), 0777, true); 25 | } 26 | 27 | return new OutputStream(fopen($this->getSource(), 'w')); 28 | } 29 | 30 | public function canPredictZipDataSize(): bool 31 | { 32 | return true; 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Models/S3File.php: -------------------------------------------------------------------------------- 1 | buildReadableStream(); 24 | $size = $stream->getSize(); 25 | $stream->close(); 26 | 27 | return $size; 28 | } 29 | 30 | public function setS3Client(S3Client $client): self 31 | { 32 | $this->client = $client; 33 | 34 | return $this; 35 | } 36 | 37 | public function getS3Client(): S3Client 38 | { 39 | if (!isset($this->client)) { 40 | $this->client = app('zipstream.s3client'); 41 | } 42 | 43 | return $this->client; 44 | } 45 | 46 | protected function buildReadableStream(): StreamInterface 47 | { 48 | $this->getS3Client()->registerStreamWrapper(); 49 | 50 | return Utils::streamFor(fopen($this->getSource(), 'r')); 51 | } 52 | 53 | protected function buildWritableStream(): OutputStream 54 | { 55 | $this->getS3Client()->registerStreamWrapper(); 56 | 57 | return new OutputStream(fopen($this->getSource(), 'w')); 58 | } 59 | 60 | public function canPredictZipDataSize(): bool 61 | { 62 | return true; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/Models/TempFile.php: -------------------------------------------------------------------------------- 1 | getSource()); 23 | } 24 | 25 | protected function buildReadableStream(): StreamInterface 26 | { 27 | return Utils::streamFor($this->getSource()); 28 | } 29 | 30 | protected function buildWritableStream(): OutputStream 31 | { 32 | throw new NotWritableException(); 33 | } 34 | 35 | public function canPredictZipDataSize(): bool 36 | { 37 | return true; 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/OutputStream.php: -------------------------------------------------------------------------------- 1 | stream = Utils::streamFor($stream); 20 | } 21 | 22 | public function cacheTo(StreamInterface $stream): self 23 | { 24 | $this->cached = $stream; 25 | 26 | return $this; 27 | } 28 | 29 | public function write($string): int 30 | { 31 | $result = $this->stream->write($string); 32 | 33 | $this->cached()?->write($string); 34 | 35 | return $result; 36 | } 37 | 38 | public function close(): void 39 | { 40 | $this->stream->close(); 41 | 42 | $this->cached()?->close(); 43 | } 44 | 45 | public function cached(): ?StreamInterface 46 | { 47 | return $this->cached; 48 | } 49 | } -------------------------------------------------------------------------------- /src/Queue.php: -------------------------------------------------------------------------------- 1 | has($this->queueKey($file->getZipPath())) && config('zipstream.conflict_strategy') === 'rename') { 13 | $file->setZipPath($this->uniqueZipPath($file->getZipPath())); 14 | } 15 | 16 | if (!$this->has($this->queueKey($file->getZipPath())) || config('zipstream.conflict_strategy') === 'replace') { 17 | $this->put($this->queueKey($file->getZipPath()), $file); 18 | } 19 | 20 | // We are either done, or we had a conflict and config is set to 'skip' (or some invalid value which we'll ignore) 21 | 22 | return $this; 23 | } 24 | 25 | protected function queueKey($zipPath): string 26 | { 27 | return config('zipstream.case_insensitive_conflicts') 28 | ? strtolower($zipPath) 29 | : $zipPath; 30 | } 31 | 32 | protected function uniqueZipPath(string $zipPath): string 33 | { 34 | $dirname = trim(pathinfo($zipPath, PATHINFO_DIRNAME), '.'); 35 | $filename = pathinfo($zipPath, PATHINFO_FILENAME); 36 | $extension = rtrim('.' . pathinfo($zipPath, PATHINFO_EXTENSION), '.'); 37 | 38 | $i = 0; 39 | 40 | do { 41 | $i++; 42 | $path = ltrim($dirname . DIRECTORY_SEPARATOR . $filename . '_' . $i . $extension, DIRECTORY_SEPARATOR); 43 | } while ($this->has($this->queueKey($path))); 44 | 45 | return $path; 46 | } 47 | } -------------------------------------------------------------------------------- /src/ZipStreamServiceProvider.php: -------------------------------------------------------------------------------- 1 | name('zipstream')->hasConfigFile(); 13 | 14 | $this->app->singleton('zipstream.builder', Builder::class); 15 | 16 | $this->app->singleton('zipstream.s3client', function($app) { 17 | $config = $app['config']->get('zipstream.aws'); 18 | 19 | if(!count(array_filter($config['credentials']))) { 20 | unset($config['credentials']); 21 | } 22 | 23 | if($app['config']->get('zipstream.aws_anonymous_client')) { 24 | $config['credentials'] = false; 25 | } 26 | 27 | return new \Aws\S3\S3Client($config); 28 | }); 29 | } 30 | 31 | public function provides(): array 32 | { 33 | return ['zipstream.builder', 'zipstream.s3client']; 34 | } 35 | } 36 | --------------------------------------------------------------------------------