PHP Code to Open Zip Files and Extract the Contents

computerGirl on sofa

I recently was working on a project for a dynamic website that presented a huge collection of files available for download. All of the files (over 25,000) were stored in the ubiquitous ZIP format.  This is great for reducing the amount of disk space required, however it can make it challenging to work with them.  What we wanted to do was allow the visitors to the site to be able to review the contents of the file, and view any of the files that are contained within the ZIP file.  PHP has some handy functions that allow you to manipulate ZIP files, however they are not well documented.  Although fairly straight-forward, I am including some example code here that will allow you to quickly copy and paste it for your requirements.

The core routines include zip_open() that opens the ZIP file for use.  zip_read then allows you to transverse the ZIP directory to identify the files contained within.   The zip_entry routines provide additional details about the file enclosed, and zip_entry_open() provides the door that allows you to open a specific file and then zip_entry_read allows you to extract the contained file.   On our site, the contained files were mostly text files that could be easily displayed.  Simply iterate through the file collecting the contents into a temporary variable.  If displaying on a webpage, use the handy nl2lbr() function to convert line feeds into HTML line breaks.

All in all, PHP’s built-in ZIP functions are a handy tool, a few simple lines of code will allow you to easily manipulate ZIP files and allow individual files to be viewed.  Leave a comment if you have any questions or suggestions.  You can see the final site at The Programmer’s Corner.

Code that will allow you to open a ZIP file, iterate through the directory, and retrieve the files contained within.

   $zip = zip_open('PCorner/' . $_GET['category'] . '/' . $_GET['file']);
    while ($zip_entry = zip_read($zip)) {
        $output.= '<tr class="' . $class . '">';
        $output.= '<td>';
        $file = strtoupper(basename(zip_entry_name($zip_entry)));
        $size = zip_entry_filesize($zip_entry);
        $csize = zip_entry_compressedsize($zip_entry);
        $type = zip_entry_compressionmethod($zip_entry);
    }

Code that will allow you to open a ZIP file, find a specific file, and extract it for viewing, or further manipulation.

   $zip = zip_open('PCorner/' . $_GET['category'] . '/' . $_GET['file']);
        while ($zip_entry = zip_read($zip)) {
            $file = basename(zip_entry_name($zip_entry));
            if (strtoupper($file) == strtoupper($_GET['operation'])) {
                if (!zip_entry_open($zip, $zip_entry)) {
                    die('');
                }
                while ($data = zip_entry_read($zip_entry)) {
                    $output.= nl2br($data);
                }

            }
        }
   echo $output;

Leave a Reply

Your email address will not be published. Required fields are marked *