Download the PHP package hanfy/batch-zip-stream without Composer

On this page you can find all versions of the php package hanfy/batch-zip-stream. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package batch-zip-stream

Batch ZIP Stream Engine

A production-grade batch ZIP streaming implementation in PHP that creates ZIP archives incrementally across multiple executions while streaming output to abstract writable streams.

Table of Contents

  1. Overview
  2. Architecture
  3. Installation
  4. Quick Start
  5. Stream-Agnostic API
  6. Core Concepts
  7. API Reference
  8. State Persistence
  9. Error Handling
  10. ZIP64 Support
  11. Encryption Support
  12. Extensibility
  13. Security Considerations
  14. Performance
  15. File Structure

Overview

Problem Statement

Creating large ZIP archives in web environments presents several challenges:

Solution

This library provides a batch-based ZIP creation engine that:

Memory Characteristics

File Count State File Size Memory Usage
1,000 ~500 bytes ~1 MB
10,000 ~500 bytes ~1 MB
100,000 ~500 bytes ~1 MB
1,000,000 ~500 bytes ~1 MB

State file size remains constant regardless of archive size because file entries are stored in a separate append-only file that is streamed, never fully loaded into memory.


Architecture

Class Responsibilities

Data Flow


Installation

Requirements

Via Composer (Recommended)

You can install the library via Composer:

Then include the Composer autoloader in your script:

Manual Installation (No Composer)

If you are not using Composer, download/clone this repository and include the built-in autoloader:


Quick Start

Simple Single-Batch Usage

Multi-Batch Usage with Persistence

Streaming Large Files

Large Archive (1M+ Files)


Stream-Agnostic API

The architecture supports any output stream, not just files. This enables:

Creating Sessions with Custom Streams

Using a Stream Factory

Using a Pre-Created Stream

In-Memory ZIP Creation

Adding Files from Streams

The addFileFromStream() method accepts any ReadableStreamInterface:

Available Session Methods

Method Description
startSession($sessionId) Start or resume a session, returns session ID
getWriter() Get the BatchZipWriter instance
addFile($path, $sourcePath, $compressionMethod, $encryptionMethod, $password) Add file from local filesystem with optional compression, encryption, and custom password
addFileFromString($path, $content, $compressionMethod, $encryptionMethod, $password) Add file from string content with optional compression, encryption, and custom password
addFileFromStream($path, $stream, $compressionMethod, $modificationTime) Add file from any ReadableStreamInterface with optional compression and modification time
addEmptyDirectory($path, $modificationTime) Add an empty directory with optional modification time
saveProgress() Persist current session progress and state
finalize($comment) Write CDR, EOCD, and complete the archive
cleanup() Remove state files and close resources for the current session
abort($reason, $deleteArchive) Mark session as failed, close resources, and optionally delete the partial archive
close() Close session streams and locks without deleting state (useful to pause a batch)
getStream() Get the current output stream
getState() Get the ArchiveState instance
getStats() Get session statistics (count, sizes, path, etc.)
exists($sessionId) Check if a session's state files exist
cleanupOldSessions($maxAgeSeconds) Clean up stale or orphaned sessions older than the specified seconds
listSessions() List all active session IDs

Core Concepts

State Management

The library uses a split-state architecture for memory efficiency:

Critical Rule: Only ArchiveState is persisted between batches. ZIP writers, streams, and compression contexts are NEVER serialized.

File Entries

Each file in the archive is represented by a FileEntry:

Phases

Archives progress through phases:

  1. INITIALIZING: Fresh archive, no files added
  2. ADDING_FILES: Files being added
  3. FINALIZING: Writing Central Directory
  4. COMPLETED: Successfully finished
  5. FAILED: Error occurred

API Reference

BatchZipSession

High-level session manager.

Constructors

Methods

BatchZipWriter

Low-level memory-efficient writer.

Constructor

Methods

Method Description
addFile(string $filename, ReadableStreamInterface $source, int $method = DEFLATE, ?int $mtime = null, int $enc = ENC_NONE, ?string $password = null) Add a file from a stream
addFileFromString(string $filename, string $data, int $method = DEFLATE, ?int $mtime = null, int $enc = ENC_NONE, ?string $password = null) Add a file from string data
finalize(string $comment = '') Write Central Directory and EOCD
close() Close the output stream
getState() Get the current archive state
getEntryStore() Get the entry store
canAddFiles() Check if files can be added
canFinalize() Check if archive can be finalized

State Persistence

File-Based Persistence

Files per Session

Each session creates:

Custom Persistence

Implement StatePersistenceInterface for custom backends (Redis, database, etc.):


Error Handling

Exception Hierarchy

Error Handling Strategy

  1. Any write failure invalidates the entire archive
  2. State is marked as FAILED on any exception
  3. No silent fallbacks - all errors throw exceptions
  4. Central Directory is NEVER written if any file failed

ZIP64 Support

Full ZIP64 support for:

ZIP64 is automatically enabled when needed:

ZIP64 Structures

When ZIP64 is required, the following structures are added:

  1. ZIP64 Extra Field in Central Directory entries
  2. ZIP64 End of Central Directory Record
  3. ZIP64 End of Central Directory Locator

Encryption Support

The library supports securing ZIP archives using standard zip encryption methods:

  1. Traditional PKWARE Encryption (ZipFormat::ENC_TRADITIONAL): Highly compatible across older extraction tools but cryptographically weak.
  2. WinZip AES-256 Strong Encryption (ZipFormat::ENC_AES_256): Industry-standard strong encryption. Requires the openssl PHP extension enabled in PHP.

Setting a Global Password

You can configure a global password during BatchZipSession construction:

Encrypting Files

To encrypt files, pass the encryption method to the addFile or addFileFromString methods.

Overriding Passwords per File

You can override the global password for specific files by passing a custom password as the final parameter.


Extensibility

Custom Output Streams

Implement WritableStreamInterface for cloud storage:

Built-in Streams

The library provides several built-in stream implementations:

MemoryWritableStream

An in-memory buffer for testing or small archives:

FileWritableStream

Standard file output stream:

BufferedWritableStream

Wraps another stream with buffering:

CallbackWritableStream

Invokes a callback on each write:

Custom Input Streams

Implement ReadableStreamInterface for custom sources:


Security Considerations

Path Traversal

Filenames are automatically sanitized:

Validation

The FileEntryStore provides entry-level validation:


Performance

Memory Usage

Tuning Options

Buffered Output

For better I/O performance with small writes:


File Structure



All versions of batch-zip-stream with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
ext-zlib Version *
ext-hash Version *
ext-openssl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package hanfy/batch-zip-stream contains the following files

Loading the files please wait ...