cult3

How to create a Zip Archive in PHP

Jan 04, 2016

Table of contents:

  1. The ZipArchive class
  2. Setting up the files
  3. Creating the ZipArchive object
  4. Iterating through the files
  5. Conclusion

Zip is well recognised and is pretty much a universal filetype for sending multiple files together in one go.

All good web applications should allow its users to export their data in a common format.

But if your application has more than just data, this can get tricky.

For example, if you project management application also includes file uploads, images, or data that doesn’t fit into a spreadsheet, you need a way to package up all of the data and the user’s file uploads into one export.

Creating a zip archive is the perfect solution to this problem.

Zip archives are easy to open and understood by even less technology savvy users.

In today’s tutorial we will be looking at creating a Zip Archive in PHP.

The ZipArchive class

A little known aspect of the PHP programming language is the ZipArchive class.

This class allows you to create Zip archives and provides a number of different methods for doing so.

Have a quick browse through the documentation to get yourself acquainted.

We’re not going to be using every method of the class today, but it’s a good idea to get a sense of what is possible.

Setting up the files

So firstly we need to set up the files to create the zip.

In this tutorial I’m going to have an existing directory which I want to turn into a zip archive. This directory can have multiple sub directories.

I’m going to assume you are using Laravel for this tutorial, so I’ll be using some Laravel specific functions.

Firstly we need to grab the name of the directory that we want to turn into a Zip Archive:

$path = storage_path(sprintf("app/%s", $directory));

Next we need to generate a filename for the Zip Archive we will be creating:

$filename = sprintf("%s.zip", $path);

Creating the ZipArchive object

Now that we have the path to the directory and the name of the zip archive to create we can actually instantiate a new instance of the ZipArchive class:

$zip = new ZipArchive();
$zip->open($filename, ZipArchive::CREATE);

Here I’m instantiating the new object, and then calling the open method and passing the filename and a flag to tell the object we want to create a new archive.

Iterating through the files

Next we need to grab all of the files in the directory and iterate through them.

To do that, we can create a new RecursiveIteratorIterator and pass it an instance of RecursiveDirectoryIterator.

$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::LEAVES_ONLY
);

Now that we can iterate through the files we can add each one to the Zip archive in turn:

foreach ($files as $name => $file) {
    if (!$file->isDir()) {
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($path) + 1);
        $zip->addFile($filePath, $relativePath);
    }
}

Finally we can call the close method on the ZipArchive object to close the archive:

$zip->close();

Conclusion

The ability to create Zip Archives is incredibly useful. Whenever you need to send multiple files in one go it’s a good idea to bundle them up as a zip archive.

This is perfect if you need to allow your users to export their data and uploads all in one go.

Zip Archives are a universally recognised format and so it makes a lot of sense to go down this route.

Fortunately, PHP makes it really easy to create Zip Archives!

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.