|
1 | 1 | Adding Content-Length header
|
2 | 2 | =============
|
3 | 3 |
|
4 |
| -Adding a ``Content-Length`` header for ``ZipStream`` is not trivial since the |
5 |
| -size is not known beforehand. |
| 4 | +Adding a ``Content-Length`` header for ``ZipStream`` can be achieved by |
| 5 | +using the options ``SIMULATION_STRICT`` or ``SIMULATION_LAX`` in the |
| 6 | +``operationMode`` parameter. |
6 | 7 |
|
7 |
| -The following workaround adds an approximated header: |
| 8 | +In the ``SIMULATION_STRICT`` mode, ``ZipStream`` will not allow to calculate the |
| 9 | +size based on reading the whole file. ``SIMULATION_LAX`` will read the whole |
| 10 | +file if neccessary. |
8 | 11 |
|
9 |
| -.. code-block:: php |
| 12 | +``SIMULATION_STRICT`` is therefore useful to make sure that the size can be |
| 13 | +calculated efficiently. |
10 | 14 |
|
11 |
| - use ZipStream\CompressionMethod; |
| 15 | +.. code-block:: php |
| 16 | + use ZipStream\OperationMode; |
12 | 17 | use ZipStream\ZipStream;
|
13 | 18 |
|
14 |
| - class Zip |
15 |
| - { |
16 |
| - private $files = []; |
17 |
| -
|
18 |
| - public function __construct( |
19 |
| - private readonly string $name |
20 |
| - ) { } |
21 |
| -
|
22 |
| - public function addFile( |
23 |
| - string $name, |
24 |
| - string $data, |
25 |
| - ): void { |
26 |
| - $this->files[] = ['type' => 'addFile', 'name' => $name, 'data' => $data]; |
27 |
| - } |
28 |
| -
|
29 |
| - public function addFileFromPath( |
30 |
| - string $name, |
31 |
| - string $path, |
32 |
| - ): void { |
33 |
| - $this->files[] = ['type' => 'addFileFromPath', 'name' => $name, 'path' => $path]; |
| 19 | + $zip = new ZipStream( |
| 20 | + operationMode: OperationMode::SIMULATE_STRICT, // or SIMULATE_LAX |
| 21 | + defaultEnableZeroHeader: false, |
| 22 | + sendHttpHeaders: true, |
| 23 | + outputStream: $stream, |
| 24 | + ); |
| 25 | +
|
| 26 | + // Normally add files |
| 27 | + $zip->addFile('sample.txt', 'Sample String Data'); |
| 28 | +
|
| 29 | + // Use addFileFromCallback and exactSize if you want to defer opening of |
| 30 | + // the file resource |
| 31 | + $zip->addFileFromCallback( |
| 32 | + 'sample.txt', |
| 33 | + exactSize: 18, |
| 34 | + callback: function () { |
| 35 | + return fopen('...'); |
34 | 36 | }
|
| 37 | + ); |
35 | 38 |
|
36 |
| - public function getEstimate(): int { |
37 |
| - $estimate = 22; |
38 |
| - foreach ($this->files as $file) { |
39 |
| - $estimate += 76 + 2 * strlen($file['name']); |
40 |
| - if ($file['type'] === 'addFile') { |
41 |
| - $estimate += strlen($file['data']); |
42 |
| - } |
43 |
| - if ($file['type'] === 'addFileFromPath') { |
44 |
| - $estimate += filesize($file['path']); |
45 |
| - } |
46 |
| - } |
47 |
| - return $estimate; |
48 |
| - } |
49 |
| -
|
50 |
| - public function finish() |
51 |
| - { |
52 |
| - header('Content-Length: ' . $this->getEstimate()); |
53 |
| - $zip = new ZipStream( |
54 |
| - outputName: $this->name, |
55 |
| - SendHttpHeaders: true, |
56 |
| - enableZip64: false, |
57 |
| - defaultCompressionMethod: CompressionMethod::STORE, |
58 |
| - ); |
59 |
| -
|
60 |
| - foreach ($this->files as $file) { |
61 |
| - if ($file['type'] === 'addFile') { |
62 |
| - $zip->addFile( |
63 |
| - fileName: $file['name'], |
64 |
| - data: $file['data'], |
65 |
| - ); |
66 |
| - } |
67 |
| - if ($file['type'] === 'addFileFromPath') { |
68 |
| - $zip->addFileFromPath( |
69 |
| - fileName: $file['name'], |
70 |
| - path: $file['path'], |
71 |
| - ); |
72 |
| - } |
73 |
| - } |
74 |
| - $zip->finish(); |
75 |
| - } |
76 |
| - } |
77 |
| -
|
78 |
| -It only works with the following constraints: |
79 |
| - |
80 |
| -- All file content is known beforehand. |
81 |
| -- Content Deflation is disabled |
| 39 | + // Read resulting file size |
| 40 | + $size = $zip->finish(); |
| 41 | + |
| 42 | + // Tell it to the browser |
| 43 | + header('Content-Length: '. $size); |
| 44 | + |
| 45 | + // Execute the Simulation and stream the actual zip to the client |
| 46 | + $zip->executeSimulation(); |
82 | 47 |
|
83 |
| -Thanks to |
84 |
| -`partiellkorrekt <https://github.com/maennchen/ZipStream-PHP/issues/89#issuecomment-1047949274>`_ |
85 |
| -for this workaround. |
0 commit comments