How to Extract Zip file using PHP

Learn how to extract a zip file using the PHP ZipArchive class. In the previous post, we have explained how to create ZIP file in PHP.

ZipArchive is a simple PHP utility class that provides us the functionality to create a zip file or to extract the zip file dynamically on the server.

It has a extractTo() method, which extracts the zip file contents to the specified destination. The ZipArchive class also has a lot of other methods and properties to help you get more information about the archive before extracting all its contents.

Syntax:

bool ZipArchive::extractTo(string $destination, mixed $entries);

Parameters:

destination: This parameter can be used to specify the zip file.
entries: This parameter can be used to specify a single file name that is to be extracted.

Example: Extract Zip File in PHP

Create a ExtractorZip Class

The Extractor class helps to extract zip files on the server using PHP ZipArchive.

ExtractorZip.class.php

<?php
/**
 * Class ExtractorZip
 
 */
class ExtractorZip {

    /**
     * Checks file extension and call zip extractor functions.
     *
     * @param $archive
     * @param $destination
     */
    public static function extract($archive, $destination){
        $ext = pathinfo($archive, PATHINFO_EXTENSION);
        // check file is zip or not
        if($ext == 'zip')
        {
			$res = self::extractZipArchive($archive, $destination);
			
			return $res;
		}
    }
    
    /**
     * Decompress/extract a zip archive using ZipArchive.
     *
     * @param $archive
     * @param $destination
     */
    public static function extractZipArchive($archive, $destination){
        // Check if webserver supports unzipping.
        if(!class_exists('ZipArchive')){
            $GLOBALS['status'] = array('error' => 'Your PHP version does not support unzip functionality.');
            return false;
        }
    
        $zip = new ZipArchive;
    
        // Check if archive is readable.
        if($zip->open($archive) === TRUE){
            // Check if destination is writable
            if(is_writeable($destination . '/')){
                $zip->extractTo($destination);
                $zip->close();
                $GLOBALS['status'] = array('success' => 'Files extracted successfully');
                return true;
            }else{
                $GLOBALS['status'] = array('error' => 'Directory not writeable by server.');
                return false;
            }
        }else{
            $GLOBALS['status'] = array('error' => 'Cannot read .zip file.');
            return false;
        }
    }
    
}

Create index.php to Extract ZIP in PHP

Use ExtractorZip class to extract zip file to the specified path using PHP.

  • Include and initialize the ExtractorZip class.
  • Mention the directory path of the zip file, that you want to extract.
  • Mention the destination to extract the ZIP file on the server.
  • Call the extractZipMethod() function of ExtractorZip class.

index.php

<?php

// Include and initialize ExtractorZip class
require_once 'ExtractorZip.class.php';
$extractor = new ExtractorZip;

// Path of archive file
$archivePath = '/path/to/archive.zip';

// Destination path
$destPath = '/destination/dir/';

// Extract archive file
$extract = $extractor->extractZipMethod($archivePath, $destPath);

if($extract){
    echo $GLOBALS['status']['success'];
}else{
    echo $GLOBALS['status']['error'];
}

Similar Posts