summaryrefslogtreecommitdiffstats
path: root/vendor/maennchen/zipstream-php/guides/ContentLength.rst
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/ContentLength.rst
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
1 files changed, 79 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