Commit f9fcb58
Add ZipStreamWriter to stream-write zip archives on PHP 7.2+ (#103)
This pull request introduces enhancements to the `ZipStreamWriter`
class, enabling efficient file streaming and compression when writing
ZIP archives. Key features include:
1. **Streaming File into ZIP**: Implements a method to stream a file
from disk directly into a ZIP archive without loading the entire file
into memory.
2. **Handling Central Directory**: Implements a method to write the
central directory entries and the end-of-central-directory record,
finalizing the ZIP archive.
3. **Deflate Compression**: Supports optional deflate compression for
files being added to the ZIP archive.
Closes #88
### Major Changes
1. **`writeFileFromPath` Method**:
- Reads the source file from disk in two passes:
- First pass: Computes CRC32, uncompressed size, and compressed size
without buffering the entire file.
- Second pass: Streams the file's compressed data directly into the ZIP
archive.
- Supports deflate compression using `deflate_add`.
2. **`flush_directory_index` Method**:
- Collects and writes central directory entries to the ZIP stream.
- Writes the end-of-central-directory record to finalize the ZIP
structure.
### Example Usage
```php
use WordPress\Zip\ZipStreamWriter;
// File paths
$sourcePathOnDisk = '/path/to/source/file.txt';
$targetPathInZip = 'archive/file.txt';
// Create a file pointer for the output ZIP file
$zipFilePointer = fopen('output.zip', 'wb');
// Instantiate the ZipStreamWriter
$zipWriter = new ZipStreamWriter($zipFilePointer);
// Write a file from the filesystem into the ZIP archive
$zipWriter->writeFileFromPath($sourcePathOnDisk, $targetPathInZip, true); // Use 'false' for no compression
// Finalize the ZIP file
$zipWriter->flush_directory_index();
fclose($zipFilePointer);
```
### How to Test
1. Clone the repository and checkout the branch with these changes.
2. Ensure PHPUnit is installed.
3. Run the test suite using the command:
```sh
vendor/bin/phpunit
```
4. Verify that all tests pass, indicating the functionality works as
expected.
### Notes
- This implementation aims to handle large files efficiently by
streaming data in chunks.
- The `flush_directory_index` method should be called once all file
entries have been written to ensure the ZIP archive is finalized
correctly.
Feel free to provide any feedback or request further modifications as
needed.
---------
Co-authored-by: Michael Reichardt <30837295+reimic@users.noreply.github.com>1 parent 87afea1 commit f9fcb58
File tree
6 files changed
+565
-5
lines changed- .github/workflows
- src/WordPress/Zip
- tests/unit/zip
6 files changed
+565
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36 | 36 | | |
37 | 37 | | |
38 | 38 | | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
21 | 21 | | |
22 | 22 | | |
23 | 23 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
7 | 9 | | |
8 | 10 | | |
9 | 11 | | |
| |||
18 | 20 | | |
19 | 21 | | |
20 | 22 | | |
21 | | - | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
| |||
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
41 | 41 | | |
42 | 42 | | |
43 | 43 | | |
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
47 | 47 | | |
48 | | - | |
49 | 48 | | |
50 | 49 | | |
51 | 50 | | |
| |||
65 | 64 | | |
66 | 65 | | |
67 | 66 | | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
68 | 76 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
6 | 14 | | |
7 | 15 | | |
8 | 16 | | |
| |||
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | | - | |
| 69 | + | |
62 | 70 | | |
63 | 71 | | |
64 | 72 | | |
| |||
82 | 90 | | |
83 | 91 | | |
84 | 92 | | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
85 | 102 | | |
0 commit comments