Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Managing ZIP-archives with PHP

Monday, August 22nd, 2016 by Servage

zip-archiveManaging ZIP archives became easier when the ZipArchive class was introduced in PHP 5.2. ZipArchive allows you to read, extract and create your own compressed archives. In this article we will go through two of these operations: reading and extracting ZIP archives. This is useful for websites where you offer downloads of data that may benefit from being packaged. I.e. a collection of images or other files, to aggregated dynamically and downloaded as a single archive.

Reading ZIP files

Whether reading or extracting a ZIP archive, the first thing is to create a new ZipArchive object. This can be done as follows: $zip = new ZipArchive(). After you have a new ZipArchive object, you can use a method called open which is a member function of the ZipArchive object. Give a file name as the first argument when calling the method. For instance: $zip->open(“my_archive.zip”).

Opening a ZIP file is a simple task in PHP. But why would you want to open a ZIP file? As you might have guessed, you have to open a ZIP archive before you can extract it. That’s not the only thing you can do with ZIPs though. You can also get the number of files in an archive with the numFiles member variable, for example $zip->numFiles. You can also use the statIndex method in a foreach loop to get the names and sizes of all files in an archive. The statIndex takes a file index as a parameter. For example, to get details about the first file in a ZIP archive, you can use: $zip->statIndex(0). This returns an associative array of the file details.

Extracting an archive

Probably the most important method of the ZipArchive class is the extractTo() method. As the name suggests, it allows you to extract a ZIP archive to a specific destination. The syntax is as follows: $zip->extractTo(“/path/to/destination/folder”) where $zip is an opened ZipArchive object. This will extract all files recursively from the archive to the destination folder.

Closing an archive

In programming, it is important to destroy a resource when it is no longer needed. This applies to sockets, files objects and more. Speaking of files, this means closing them, and it also applies to opened ZIP archives.

So how do we close the ZIP file we opened earlier? It is as simple as opening a file. Instead of using the open() member function, there is a function called close() that does the opposite: it closes the ZIP archive. It is also just as simple to use as the open() function. The syntax for it is: $zip->close() where again, $zip is an opened ZipArchive object. Running the close() function returns true if the file was closed successfully and false if any error related to closing the file has occurred.

Managing ZIP-archives with PHP, 4.0 out of 5 based on 3 ratings
Categories: Guides & Tutorials

Keywords:

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.