wp-php-toolkit / zip
Zip component for WordPress.
Requires
- php: >=7.2
- wp-php-toolkit/bytestream: ^0.7.2
- wp-php-toolkit/filesystem: ^0.7.2
- wp-php-toolkit/http-client: ^0.7.2
Requires (Dev)
- phpunit/phpunit: ^9.5
- dev-trunk
- v0.7.2
- v0.7.1
- v0.7.0
- v0.6.2
- v0.6.1
- v0.6.0
- v0.5.1
- v0.5.0
- v0.4.1
- v0.4.0
- v0.3.1
- v0.3.0
- v0.2.0
- v0.1.5
- v0.1.4
- v0.1.3
- v0.1.2
- v0.1.1
- v0.1.0
- 0.0.19
- 0.0.18
- 0.0.17
- 0.0.16
- v0.0.15
- v0.0.15-alpha
- 0.0.14
- 0.0.13
- 0.0.12
- 0.0.11
- v0.0.8-alpha
- 0.0.7
- v0.0.7-alpha
- 0.0.6
- v0.0.6-alpha
- v0.0.5-alpha
- v0.0.4-alpha
- v0.0.3-alpha
- v0.0.2-alpha
- v0.0.1-alpha
This package is auto-updated.
Last update: 2026-04-30 22:26:13 UTC
README
A pure PHP library for reading and writing ZIP archives without the libzip extension or ZipArchive class. It provides a streaming ZipFilesystem reader that exposes ZIP contents through a standard filesystem interface, and a ZipEncoder that writes ZIP files incrementally. Handles both stored and deflate-compressed entries.
Installation
composer require wp-php-toolkit/zip
Quick Start
use WordPress\ByteStream\ReadStream\FileReadStream; use WordPress\Zip\ZipFilesystem; // Open a ZIP file and read its contents $zip = ZipFilesystem::create( FileReadStream::from_path( 'archive.zip' ) ); // List top-level entries $entries = $zip->ls(); // ['readme.txt', 'src', 'images'] // Read a file $content = $zip->get_contents( 'readme.txt' );
Usage
Reading ZIP Archives
ZipFilesystem implements the Filesystem interface, so you can list directories, check paths, and read files just like a regular filesystem.
use WordPress\ByteStream\ReadStream\FileReadStream; use WordPress\Zip\ZipFilesystem; $zip = ZipFilesystem::create( FileReadStream::from_path( 'book.epub' ) ); // List the root directory $entries = $zip->ls(); // ['mimetype', 'EPUB', 'META-INF'] // List a subdirectory $epub_files = $zip->ls( '/EPUB' ); // ['cover.xhtml', 'css', 'images', 'nav.xhtml', 'package.opf', ...] // Check if a path exists $zip->exists( 'mimetype' ); // true $zip->is_file( 'mimetype' ); // true $zip->is_dir( 'EPUB' ); // true $zip->is_file( 'EPUB' ); // false // Read file contents $mimetype = $zip->get_contents( 'mimetype' ); // "application/epub+zip" $cover = $zip->get_contents( 'EPUB/cover.xhtml' ); // "<?xml version="1.0" encoding="UTF-8"?>..."
Streaming File Reads
For large files inside the archive, use open_read_stream() to read data incrementally instead of loading everything into memory.
use WordPress\ByteStream\ReadStream\FileReadStream; use WordPress\Zip\ZipFilesystem; $zip = ZipFilesystem::create( FileReadStream::from_path( 'archive.zip' ) ); $stream = $zip->open_read_stream( 'large-dataset.csv' ); while ( $bytes = $stream->pull( 4096 ) ) { $chunk = $stream->consume( $bytes ); // Process the chunk... }
Creating ZIP Archives
Use ZipEncoder to build ZIP files from scratch. Write individual files with append_file(), or copy an entire filesystem tree with append_from_filesystem().
use WordPress\ByteStream\MemoryPipe; use WordPress\ByteStream\WriteStream\FileWriteStream; use WordPress\Zip\FileEntry; use WordPress\Zip\ZipDecoder; use WordPress\Zip\ZipEncoder; // Create a new ZIP file $output = FileWriteStream::from_path( 'output.zip', 'truncate' ); $encoder = new ZipEncoder( $output ); // Add a file with no compression $encoder->append_file( new FileEntry( array( 'path' => 'hello.txt', 'compression_method' => ZipDecoder::COMPRESSION_NONE, 'body_reader' => new MemoryPipe( 'Hello, world!' ), ) ) ); // Add a file with deflate compression $encoder->append_file( new FileEntry( array( 'path' => 'data/notes.txt', 'compression_method' => ZipDecoder::COMPRESSION_DEFLATE, 'body_reader' => new MemoryPipe( 'This will be compressed.' ), ) ) ); // Finalize and close $encoder->close(); $output->close_writing();
Copying from One ZIP to Another
Because ZipFilesystem implements the standard Filesystem interface, you can pass it directly to ZipEncoder::append_from_filesystem() to repackage a ZIP archive.
use WordPress\ByteStream\ReadStream\FileReadStream; use WordPress\ByteStream\WriteStream\FileWriteStream; use WordPress\Zip\ZipEncoder; use WordPress\Zip\ZipFilesystem; // Open the source ZIP $source = ZipFilesystem::create( FileReadStream::from_path( 'original.zip' ) ); // Create a new ZIP with the same contents $output = FileWriteStream::from_path( 'copy.zip', 'truncate' ); $encoder = new ZipEncoder( $output ); $encoder->append_from_filesystem( $source ); $encoder->close(); $output->close_writing();
API Reference
ZipFilesystem
| Method | Description |
|---|---|
create( ByteReadStream $reader ) |
Create a filesystem view of a ZIP archive |
ls( $dir = '/' ) |
List entries in a directory |
is_file( $path ) |
Check if a path is a file |
is_dir( $path ) |
Check if a path is a directory |
exists( $path ) |
Check if a path exists |
get_contents( $path ) |
Read an entire file as a string |
open_read_stream( $path ) |
Open a streaming reader for a file |
ZipEncoder
| Method | Description |
|---|---|
__construct( ByteWriteStream $output ) |
Create an encoder that writes to the given stream |
append_file( FileEntry $entry ) |
Add a single file to the archive |
append_from_filesystem( Filesystem $fs, $path ) |
Recursively add files from a filesystem |
close() |
Write the central directory and finalize the archive |
FileEntry
Constructed with an associative array of header fields:
| Field | Description |
|---|---|
path |
File path inside the archive |
body_reader |
A ByteReadStream with the file data |
compression_method |
ZipDecoder::COMPRESSION_NONE or ZipDecoder::COMPRESSION_DEFLATE |
Requirements
- PHP 7.2+
- No external PHP extensions required (no libzip)