Sample code for 30+ languages & platforms
PHP Requires Chilkat v11.0.0+

Unzip Selected Files from a Zip Archive

See more Zip Examples

Demonstrates how to iterate over the files contained within a .zip and unzip specific files.

Chilkat PHP Downloads

PHP
<?php

use Chilkat2\Zip;
use Chilkat2\ZipEntry;

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

$zip = new Zip();

try {
    $zip->OpenZip('my_files.zip');
} catch (\Chilkat2\Exception $e) {
    echo $e->getMessage(), "\n";
    exit;
}

$unzipDir = '/temp/unzipDir';

// Get the number of files and directories in the .zip
$n = $zip->NumEntries;

$entry = new ZipEntry();

$i = 0;
while ($i < $n) {
    $zip->EntryAt($i, $entry);
    if (!$entry->IsDirectory) {
        // (the filename may include a path)
        echo $entry->FileName, "\n";

        // Your application may choose to unzip this entry
        // based on the filename.
        // If the entry should be unzipped, then call Extract(unzipDir)
        try {
            $entry->Extract($unzipDir);
        } catch (\Chilkat2\Exception $e) {
            echo $e->getMessage(), "\n";
            exit;
        }
    }

    $i = $i + 1;
}