How to Zip and Unzip files in PHP
How to Zip and Unzip files in PHP

Learn how to zip and unzip a file in PHP by Monty Shokeen.

This tutorial focuses on how to automate file compression and extraction (zipping and unzipping) using PHP extensions.

The principal characteristics and functionalities described are centered around the PHP ZipArchive class:

1. Benefits of File Compression

Compressing files when transferring them over the internet offers several advantages:

  • Reduced Size: The total size of files is significantly lowered.

  • Bandwidth Saving: Compression helps save bandwidth.

  • Faster Downloads: Users benefit from faster download speeds.

  • Automation: Using PHP functions eliminates the tiresome process of manual compression.

2. Core Archive Management (Opening and Flags)

To start, a new ZipArchive instance is created, followed by calling the open($filename, [$flags]) method. Key optional flags determine how the archive is handled:

  • ZipArchive::CREATE: Creates a new archive if one does not exist.

  • ZipArchive::OVERWRITE: Overwrites existing contents in the specified archive.

  • ZipArchive::EXCL: Results in an error if the archive already exists.

  • ZipArchive::RDONLY: Allows opening the archive in read-only mode (available since PHP 7.4.3).

  • Using combinations (e.g., ZipArchive::OVERWRITE|ZipArchive::CREATE) allows overwriting an existing archive while simultaneously creating a new one if none is found.

3. Adding Files (Compression)

Once the archive is opened, files can be added:

  • Individual Files: The addFile($filename, $localname, $start, $length) method adds a file from a given path to the archive. The $localname parameter sets the file's name inside the archive.

  • Multiple Files/Directory Matching: The addGlob($pattern, $flags, $options) method is used to archive multiple files (e.g., all .pdf or .png files).

    • The $options parameter (an associative array) controls the directory structure inside the archive.

    • Options include add_path (prefixing the file's local path) and removeallpath (removing everything from the path except the file name, adding it to the archive root).

  • After adding files, the close() method is called to save changes and close the archive.

4. Extracting Content

The extractTo($destination, $entries) method is used to extract archive contents:

  • It can extract everything inside the archive (if the second parameter is omitted) or specific files.

  • When extracting specific files, the proper path of the file inside the archive must be specified.

  • The directory and file structure are preserved during the extraction process.

5. Archive Modification and Control

The ZipArchive class provides further control over the archived contents:

  • Counting Files: The number of files can be counted using the count() method or the numFiles property.

  • Renaming: The renameName($oldname, $newname) function changes a file's name within the archive.

  • Deleting: The deleteName() method can delete an individual file or an entire directory from the archive.