Path

ez components / documentation / api reference / 1.0 / archive


eZ Components 1.0

Archive

[ Tutorial ] [ Class tree ] [ Element index ] [ ChangeLog ] [ Credits ]

Introduction

The Archive component provides a generic API for creating and extracting archives. Currently, the archive supports the Tar and Zip format. Compression algorithms, such as GZip or BZip2, are indirectly supported by this component. 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 non-existing Tar or Zip archive. The Archive is designed that it provides methods for extracting entries (files, directories, symbolic links, etc), appending entries, or removing entries on the open archive.
ezcArchiveEntry
The ezcArchiveEntry is returned when an entry (file, directory, etc) is requested from the opened archive. The ezcArchiveEntry provides file information about the file path, it's access rights and whether the file is an directory, symbolic link, hard link, block-file, etc. The owner name, the group name, the last access time are also available.

More information about these classes can be found in the documention 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:

  • Unix V7
  • Ustar
  • POSIX
  • Gnu

Each format can be extracted from the archive. Appending entries to the archive is only available for Unix V7 and Ustar format.

Extracting an Archive comes in two flavors:

The Archive can be treated like an iterator. After opening the archive, it points to the first entry. The iterator can be moved using the 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. include( "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 the 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 uses some date functions and without a time zone PHP may show some warnings.

The gzipped TAR archive is opened using the zlib stream. The while() method iterates over each entry, shows the name, and extracts the entry itself.

The Archive extends from the PHP Iterator class, the example above can be rewritten as:

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

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 library do not support opening a file for reading and writing.

ezcArchive has two methods for appending files:

To replace also the first file, the ezcArchive->truncate() should be used. The next example replaces all entries from an existing Zip archive with the files file1.txt and file2.txt:

  1. <?php
  2. include( "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. ?>

More Information

For more information, see the ezcArchive API documentation.

Last updated: Thu, 31 Jan 2008