Php zip file stream

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

A fast and memory efficient ZIP library for PHP 7

License

jdwil/zip-stream

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

This library is for generating large zip files with a low memory footprint. The contents of the zip file are never stored in memory at once. Everything is written using streams. This library is for writing zip files only and has no reading capabilities.

The only requirements are PHP 7.0+ and the zlib extension (almost always enabled).

composer require jdwil/zip-stream

$zipStream = ZipStream::forFile('/path/to/file.zip'); // Add a file from disk $zipStream->addFileFromDisk('foo.txt', '/path/to/foo.txt'); // Add a file from a stream $stream = ReadStream::forFile('/path/to/bar.txt'); $zipStream->addFileFromStream('bar.txt', $stream); // Add arbirary data $zipStream->addFile('baz.txt', 'some arbitrary text'); // Always close the Zip Stream $zipStream->close(); 

Dealing with huge data sets

$zipStream = ZipStream::forFile('/path/to/file.zip'); $zipStream->beginFile('foo.txt'); while ($data = $somePdoStatement->fetch()) < $zipStream->addFilePart(implode(',', $data)); > $zipStream->endFile(); $zipStream->close(); 

Stream a ZIP file directly to the user

The file is sent as it is being built, so the download begins immediately for the user.

header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="foo.zip"'); header('Content-Transfer-Encoding: binary'); $zipStream = ZipStream::forFile('php://output'); // Build your zip file $zipStream->close(); 

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

💾 PHP ZIP Streaming Library

License

