Current Location: Home> Latest Articles> How to Use PHP's zip_read() Function to Read and Extract Files from a ZIP Archive One by One

How to Use PHP's zip_read() Function to Read and Extract Files from a ZIP Archive One by One

M66 2025-06-23

Handling ZIP compressed files in PHP is a common task, especially useful when dealing with uploaded files or automated archiving. The PHP Zip extension provides a complete API for manipulating ZIP files, with zip_read() being the key function for iterating over entries within a ZIP file. This article demonstrates how to use the zip_read() function to read and extract each file within a ZIP archive.

1. Environment Setup

Before starting, make sure your PHP environment has the Zip extension enabled. You can check if it's enabled using the following command:

<?php
phpinfo();
?>

Search for Zip on the page. If it’s listed, it means it’s enabled. If not, you should install and enable the extension based on your operating system.

2. Basic Workflow Overview

The basic steps to use zip_read() are as follows:

  1. Use zip_open() to open the ZIP file.

  2. Use zip_read() to iterate through each entry.

  3. Use zip_entry_name() to get the entry name.

  4. Use zip_entry_open() and zip_entry_read() to read the entry content.

  5. Optionally, write the read content to a file to extract the file.

3. Sample Code Explanation

The following is a complete example of how to read each file in a ZIP archive and extract it to a specified directory:

<?php
<p>$zipFile = 'archive.zip'; // Assuming the file is in the current directory<br>
$extractTo = 'extracted_files/';</p>
<p>// Ensure the target directory exists<br>
if (!is_dir($extractTo)) {<br>
mkdir($extractTo, 0755, true);<br>
}</p>
<p>// Open the ZIP file<br>
$zip = zip_open($zipFile);</p>
<p>if (is_resource($zip)) {<br>
while ($zipEntry = zip_read($zip)) {<br>
$entryName = zip_entry_name($zipEntry);<br>
$entryPath = $extractTo . $entryName;</p>
    if (substr($entryName, -1) === '/') {
        continue;
    }

    // Try to open the entry
    if (zip_entry_open($zip, $zipEntry, "r")) {
        $entryContent = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));

        // Ensure directory structure exists
        $dir = dirname($entryPath);
        if (!is_dir($dir)) {
            mkdir($dir, 0755, true);
        }

        // Write to file
        file_put_contents($entryPath, $entryContent);

        // Optionally: output the current file being processed
        echo "Extracted file: $entryName<br>";
        zip_entry_close($zipEntry);
    }
}
zip_close($zip);
echo "ZIP file processing complete.";

} else {
echo "Unable to open the ZIP file.";
}
?>

4. Important Considerations

  • zip_read() reads one entry at a time, so it needs to be called multiple times in a loop to read all entries.

  • Use zip_entry_filesize() to get the file size for reading the full content.

  • You must use zip_entry_open() to open an entry before reading its content.

  • If the ZIP file contains multiple directory layers, ensure the directory structure exists before extracting files.

5. Application Scenarios

  1. Online Document Extraction: Automatically extract uploaded ZIP archives to a specific server directory.

  2. Batch Image Processing: Extract images from ZIP files uploaded by users, and store them in the /uploads/ directory for subsequent processing.

  3. Data Backup and Restoration: The system automatically packages data backups and supports file-by-file extraction for restoration.

6. Further Reading