Web20 University

How To Use PHP Zipstream to create dynamic ZIP Files

Get Up to 65% Off PHP Hosting and Free Domains!

TL;DR

PHP ZipStream is a powerful library that allows you to create ZIP files in realtime without the need for temporary files and excessive memory usage (ZIP files created in a stream).

This article will get you up and running with the library with some examples of how to use it.

What is PHP ZipStream?

PHP ZipStream is an open-source library that provides a simple and efficient way to create ZIP archives in real-time. Unlike traditional methods that require creating temporary files on the server, ZipStream generates the ZIP file directly in the output stream. This approach offers several advantages:

  1. Memory Efficiency: ZipStream doesn’t load the entire archive into memory, making it suitable for creating large ZIP files.
  2. Real-time Generation: Files are added to the archive as they’re streamed, allowing for dynamic content creation.
  3. No Temporary Files: By avoiding temporary file creation, ZipStream reduces disk I/O and improves performance.

How to Use PHP ZipStream

Let’s walk through the process of using PHP ZipStream in your projects:

1. Installation

First, you’ll need to install the ZipStream library. The easiest way is to use Composer:

composer require maennchen/zipstream-php

2. Basic Usage

Here’s a simple example of how to create a ZIP file with ZipStream:

<?php
require_once 'vendor/autoload.php';

use ZipStream\ZipStream;

// Create a new ZipStream object
$zip = new ZipStream('example.zip');

// Add files to the zip file
$zip->addFile('file1.txt', 'This is the contents of file 1');
$zip->addFile('file2.txt', 'This is the contents of file 2');

// Finish the zip stream
$zip->finish();

This code creates a ZIP file named ’example.zip’ containing two text files.

3. Adding Files from Disk

You can also add existing files from your server:

$zip->addFileFromPath('document.pdf', '/path/to/document.pdf');

4. Creating Directories

ZipStream allows you to create directories within your ZIP archive:

$zip->addFile('directory/file3.txt', 'This file is inside a directory');

5. Large File Handling

For large files, you can use a callback function to stream the content:

$zip->addFileFromCallback('large_file.dat', function($zipStream) {
    // Stream your file content here
    $handle = fopen('path/to/large/file.dat', 'rb');
    while (!feof($handle)) {
        $zipStream->write(fread($handle, 8192));
    }
    fclose($handle);
}, [
    'size' => filesize('path/to/large/file.dat')
]);

6. Configuring Options

ZipStream offers various options to customize your ZIP creation:

$options = new ZipStream\Option\Archive();
$options->setZeroHeader(true);
$options->setEnableZip64(false);

$zip = new ZipStream('customized.zip', $options);

Conclusion

PHP ZipStream provides a powerful and efficient way to create ZIP archives dynamically in your PHP applications. Its stream-based approach makes it an excellent choice for handling large files or generating archives with dynamic content. By eliminating the need for temporary files and reducing memory usage, ZipStream can significantly improve the performance of your file delivery systems.

Whether you’re creating downloadable reports, bundling user-generated content, or archiving data for backup purposes, PHP ZipStream offers a flexible and reliable solution. Give it a try in your next project and experience the benefits of on-the-fly ZIP creation!

Remember to check the official documentation for more advanced features and options as you integrate ZipStream into your applications.

Get Up to 65% Off PHP Hosting and Free Domains!