Zeta Components - high quality PHP components

eZ Components - Archive

Introduction

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.

Class overview

The following list sums up the most important classes:

ezcArchive
This class provides the main API for accessing or creating a Tar or Zip archive. ezcArchive provides methods for extracting entries (files, directories, symbolic links and so on), appending entries and removing entries.
ezcArchiveEntry
The ezcArchiveEntry class is returned when an entry (such as a file or directory) is requested from the opened archive. ezcArchiveEntry provides entry information about the path, its access rights and whether the entry is a directory, a symbolic link, a hard link, a block-file and so on. The owner name, the group name and the last access time are also available.

More information about these classes can be found in the documentation of the class itself.

Usage

The following examples demonstrate how to use the Archive component.

Extracting a Tar-archive

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. date_default_timezone_set"UTC" );
  4. // Open the gzipped TAR archive.
  5. $archive ezcArchive::open"compress.zlib:///tmp/my_archive.tar.gz" );
  6. while( $archive->valid() )
  7. {
  8.     // Returns the current entry (ezcArchiveEntry).
  9.     $entry $archive->current();
  10.     // ezcArchiveEntry has an __toString() method.
  11.     echo $entry"\n";
  12.     // Extract the current archive entry to /tmp/target_location/
  13.     $archive->extractCurrent"/tmp/target_location/" );
  14.     $archive->next();
  15. }
  16. ?>

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. $archive ezcArchive::open"compress.zlib:///tmp/my_archive.tar.gz" );
  4. // The foreach method calls internally the iterator methods.
  5. foreach( $archive as $entry )
  6. {
  7.     echo $entry"\n";
  8.     $archive->extractCurrent"/tmp/target_location/" );
  9. }
  10. ?>

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. $options = new ezcArchiveOptions( array( 'readOnly' => true ) );
  4. $archive ezcArchive::open(
  5.     "compress.zlib:///tmp/my_archive.tar.gz"null$options );
  6. // The foreach method calls internally the iterator methods.
  7. foreach( $archive as $entry )
  8. {
  9.     echo $entry"\n";
  10.     $archive->extractCurrent"/tmp/target_location/" );
  11. }
  12. ?>

Appending files to an archive

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. date_default_timezone_set"UTC" );
  4. $archive ezcArchive::open"/tmp/my_archive.zip" );
  5. $archive->truncate();
  6. $filesToAppend[] = "/tmp/file1.txt";
  7. $filesToAppend[] = "/tmp/file2.txt";
  8. // The second parameter specifies prefix. The prefix is normally not included 
  9. // in the archive.
  10. $archive->appendToCurrent$filesToAppend"/tmp/" );
  11. ?>

Appending directories to an archive

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. date_default_timezone_set"UTC" );
  4. $archive ezcArchive::open"/tmp/my_archive.zip" );
  5. $archive->truncate();
  6. $filesToAppend[] = "/tmp/directory/";
  7. $filesToAppend[] = "/tmp/file.txt";
  8. // The second parameter specifies prefix. The prefix is normally not included 
  9. // in the archive.
  10. $archive->appendToCurrent$filesToAppend"/tmp/" );
  11. ?>

Appending a directory tree to an archive

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:

  1. <?php
  2. require_once 'tutorial_autoload.php';
  3. date_default_timezone_set"UTC" );
  4. class ArchiveContext extends ezcBaseFileFindContext
  5. {
  6.     public $archive;
  7.     public $prefix;
  8. }
  9. function findRecursiveCallbackezcBaseFileFindContext $context$sourceDir$fileName$fileInfo )
  10. {
  11.     $path "{$sourceDir}/{$fileName}";
  12.     if ( is_dir$path ) )
  13.     {
  14.         $path .= '/';
  15.     }
  16.     $context->archive->append( array( $path ), $context->prefix );
  17. }
  18. function appendRecursive$archive$sourceDir$prefix )
  19. {
  20.     $context = new ArchiveContext();
  21.     $context->archive $archive;
  22.     $context->prefix $prefix;
  23.     ezcBaseFile::walkRecursive$sourceDir, array(), array(), 'findRecursiveCallback'$context );
  24. }
  25. $archive ezcArchive::open"my_archive.zip"ezcArchive::ZIP );
  26. $archive->truncate();
  27. // the 2nd parameter is the directory, the 3rd parameter is the prefix
  28. appendRecursive$archive'/tmp/directory/''/tmp/directory/' );
  29. ?>

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.

More Information

For more information, see the ezcArchive API documentation.