Table of Contents
The Archive component provides a generic API for creating and extracting archives. Currently, the Archive component supports the Tar and Zip formats. Compression algorithms, such as GZip or BZip2, are indirectly supported. The stream wrappers from PHP should be used to handle compressed archives.
The following list sums up the most important classes:
More information about these classes can be found in the documentation of the class itself.
The following examples demonstrate how to use the Archive component.
The Tar format has more than one standard. The most common formats are:
The Archive component can extract from any of these formats. Appending entries to the archive is only available for the Unix V7 and Ustar formats.
Extracting entries can occur in two ways:
An ezcArchive object can be used like an iterator. After opening the file, it points to the first entry. The iterator can be moved using ezcArchive->next() and ezcArchive->rewind() to move to the next entry or go back to the first entry.
The next example demonstrates how to extract an entire archive file-by-file:
First, tutorial_autoload.php is included. The included file loads the correct php files for the Archive package. Hereafter the time zone is set to "UTC". The Archive component uses some date functions and might therefore produce warnings if the time zone is not specified.
The gzipped Tar archive is opened using the zlib stream. The while() method iterates over each entry, showing the name and extracting the entry itself.
The Archive component extends from the PHP Iterator class, thus the above example can be rewritten as follows:
Please be aware that by default archive files are opened in read/write mode. In order to prevent that, you can set an option to open the archive in read-only mode. This also prevents the modify and create timestamps of the file to be preserved. The following example shows that:
Unfortunately, it is not yet possible to directly append files to a gzipped or bzipped Tar archive. The ZLib and BZip2 libraries do not support opening a file for reading and writing.
ezcArchive has two methods for appending files:
To replace the first file as well, use ezcArchive->truncate(). The next example replaces all entries from an existing Zip archive with the files file1.txt and file2.txt:
You need to append a slash '/' to the end of the directory name that is added to an archive.
The next example replaces all entries from an existing Zip archive with the 'directory' folder and the 'file.txt' file:
Using the function ezcBase::walkRecursive() a directory tree can be added to an archive.
The next example shows how to browse a directory tree and add all the files and directories inside to an archive:
The ArchiveContext class will hold the archive object which is passed to the callback function findRecursiveCallback. The appendRecursive function sets up the context object (of class ArchiveContext) and calls the findRecursiveCallback function. In the findRecursiveCallback function the current file or directory is appended to the archive object inside the context.