maennchen/ZipStream-PHP

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Bumps [actions/upload-pages-artifact](https://github.com/actions/upload-pages-artifact) from 1 to 2. — [Release notes](https://github.com/actions/upload-pages-artifact/releases) — [Commits](actions/upload-pages-artifact@v1. v2) — updated-dependencies: — dependency-name: actions/upload-pages-artifact dependency-type: direct:production update-type: version-update:semver-major . Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot]

Git stats

Files

Failed to load latest commit information.

README.md

The main branch is not stable. Please see the releases for a stable version.

A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk. You can directly send it to the user, which is much faster. It can work with S3 buckets or any PSR7 Stream.

Please see the LICENSE file for licensing and warranty information.

Simply add a dependency on maennchen/zipstream-php to your project’s composer.json file if you use Composer to manage the dependencies of your project. Use following command to add the package to your project’s dependencies:

composer require maennchen/zipstream-php

For detailed instructions, please check the Documentation.

// Autoload the dependencies require 'vendor/autoload.php'; // create a new zipstream object $zip = new ZipStream\ZipStream( outputName: 'example.zip', // enable output of HTTP headers sendHttpHeaders: true, ); // create a file named 'hello.txt' $zip->addFile( fileName: 'hello.txt', data: 'This is the contents of hello.txt', ); // add a file named 'some_image.jpg' from a local file 'path/to/image.jpg' $zip->addFileFromPath( fileName: 'some_image.jpg', path: 'path/to/image.jpg', ); // finish the zip stream $zip->finish();
  • Minimum PHP Version: 8.1
  • Only 64bit Architecture is supported.
  • The class ZipStream\Option\Method has been replaced with the enum ZipStream\CompressionMethod .
  • Most clases have been flagged as @internal and should not be used from the outside. If you’re using internal resources to extend this library, please open an issue so that a clean interface can be added & published. The externally available classes & enums are:
    • ZipStream\CompressionMethod
    • ZipStream\Exception*
    • ZipStream\ZipStream
    • The class ZipStream\Option\Archive has been replaced in favor of named arguments in the ZipStream\ZipStream constuctor.
    • The archive options largeFileSize & largeFileMethod has been removed. If you want different compressionMethods based on the file size, you’ll have to implement this yourself.
    • The archive option httpHeaderCallback changed the type from callable to Closure .
    • The archive option zeroHeader has been replaced with the option defaultEnableZeroHeader and can be overridden for every file. Its default value changed from false to true .
    • The archive option statFiles was removed since the library no longer checks filesizes this way.
    • The archive option deflateLevel has been replaced with the option defaultDeflateLevel and can be overridden for every file.
    • The first argument ( name ) of the ZipStream\ZipStream constuctor has been replaced with the named argument outputName .
    • Headers are now also sent if the outputName is empty. If you do not want to automatically send http headers, set sendHttpHeaders to false .
    • The class ZipStream\Option\File has been replaced in favor of named arguments in the ZipStream\ZipStream->addFile* functions.
    • The file option method has been renamed to compressionMethod .
    • The file option time has been renamed to lastModificationDateTime .
    • The file option size has been renamed to maxSize .

    ZipStream-PHP is a collaborative project. Please take a look at the .github/CONTRIBUTING.md file.

    Versions are supported according to the table below.

    Please do not open any pull requests contradicting the current version support status.

    Careful: Always check the README on main for up-to-date information.

    Version New Features Bugfixes Security
    3
    2
    1
    0

    This library aligns itself with the PHP core support. New features and bugfixes will only target PHP versions according to their current status.

    • Paul Duncan pabs@pablotron.org — https://pablotron.org/
    • Jonatan Männchen jonatan@maennchen.ch — https://maennchen.dev
    • Jesse G. Donat donatj@gmail.com — https://donatstudios.com
    • Nicolas CARPi nico-git@deltablot.email — https://www.deltablot.com
    • Nik Barham nik@brokencube.co.uk — https://www.brokencube.co.uk

    This project exists thanks to all the people who contribute. [Contribute].

    Become a financial contributor and help us sustain our community. [Contribute]

    Support this project with your organization. Your logo will show up here with a link to your website. [Contribute]

    Источник

    ZipStream PHP

    A fast and simple streaming zip file downloader for PHP. Using this library will save you from having to write the Zip to disk. You can directly send it to the user, which is much faster. It can work with S3 buckets or any PSR7 Stream.

    Installation

    Simply add a dependency on maennchen/zipstream-php to your project’s composer.json file if you use Composer to manage the dependencies of your project. Use following command to add the package to your project’s dependencies:

    composer require maennchen/zipstream-php 

    If you want to use addFileFromPsr7Stream` ( Psr\Http\Message\StreamInterface ) or use a stream instead of a resource as outputStream , the following dependencies must be installed as well:

    composer require psr/http-message guzzlehttp/psr7 

    If composer install yields the following error, your installation is missing the mbstring extension, either install it or run the follwoing command:

    Your requirements could not be resolved to an installable set of packages. Problem 1 - Root composer.json requires PHP extension ext-mbstring * but it is missing from your system. Install or enable PHP's mbstrings extension. 
    composer require symfony/polyfill-mbstring 

    Usage Intro

    // Autoload the dependencies require 'vendor/autoload.php'; // create a new zipstream object $zip = new ZipStream\ZipStream( outputName: 'example.zip', // enable output of HTTP headers sendHttpHeaders: true, ); // create a file named 'hello.txt' $zip->addFile( fileName: 'hello.txt', data: 'This is the contents of hello.txt', ); // add a file named 'some_image.jpg' from a local file 'path/to/image.jpg' $zip->addFileFromPath( fileName: 'some_image.jpg', path: 'path/to/image.jpg', ); // add a file named 'goodbye.txt' from an open stream resource $filePointer = tmpfile(); fwrite($filePointer, 'The quick brown fox jumped over the lazy dog.'); rewind($filePointer); $zip->addFileFromStream( fileName: 'goodbye.txt', stream: $filePointer, ); fclose($filePointer); // add a file named 'streamfile.txt' from the body of a `guzzle` response // Setup with `psr/http-message` & `guzzlehttp/psr7` dependencies required. $zip->addFileFromPsr7Stream( fileName: 'streamfile.txt', stream: $response->getBody(), ); // finish the zip stream $zip->finish(); 

    You can also add comments, modify file timestamps, and customize (or disable) the HTTP headers. It is also possible to specify the storage method when adding files, the current default storage method is DEFLATE i.e files are stored with Compression mode 0x08.

    Known Issues

    The native Mac OS archive extraction tool prior to macOS 10.15 might not open archives in some conditions. A workaround is to disable the Zip64 feature with the option enableZip64: false . This limits the archive to 4 Gb and 64k files but will allow users on macOS 10.14 and below to open them without issue. See #116.

    The linux unzip utility might not handle properly unicode characters. It is recommended to extract with another tool like 7-zip. See #146.

    It is the responsability of the client code to make sure that files are not saved with the same path, as it is not possible for the library to figure it out while streaming a zip. See #154.

    Источник

    Читайте также:  Java write large files
Оцените статью