summaryrefslogtreecommitdiffstats
path: root/vendor/maennchen/zipstream-php/guides
diff options
context:
space:
mode:
authorAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
committerAnton Luka Šijanec <anton@sijanec.eu>2024-05-27 13:08:29 +0200
commit75160b12821f7f4299cce7f0b69c83c1502ae071 (patch)
tree27e25e4ccaef45f0c58b22831164050d1af1d4db /vendor/maennchen/zipstream-php/guides
parentprvi-commit (diff)
download1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.gz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.bz2
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.lz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.xz
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.tar.zst
1ka-75160b12821f7f4299cce7f0b69c83c1502ae071.zip
Diffstat (limited to '')
-rw-r--r--vendor/maennchen/zipstream-php/guides/ContentLength.rst79
-rw-r--r--vendor/maennchen/zipstream-php/guides/FlySystem.rst33
-rw-r--r--vendor/maennchen/zipstream-php/guides/Nginx.rst16
-rw-r--r--vendor/maennchen/zipstream-php/guides/Options.rst61
-rw-r--r--vendor/maennchen/zipstream-php/guides/PSR7Streams.rst18
-rw-r--r--vendor/maennchen/zipstream-php/guides/StreamOutput.rst33
-rw-r--r--vendor/maennchen/zipstream-php/guides/Symfony.rst126
-rw-r--r--vendor/maennchen/zipstream-php/guides/Varnish.rst22
-rw-r--r--vendor/maennchen/zipstream-php/guides/index.rst85
9 files changed, 473 insertions, 0 deletions
diff --git a/vendor/maennchen/zipstream-php/guides/ContentLength.rst b/vendor/maennchen/zipstream-php/guides/ContentLength.rst
new file mode 100644
index 0000000..e51e692
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/ContentLength.rst
@@ -0,0 +1,79 @@
+Adding Content-Length header
+=============
+
+Adding a ``Content-Length`` header for ``ZipStream`` is not trivial since the
+size is not known beforehand.
+
+The following workaround adds an approximated header:
+
+.. code-block:: php
+
+ class Zip
+ {
+ /** @var string */
+ private $name;
+
+ private $files = [];
+
+ public function __construct($name)
+ {
+ $this->name = $name;
+ }
+
+ public function addFile($name, $data)
+ {
+ $this->files[] = ['type' => 'addFile', 'name' => $name, 'data' => $data];
+ }
+
+ public function addFileFromPath($name, $path)
+ {
+ $this->files[] = ['type' => 'addFileFromPath', 'name' => $name, 'path' => $path];
+ }
+
+ public function getEstimate()
+ {
+ $estimate = 22;
+ foreach ($this->files as $file) {
+ $estimate += 76 + 2 * strlen($file['name']);
+ if ($file['type'] === 'addFile') {
+ $estimate += strlen($file['data']);
+ }
+ if ($file['type'] === 'addFileFromPath') {
+ $estimate += filesize($file['path']);
+ }
+ }
+ return $estimate;
+ }
+
+ public function finish()
+ {
+ header('Content-Length: ' . $this->getEstimate());
+ $options = new \ZipStream\Option\Archive();
+ $options->setSendHttpHeaders(true);
+ $options->setEnableZip64(false);
+ $options->setDeflateLevel(-1);
+ $zip = new \ZipStream\ZipStream($this->name, $options);
+
+ $fileOptions = new \ZipStream\Option\File();
+ $fileOptions->setMethod(\ZipStream\Option\Method::STORE());
+ foreach ($this->files as $file) {
+ if ($file['type'] === 'addFile') {
+ $zip->addFile($file['name'], $file['data'], $fileOptions);
+ }
+ if ($file['type'] === 'addFileFromPath') {
+ $zip->addFileFromPath($file['name'], $file['path'], $fileOptions);
+ }
+ }
+ $zip->finish();
+ exit;
+ }
+ }
+
+It only works with the following constraints:
+
+- All file content is known beforehand.
+- Content Deflation is disabled
+
+Thanks to
+`partiellkorrekt <https://github.com/maennchen/ZipStream-PHP/issues/89#issuecomment-1047949274>`_
+for this workaround. \ No newline at end of file
diff --git a/vendor/maennchen/zipstream-php/guides/FlySystem.rst b/vendor/maennchen/zipstream-php/guides/FlySystem.rst
new file mode 100644
index 0000000..0243f24
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/FlySystem.rst
@@ -0,0 +1,33 @@
+Usage with FlySystem
+===============
+
+For saving or uploading the generated zip, you can use the
+`Flysystem <https://flysystem.thephpleague.com>`_ package, and its many
+adapters.
+
+For that you will need to provide another stream than the ``php://output``
+default one, and pass it to Flysystem ``putStream`` method.
+
+.. code-block:: php
+
+ // Open Stream only once for read and write since it's a memory stream and
+ // the content is lost when closing the stream / opening another one
+ $tempStream = fopen('php://memory', 'w+');
+
+ // Init Options
+ $zipStreamOptions = new Archive();
+ $zipStreamOptions->setOutputStream($tempStream);
+
+ // Create Zip Archive
+ $zipStream = new ZipStream('test.zip', $zipStreamOptions);
+ $zipStream->addFile('test.txt', 'text');
+ $zipStream->finish();
+
+ // Store File (see Flysystem documentation, and all its framework integration)
+ $adapter = new Local(__DIR__.'/path/to/folder'); // Can be any adapter (AWS, Google, Ftp, etc.)
+ $filesystem = new Filesystem($adapter);
+
+ $filesystem->putStream('test.zip', $tempStream)
+
+ // Close Stream
+ fclose($tempStream); \ No newline at end of file
diff --git a/vendor/maennchen/zipstream-php/guides/Nginx.rst b/vendor/maennchen/zipstream-php/guides/Nginx.rst
new file mode 100644
index 0000000..c53d300
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/Nginx.rst
@@ -0,0 +1,16 @@
+Usage with nginx
+=============
+
+If you are using nginx as a webserver, it will try to buffer the response.
+So you'll want to disable this with a custom header:
+
+.. code-block:: php
+ header('X-Accel-Buffering: no');
+ # or with the Response class from Symfony
+ $response->headers->set('X-Accel-Buffering', 'no');
+
+Alternatively, you can tweak the
+`fastcgi cache parameters <https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_buffers>`_
+within nginx config.
+
+See `original issue <https://github.com/maennchen/ZipStream-PHP/issues/77>`_. \ No newline at end of file
diff --git a/vendor/maennchen/zipstream-php/guides/Options.rst b/vendor/maennchen/zipstream-php/guides/Options.rst
new file mode 100644
index 0000000..eabaa6f
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/Options.rst
@@ -0,0 +1,61 @@
+Available options
+===============
+
+Here is the full list of options available to you. You can also have a look at
+``src/Option/Archive.php`` file.
+
+First, an instance of ``ZipStream\Option\Archive`` needs to be created, and
+after that you use setters methods to modify the values.
+
+.. code-block:: php
+ use ZipStream\ZipStream;
+ use ZipStream\Option\Archive as ArchiveOptions;
+
+ require_once 'vendor/autoload.php';
+
+ $opt = new ArchiveOptions();
+
+ // Define output stream (argument is of type resource)
+ $opt->setOutputStream($fd);
+
+ // Set the deflate level (default is 6; use -1 to disable it)
+ $opt->setDeflateLevel(6);
+
+ // Add a comment to the zip file
+ $opt->setComment('This is a comment.');
+
+ // Size, in bytes, of the largest file to try and load into memory (used by addFileFromPath()). Large files may also be compressed differently; see the 'largeFileMethod' option.
+ $opt->setLargeFileSize(30000000);
+
+ // How to handle large files. Legal values are STORE (the default), or DEFLATE. Store sends the file raw and is significantly faster, while DEFLATE compresses the file and is much, much slower. Note that deflate must compress the file twice and is extremely slow.
+ $opt->setLargeFileMethod(ZipStream\Option\Method::STORE());
+ $opt->setLargeFileMethod(ZipStream\Option\Method::DEFLATE());
+
+ // Send http headers (default is false)
+ $opt->setSendHttpHeaders(false);
+
+ // HTTP Content-Disposition. Defaults to 'attachment', where FILENAME is the specified filename. Note that this does nothing if you are not sending HTTP headers.
+ $opt->setContentDisposition('attachment');
+
+ // Set the content type (does nothing if you are not sending HTTP headers)
+ $opt->setContentType('application/x-zip');
+
+ // Set the function called for setting headers. Default is the `header()` of PHP
+ $opt->setHttpHeaderCallback('header');
+
+ // Enable streaming files with single read where general purpose bit 3 indicates local file header contain zero values in crc and size fields, these appear only after file contents in data descriptor block. Default is false. Set to true if your input stream is remote (used with addFileFromStream()).
+ $opt->setZeroHeader(false);
+
+ // Enable reading file stat for determining file size. When a 32-bit system reads file size that is over 2 GB, invalid value appears in file size due to integer overflow. Should be disabled on 32-bit systems with method addFileFromPath if any file may exceed 2 GB. In this case file will be read in blocks and correct size will be determined from content. Default is true.
+ $opt->setStatFiles(true);
+
+ // Enable zip64 extension, allowing very large archives (> 4Gb or file count > 64k)
+ // default is true
+ $opt->setEnableZip64(true);
+
+ // Flush output buffer after every write
+ // default is false
+ $opt->setFlushOutput(true);
+
+ // Now that everything is set you can pass the options to the ZipStream instance
+ $zip = new ZipStream('example.zip', $opt);
diff --git a/vendor/maennchen/zipstream-php/guides/PSR7Streams.rst b/vendor/maennchen/zipstream-php/guides/PSR7Streams.rst
new file mode 100644
index 0000000..4b4ca4b
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/PSR7Streams.rst
@@ -0,0 +1,18 @@
+Usage with PSR 7 Streams
+===============
+
+PSR-7 streams are `standardized streams <https://www.php-fig.org/psr/psr-7/>`_.
+
+ZipStream-PHP supports working with these streams with the function
+``addFileFromPsr7Stream``.
+
+For all parameters of the function see the API documentation.
+
+Example
+---------------
+
+.. code-block:: php
+
+ $stream = $response->getBody();
+ // add a file named 'streamfile.txt' from the content of the stream
+ $zip->addFileFromPsr7Stream('streamfile.txt', $stream);
diff --git a/vendor/maennchen/zipstream-php/guides/StreamOutput.rst b/vendor/maennchen/zipstream-php/guides/StreamOutput.rst
new file mode 100644
index 0000000..1a0495f
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/StreamOutput.rst
@@ -0,0 +1,33 @@
+Stream Output
+===============
+
+Stream to S3 Bucket
+---------------
+
+.. code-block:: php
+ use Aws\S3\S3Client;
+ use Aws\Credentials\CredentialProvider;
+ use ZipStream\Option\Archive;
+ use ZipStream\ZipStream;
+
+ $bucket = 'your bucket name';
+ $client = new S3Client([
+ 'region' => 'your region',
+ 'version' => 'latest',
+ 'bucketName' => $bucket,
+ 'credentials' => CredentialProvider::defaultProvider(),
+ ]);
+ $client->registerStreamWrapper();
+
+ $zipFile = fopen("s3://$bucket/example.zip", 'w');
+
+ $options = new Archive();
+ $options->setEnableZip64(false);
+ $options->setOutputStream($zipFile);
+
+ $zip = new ZipStream(null, $options);
+ $zip->addFile('file1.txt', 'File1 data');
+ $zip->addFile('file2.txt', 'File2 data');
+ $zip->finish();
+
+ fclose($zipFile); \ No newline at end of file
diff --git a/vendor/maennchen/zipstream-php/guides/Symfony.rst b/vendor/maennchen/zipstream-php/guides/Symfony.rst
new file mode 100644
index 0000000..18f9059
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/Symfony.rst
@@ -0,0 +1,126 @@
+Usage with Symfony
+===============
+
+Overview for using ZipStream in Symfony
+--------
+
+Using ZipStream in Symfony requires use of Symfony's ``StreamedResponse`` when
+used in controller actions.
+
+Wrap your call to the relevant ``ZipStream`` stream method (i.e. ``addFile``,
+``addFileFromPath``, ``addFileFromStream``) in Symfony's ``StreamedResponse``
+function passing in any required arguments for your use case.
+
+Using Symfony's ``StreamedResponse`` will allow Symfony to stream output from
+ZipStream correctly to users' browsers and avoid a corrupted final zip landing
+on the users' end.
+
+Example for using ``ZipStream`` in a controller action to zip stream files
+stored in an AWS S3 bucket by key:
+
+.. code-block:: php
+
+ use Symfony\Component\HttpFoundation\StreamedResponse;
+ use Aws\S3\S3Client;
+ use ZipStream;
+
+ //...
+
+ /**
+ * @Route("/zipstream", name="zipstream")
+ */
+ public function zipStreamAction()
+ {
+ //sample test file on s3
+ $s3keys = array(
+ "ziptestfolder/file1.txt"
+ );
+
+ $s3Client = $this->get('app.amazon.s3'); //s3client service
+ $s3Client->registerStreamWrapper(); //required
+
+ //using StreamedResponse to wrap ZipStream functionality for files on AWS s3.
+ $response = new StreamedResponse(function() use($s3keys, $s3Client)
+ {
+ // Define suitable options for ZipStream Archive.
+ $options = new \ZipStream\Option\Archive();
+ $options->setContentType('application/octet-stream');
+ // this is needed to prevent issues with truncated zip files
+ $options->setZeroHeader(true);
+ $options->setComment('test zip file.');
+
+ //initialise zipstream with output zip filename and options.
+ $zip = new ZipStream\ZipStream('test.zip', $options);
+
+ //loop keys - useful for multiple files
+ foreach ($s3keys as $key) {
+ // Get the file name in S3 key so we can save it to the zip
+ //file using the same name.
+ $fileName = basename($key);
+
+ //concatenate s3path.
+ $bucket = 'bucketname'; //replace with your bucket name or get from parameters file.
+ $s3path = "s3://" . $bucket . "/" . $key;
+
+ //addFileFromStream
+ if ($streamRead = fopen($s3path, 'r')) {
+ $zip->addFileFromStream($fileName, $streamRead);
+ } else {
+ die('Could not open stream for reading');
+ }
+ }
+
+ $zip->finish();
+
+ });
+
+ return $response;
+ }
+
+In the above example, files on AWS S3 are being streamed from S3 to the Symfon
+application via ``fopen`` call when the s3Client has ``registerStreamWrapper``
+applied. This stream is then passed to ``ZipStream`` via the
+``addFileFromStream`` function, which ZipStream then streams as a zip to the
+client browser via Symfony's ``StreamedResponse``. No Zip is created server
+side, which makes this approach a more efficient solution for streaming zips to
+the client browser especially for larger files.
+
+For the above use case you will need to have installed
+`aws/aws-sdk-php-symfony <https://github.com/aws/aws-sdk-php-symfony>`_ to
+support accessing S3 objects in your Symfony web application. This is not
+required for locally stored files on you server you intend to stream via
+``ZipStream``.
+
+See official Symfony documentation for details on
+`Symfony's StreamedResponse <https://symfony.com/doc/current/components/http_foundation.html#streaming-a-response>`_
+``Symfony\Component\HttpFoundation\StreamedResponse``.
+
+Note from `S3 documentation <https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/s3-stream-wrapper.html>`_:
+
+ Streams opened in "r" mode only allow data to be read from the stream, and
+ are not seekable by default. This is so that data can be downloaded from
+ Amazon S3 in a truly streaming manner, where previously read bytes do not
+ need to be buffered into memory. If you need a stream to be seekable, you
+ can pass seekable into the stream context options of a function.
+
+Make sure to configure your S3 context correctly!
+
+Uploading a file
+--------
+
+You need to add correct permissions
+(see `#120 <https://github.com/maennchen/ZipStream-PHP/issues/120>`_)
+
+**example code**
+
+
+.. code-block:: php
+
+ $path = "s3://{$adapter->getBucket()}/{$this->getArchivePath()}";
+
+ // the important bit
+ $outputContext = stream_context_create([
+ 's3' => ['ACL' => 'public-read'],
+ ]);
+
+ fopen($path, 'w', null, $outputContext); \ No newline at end of file
diff --git a/vendor/maennchen/zipstream-php/guides/Varnish.rst b/vendor/maennchen/zipstream-php/guides/Varnish.rst
new file mode 100644
index 0000000..952d287
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/Varnish.rst
@@ -0,0 +1,22 @@
+Usage with Varnish
+=============
+
+Serving a big zip with varnish in between can cause random stream close.
+This can be solved by adding attached code to the vcl file.
+
+To avoid the problem, add the following to your varnish config file:
+
+.. code-block::
+ sub vcl_recv {
+ # Varnish can’t intercept the discussion anymore
+ # helps for streaming big zips
+ if (req.url ~ "\.(tar|gz|zip|7z|exe)$") {
+ return (pipe);
+ }
+ }
+ # Varnish can’t intercept the discussion anymore
+ # helps for streaming big zips
+ sub vcl_pipe {
+ set bereq.http.connection = "close";
+ return (pipe);
+ }
diff --git a/vendor/maennchen/zipstream-php/guides/index.rst b/vendor/maennchen/zipstream-php/guides/index.rst
new file mode 100644
index 0000000..a68fa94
--- /dev/null
+++ b/vendor/maennchen/zipstream-php/guides/index.rst
@@ -0,0 +1,85 @@
+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.
+
+.. toctree::
+
+ index
+ Symfony
+ Options
+ StreamOutput
+ FlySystem
+ PSR7Streams
+ Nginx
+ Varnish
+ ContentLength
+
+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:
+
+.. code-block:: sh
+ composer require maennchen/zipstream-php
+
+Usage Intro
+---------------
+
+Here's a simple example:
+
+.. code-block:: php
+
+ // Autoload the dependencies
+ require 'vendor/autoload.php';
+
+ // enable output of HTTP headers
+ $options = new ZipStream\Option\Archive();
+ $options->setSendHttpHeaders(true);
+
+ // create a new zipstream object
+ $zip = new ZipStream\ZipStream('example.zip', $options);
+
+ // create a file named 'hello.txt'
+ $zip->addFile('hello.txt', 'This is the contents of hello.txt');
+
+ // add a file named 'some_image.jpg' from a local file 'path/to/image.jpg'
+ $zip->addFileFromPath('some_image.jpg', 'path/to/image.jpg');
+
+ // add a file named 'goodbye.txt' from an open stream resource
+ $fp = tmpfile();
+ fwrite($fp, 'The quick brown fox jumped over the lazy dog.');
+ rewind($fp);
+ $zip->addFileFromStream('goodbye.txt', $fp);
+ fclose($fp);
+
+ // 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 <https://github.com/maennchen/ZipStream-PHP/issues/146>`_.
+
+The linux ``unzip`` utility might not handle properly unicode characters.
+It is recommended to extract with another tool like
+`7-zip <https://www.7-zip.org/>`_.
+See `#146 <https://github.com/maennchen/ZipStream-PHP/issues/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 <https://github.com/maennchen/ZipStream-PHP/issues/154>`_. \ No newline at end of file