{"id":23003752,"url":"https://github.com/stechstudio/laravel-zipstream","last_synced_at":"2025-05-15T12:04:46.348Z","repository":{"id":34635449,"uuid":"180879669","full_name":"stechstudio/laravel-zipstream","owner":"stechstudio","description":"Easily create Zip files on-the-fly and provide a streaming download","archived":false,"fork":false,"pushed_at":"2025-05-14T19:06:16.000Z","size":112,"stargazers_count":435,"open_issues_count":4,"forks_count":63,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-05-14T20:25:27.951Z","etag":null,"topics":["laravel","streaming","streaming-zip","zip"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stechstudio.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2019-04-11T21:20:16.000Z","updated_at":"2025-05-14T19:04:36.000Z","dependencies_parsed_at":"2024-06-18T13:47:42.720Z","dependency_job_id":"52d9258c-6d6c-4d1c-af44-dff5b7d1a92f","html_url":"https://github.com/stechstudio/laravel-zipstream","commit_stats":{"total_commits":76,"total_committers":13,"mean_commits":5.846153846153846,"dds":"0.26315789473684215","last_synced_commit":"c4f511615a9ff63934f21916c4eaedc7c8adb310"},"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-zipstream","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-zipstream/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-zipstream/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stechstudio%2Flaravel-zipstream/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stechstudio","download_url":"https://codeload.github.com/stechstudio/laravel-zipstream/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254227582,"owners_count":22035664,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["laravel","streaming","streaming-zip","zip"],"created_at":"2024-12-15T07:15:13.765Z","updated_at":"2025-05-15T12:04:41.330Z","avatar_url":"https://github.com/stechstudio.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Streaming Zips with Laravel\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/stechstudio/laravel-zipstream.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-zipstream)\n[![Total Downloads](https://img.shields.io/packagist/dt/stechstudio/laravel-zipstream.svg?style=flat-square)](https://packagist.org/packages/stechstudio/laravel-zipstream)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n![Build Status](https://img.shields.io/endpoint?url=https://app.chipperci.com/projects/5cc95e3c-628f-48c6-815c-1f16621c9514/status/master\u0026style=flat-square)\n\nA fast and simple streaming zip file downloader for Laravel. \n\n- Builds zip files from local or S3 file sources, or any other PSR7 stream.\n- 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.\n- Calculates the zip filesize up front for the `Content-Length` header. The user gets an accurate download time estimate in their browser.\n- Built on top of the excellent [ZipStream-PHP](https://github.com/maennchen/ZipStream-PHP) library.\n\n## Upgrading\n\nUpgrading from version 4? Make sure to look at the release notes for version 5. There are some breaking changes.\n\nhttps://github.com/stechstudio/laravel-zipstream/releases/tag/5.0\n\n## Quickstart\n\n#### 1. Install the package\n\n```php\ncomposer require stechstudio/laravel-zipstream\n```\n    \nThe service provider and facade will be automatically wired up.\n\n#### 2. In a controller method call the `create` method on the `Zip` facade\n\n```php\nuse STS\\ZipStream\\Facades\\Zip;\n\nclass ZipController {\n\n    public function build()\n    {\n        return Zip::create(\"package.zip\", [\n            \"/path/to/Some File.pdf\",\n            \"/path/to/Export.xlsx\"       \n        ]);\n    }\n}\n```\n\nThat'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.\n\n## Customize the internal zip path for a file\n\nBy default, any files you add will be stored in the root of the zip, with their original filenames. \n\nYou can customize the filename and even create sub-folders within the zip by providing your files array with key/value pairs:\n\n```php\nZip::create(\"package.zip\", [\n\n    // Will be stored as `Some File.pdf` in the zip\n    \"/path/to/Some File.pdf\",          \n \n    // Will be stored as `Export.xlsx` in the zip\n    \"/path/to/data.xlsx\" =\u003e 'Export.xlsx',\n \n    // Will create a `log` subfolder in the zip and be stored as `log/details.txt`\n    \"/path/to/log.txt\" =\u003e \"log/details.txt\"\n \n]);\n```\n\n## Fluent usage\n\nYou can also provide your files one at a time:\n\n```php\nZip::create(\"package.zip\")\n    -\u003eadd(\"/path/to/Some File.pdf\")\n    -\u003eadd(\"/path/to/data.xlsx\", 'Export.xlsx')\n    -\u003eadd(\"/path/to/log.txt\", \"log/details.txt\");\n```\n\n## Add HTTP file sources\n\nYou 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. \n\n```php\nZip::create(\"package.zip\")\n    -\u003eadd(\"https://...\", \"myfile.pdf\");\n```\n\n## Add raw file data\n\nYou can provide raw data instead of a filepath:\n\n```php\nZip::create(\"package.zip\")\n    -\u003eaddRaw(\"...file contents...\", \"hello.txt\");\n```\n\n## Add from storage disk\n\nYou can add files from a storage disk. Use `addFromDisk` and provide the disk name or disk instance as the first argument:\n\n```php\nZip::create(\"package.zip\")\n    -\u003eaddFromDisk(\"local\", \"file.txt\", \"My File.txt\")\n    -\u003eaddFromDisk(Storage::disk(\"tmp\"), \"important.txt\")\n```\n\n## Support for S3\n\n### Install AWS sdk and configure S3\n\nYou can stream files from S3 into your zip. \n\n1. Install the `aws/aws-sdk-php` package\n\n2. Set up an AWS IAM user with `s3:GetObject` permission for the S3 bucket and objects you intend to zip up.\n\n3. Store your credentials as `AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, and `AWS_DEFAULT_REGION` in your .env file.\n\n### Add S3 files to your zip\n\nProvide `s3://` paths when creating the zip:\n\n```php\nZip::create(\"package.zip\")\n    -\u003eadd(\"s3://bucket-name/path/to/object.pdf\", \"Something.pdf\");\n```\n\nBy 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:\n\n```php\nuse STS\\ZipStream\\Models\\S3File;\n\n// Create your own client however necessary\n$s3 = new Aws\\S3\\S3Client();\n\nZip::create(\"package.zip\")-\u003eadd(\n    S3File::make(\"s3://bucket-name/path/to/object.pdf\")-\u003esetS3Client($s3)\n);\n```\n\nInstead of specifying an absolute `s3://` path, you can use `addFromDisk` and specify a disk that uses the `s3` driver:\n\n```php\nZip::create(\"package.zip\")\n    -\u003eaddFromDisk(\"s3\", \"object.pdf\", \"Something.pdf\");\n```\n\nIn this case the S3 client from the storage disk will be used. \n\n## Specify your own filesizes\n\nIt 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.\n\nLet'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:\n\n```php\nuse STS\\ZipStream\\Models\\File;\n\n// Retrieve file records from the database\n$files = ...;\n\n$zip = Zip::create(\"package.zip\");\n\nforeach($files AS $file) {\n    $zip-\u003eadd(\n        File::make($file-\u003epath, $file-\u003ename)-\u003esetFilesize($file-\u003esize)\n    );\n}\n```\n\nOr if you are adding from an S3 disk:\n\n```php\n$zip-\u003eadd(\n    File::makeFromDisk('s3', $file-\u003ekey, $file-\u003ename)-\u003esetFilesize($file-\u003esize)\n);\n````\n\n## Zip size prediction\n\nBy 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!\n\nThis only works if files are not compressed.\n\nIf you have issues with the zip size prediction you can disable it with `ZIPSTREAM_PREDICT_SIZE=false` in your .env file.\n\n## Configure compression\n\nBy default, this package uses _no_ compression. Why?\n\n1) This makes building the zips super fast, and is light on your CPU\n2) This makes it possible to predict the final zip size as mentioned above.\n\nIf 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.\n\n## Configure conflict strategy\n\nIf 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:\n\n- `ZIPSTREAM_CONFLICT_STRATEGY=skip`: Keep the initial file, skip adding the conflicting file (default)\n- `ZIPSTREAM_CONFLICT_STRATEGY=replace`: Keep the latest file, overwrite previous files at the same path\n- `ZIPSTREAM_CONFLICT_STRATEGY=rename`: Append a number to the conflicting file name, e.g. `file.txt` becomes `file_1.txt`\n\nNote: 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!\n\n## Save Zip to disk\n\nEven 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.\n\nUse the `saveTo` method to write the entire zip to disk immediately. Note that this expects a folder path, the zip name will be appended.\n\n```php\nZip::create(\"package.zip\")\n    // ... add files ...\n    -\u003esaveTo(\"/path/to/folder\");\n```\n\nAnd yes, if you've properly setup and configured S3 you can even save to an S3 bucket/path.\n\n```php\nZip::create(\"package.zip\")\n    // ... add files ...\n    -\u003esaveTo(\"s3://bucket-name/path/to/folder\");\n```\n\nOr you can save to a disk:\n\n```php\nZip::create(\"package.zip\")\n    // ... add files ...\n    -\u003esaveToDisk(\"s3\", \"folder\");\n```\n\n## Caching zip while still streaming download\n\nWhat 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.\n\nUse the `cache` method to provide a cache path. Note this should be the entire path including filename.\n\n```php\nZip::create(\"package.zip\")\n    // ... add files ...\n    -\u003ecache(\"/path/to/folder/some-unique-cache-name.zip\");\n```\n\nOr you can cache to a disk:\n\n```php\nZip::create(\"package.zip\")\n    // ... add files ...\n    -\u003ecacheToDisk(\"s3\", \"folder/some-unique-cache-name.zip\");\n```\n\nYou 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.\n\n## Events\n\n- `STS\\ZipStream\\Events\\ZipStreaming`: Dispatched when a new zip stream begins processing\n- `STS\\ZipStream\\Events\\ZipStreamed`: Dispatched when a zip finishes streaming \n\n## Filename sanitization\n\nBy 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)`.\n\nIf you need to preserve non-ascii characters, you can disable this feature with an `.env` setting:\n\n```env\nZIPSTREAM_ASCII_FILENAMES=false\n```\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-zipstream","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstechstudio%2Flaravel-zipstream","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstechstudio%2Flaravel-zipstream/lists"}