Create ZIP Archive with Directory

This section provides a tutorial example on how to create a ZIP archive with a directory and adds files to that directory using the ZipArchive class.

If you want create a directory structure inside the ZIP archive so that files can be added to the archive to different locations, you need to two methods from the ZipArchive class:

1. Create directory inside ZIP archive using addEmptyDir() method. For example, the following code creates a directory and a sub-directory.

$zip->addEmptyDir('src');
$zip->addEmptyDir('src/gif');

2. Add file to archive with local path name using addFile() method. For example, the following code adds 'dot.gif' to the 'src/gif' sub-directory in the archive.

$zip->addFile('dot.gif', 'src/gif/dot.gif');

Here is an example script that creates a new ZIP archive with a directory and adds some files to that directory in the archive.

<?php
#  zip-with-directory.php
#- Copyright 2009 (c) HerongYang.com. All Rights Reserved.

  $zip = new ZipArchive;
  echo "Creating test.zip:\n";
  $rc = $zip->open('/tmp/test.zip',ZipArchive::CREATE);
  if ($rc === TRUE) {
    echo "   test.zip opened\n";

    $dir = "srcDir";
    if ($zip->addEmptyDir($dir) === TRUE) {
      echo "   $dir added\n";
    } else {
      echo "   Failed to add $dir.\n";
    }

    $file = 'dot.gif';
    if ($zip->addFile($file, "$dir/$file") === TRUE) {
      echo "   $file added\n";
    } else {
      echo "   Failed to add $file.\n";
    }

    $file = 'zip-create-new.php';
    if ($zip->addFile($file, "$dir/$file") === TRUE) {
      echo "   $file added\n";
    } else {
      echo "   Failed to add $file.\n";
    }

    $zip->close();
  } else {
    echo "   Failed to create test.zip with error: $rc\n";
  }
?>

Let's try it:

herong$ php zip-with-directory.php
Creating test.zip:
   test.zip opened
   srcDir added
   dot.gif added
   zip-create-new.php added

herong$ unzip -l /tmp/test.zip
Archive:  /tmp/test.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  06-04-2020 15:53   srcDir/
       43  06-04-2020 15:37   srcDir/dot.gif
      815  06-04-2020 15:17   srcDir/zip-create-new.php
---------                     -------
      858                     3 files

Table of Contents

 About This Book

 Introduction and Installation of PHP

 Managing PHP Engine and Modules on macOS

 Managing PHP Engine and Modules on CentOS

 cURL Module - Client for URL

 DOM Module - Parsing HTML Documents

 GD Module - Manipulating Images and Pictures

 MySQLi Module - Accessing MySQL Server

 OpenSSL Module - Cryptography and SSL/TLS Toolkit

 PCRE Module - Perl Compatible Regular Expressions

 SOAP Module - Creating and Calling Web Services

 SOAP Module - Server Functions and Examples

Zip Module - Managing ZIP Archive Files

 The ZipArchive Class

 Create New ZIP Archive

 Extract Files from ZIP Archive

Create ZIP Archive with Directory

 Create ZIP Archive for Download

 References

 Full Version in PDF/EPUB