Path

ez components / documentation / api reference / 1.0.1 / archive


eZ Components 1.0.1

Archive: ezcArchive

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

Class: ezcArchive

The ezcArchive class provides the common interface for reading and writing the archive formats Tar and Zip. [source]

Implemented Interfaces

  • Iterator (internal interface)

ezcArchive provides the main API for reading and writing to an archive. The archive itself can be compressed with GZip or BZip2 and will be handled transparently.
The open() method creates a new archive instance. For existing archives, ezcArchive determines the correct archive format by the mime-type and returns an instance of a subclass handling this format. New archives should force a format type via a parameter in the open() method.
The instance of an ezcArchive class is also an iterator, which points to the first file in the archive by default. Moving this pointer can be done via the iterator methods: rewind(), next(), valid(), key(), and current(). This iterator is defined as an object iterator and allows, for example, the foreach statement to iterate through the files.
Extra methods that operate on the current iterator are: extractCurrent() and appendToCurrent(). Which can be used, respectively, to extract the files and to append a new file to the archive.
The following example will open an existing tar.gz file and will append each file to a zip archive:
 1.  $tar ezcArchive::open"/tmp/archive.tar.gz" );
 2.  $newZip ezcArchive::open"/tmp/new_archive.zip"ezcArchive::ZIP );
 3.  
 4.  foreach $tar as $entry )
 5.  {
 6.     // $entry contains the information of the current entry in the archive.
 7.         $tar->extractCurrent"/tmp/" );
 8.     $newZip->appendToCurrent$entry->getPath()"/tmp/" );
 9.     $newZip->next();
10.  }
In order to extract an entire archive at once, use the extract() method.

Descendents

Child Class Description
ezcArchiveV7Tar The ezcArchiveV7Tar class implements the Tar v7 archive format.
ezcArchiveZip The ezcArchiveZip class implements the Zip archive format.

Constants

BZIP2 = 30 BZIP2 compression format.
GZIP = 20 Gnu ZIP compression format.
TAR = 0 Normal tar archive.
TAR_GNU = 4 GNU tar archive.
TAR_PAX = 3 PAX tar archive.
TAR_USTAR = 2 USTAR tar archive.
TAR_V7 = 1 Tar version 7 archive.
ZIP = 10 ZIP archive.

Member Variables

protected bool $completed = false
Is true when the archive is read until the end, otherwise false.
protected array(ezcArchiveEntry) $entries
Stores the entries read from the archive.
protected int $entriesRead = 0
The number of entries currently read from the archive.
protected ezcArchiveFile $file = null
Direct access to the archive file.
protected int $fileNumber = 0
The entry or file number to which the iterator points.

Method Summary

public abstract bool algorithmCanWrite( )
Returns true if writing to the archive is implemented, otherwise false.
public void append( $files, $prefix, $entries )
Append a file or directory to the end of the archive. Multiple files or directory can be added to the archive when an array is used as input parameter.
public abstract void appendToCurrent( $files, $prefix )
Appends a file to the archive after the current entry.
protected void createDefaultDirectory( $file )
Creates all the directories needed to create the file $file.
protected static ezcArchive createInstance( $archiveName, $type )
Returns an instance of the archive with the given type.
public ezcArchiveEntry current( )
Returns the current ezcArchiveEntry if it is valid, otherwise false is returned.
public void extract( $target, [$keepExisting = false] )
Extract entries from the archive to the target directory.
public bool extractCurrent( $target, [$keepExisting = false], $target , $keepExisting )
Extract the current entry to which the iterator points.
public abstract int getAlgorithm( )
Returns the algorithm that is used currently.
public array(string) getListing( )
Returns an array that lists the content of the archive.
public static ezcArchive getTarInstance( $blockFile, $type, $type )
Open a tar instance.
public static ezcArchive getZipInstance( $charFile )
Open a zip instance. This method is made public for testing purposes, and should not be used.
public bool isEmpty( )
Returns true if the current archive is empty, otherwise false.
public bool isWritable( )
Returns true if it is possible to write to the archive, otherwise false.
public int key( )
Returns the current key, entry number, if it is valid, otherwise false is returned.
public ezcArchiveEntry next( )
Forwards the iterator to the next entry.
public static ezcArchive open( $archiveName, [$forceType = null] )
Returns a new ezcArchive instance.
public void rewind( )
Rewinds the iterator to the first entry.
public bool seek( $offset, [$whence = SEEK_SET] )
Search for the entry number.
public abstract void truncate( [$fileNumber = 0], $fileNumber )
Truncate the archive to $fileNumber of files.
public bool valid( )
Returns true if the iterator points to a valid entry, otherwise false.
protected abstract void writeCurrentDataToFile( $targetPath )
Writes the file data from the current entry to the given file.
public mixed __get( $name )
Returns the property $name.
public void __set( $name, $value )
Sets the property $name to $value.
public string __toString( )
Returns a string which represents all the entries from the archive.

Methods

algorithmCanWrite

bool algorithmCanWrite( )
Returns true if writing to the archive is implemented, otherwise false.

See also:

ezcArchive::isWritable().

Redefined in descendants as

Method Description
ezcArchiveV7Tar::algorithmCanWrite()  
ezcArchiveUstarTar::algorithmCanWrite()  
ezcArchivePaxTar::algorithmCanWrite()  
ezcArchiveGnuTar::algorithmCanWrite()  
ezcArchiveZip::algorithmCanWrite()  

append

void append( $files, $prefix, string|array(string) $entries )
Append a file or directory to the end of the archive. Multiple files or directory can be added to the archive when an array is used as input parameter.

Parameters

Name Type Description
$entries string|array(string) Add the files and or directories to the archive.
$files  
$prefix  

Throws

ClassDescription
ezcArchiveWriteException if one of the files cannot be written to the archive.
ezcFileReadException if one of the files cannot be read from the local filesystem.

See also:

ezcArchive::appendToCurrent().


appendToCurrent

void appendToCurrent( string|array(string) $files, string $prefix )
Appends a file to the archive after the current entry.
One or multiple files can be added directly after the current file. The remaining entries after the current are removed from the archive!
The $files can either be a string or an array of strings. Which, respectively, represents a single file or multiple files.
$prefix specifies the begin part of the $files path that should not be included in the archive. The files in the archive are always stored relatively.
Example:
1.  $tar ezcArchive"/tmp/my_archive.tar"ezcArchive::TAR );
2.  
3.  // Append two files to the end of the archive.
4.   $tar->seek0SEEK_END );
5.  $tar->appendToCurrentarray"/home/rb/file1.txt""/home/rb/file2.txt" )"/home/rb/" );
When multiple files are added to the archive at the same time, thus using an array, does not necessarily produce the same archive as repeatively adding one file to the archive. For example, the Tar archive format, can detect that files hardlink to each other and will store it in a more efficient way.

Parameters

Name Type Description
$files string|array(string) Array or a single path to a file.
$prefix string First part of the path used in $files.

Throws

ClassDescription
ezcArchiveWriteException if one of the files cannot be written to the archive.
ezcFileReadException if one of the files cannot be read from the local filesystem.

Redefined in descendants as

Method Description
ezcArchiveV7Tar::appendToCurrent()  
ezcArchiveZip::appendToCurrent()  

createDefaultDirectory

void createDefaultDirectory( string $file )
Creates all the directories needed to create the file $file.

Parameters

Name Type Description
$file string Path to a file, where all the base directory names will be created.

createInstance

ezcArchive createInstance( string $archiveName, int $type )
Returns an instance of the archive with the given type.
Similar to open(), but the type is required.

Parameters

Name Type Description
$archiveName string The path of the archive.
$type int Open the archive with the $forceType algorithm. Possible values are: ezcArchive::ZIP, ezcArchive::TAR, ezcArchive::TAR_V7, ezcArchive::TAR_USTAR, ezcArchive::TAR_PAX, ezcArchive::TAR_GNU. TAR will use the TAR_USTAR algorithm by default.

current

ezcArchiveEntry current( )
Returns the current ezcArchiveEntry if it is valid, otherwise false is returned.

extract

void extract( string $target, [bool $keepExisting = false] )
Extract entries from the archive to the target directory.
All entries from the archive are extracted to the target directory. By default the files in the target directory are overwritten. If the $keepExisting is set to true, the files from the archive will not overwrite existing files.

Parameters

Name Type Description
$target string Absolute or relative path of the directory.
$keepExisting bool If set to true then the file will be overwritten, otherwise not.

Throws

ClassDescription
ezcArchiveReadException if an entry cannot be extracted from the archive. *

See also:

ezcArchive::extractCurrent().


extractCurrent

bool extractCurrent( $target, [ $keepExisting = false], string $target , bool $keepExisting )
Extract the current entry to which the iterator points.
Extract the current entry to which the iterator points, and return true if the current entry is extracted. If the iterator doesn't point to a valid entry, this method returns false.
True if the file is extracted correctly, otherwise false.

Parameters

Name Type Description
$target string The full path to which the target should be extracted.
$keepExisting bool True if the file shouldn't be overwritten if they already exist. For the opposite behaviour, false should be given.
$target  
$keepExisting  

Throws

ClassDescription
ezcArchiveValueException if the archive contains invalid values.
ezcBaseFileNotFoundException if the link cannot be found.

getAlgorithm

int getAlgorithm( )
Returns the algorithm that is used currently.

Redefined in descendants as

Method Description
ezcArchiveV7Tar::getAlgorithm()  
ezcArchiveUstarTar::getAlgorithm()  
ezcArchivePaxTar::getAlgorithm()  
ezcArchiveGnuTar::getAlgorithm()  
ezcArchiveZip::getAlgorithm()  

getListing

array(string) getListing( )
Returns an array that lists the content of the archive.
Use the getArchiveEntry method to get more information about an entry.

See also:

ezcArchive::__toString().


getTarInstance

ezcArchive getTarInstance( $blockFile, $type, int $type )
Open a tar instance.
This method is made public for testing purposes, and should not be used.

Parameters

Name Type Description
$blockFile ezcArchiveBlockFile  
$type int algorithm. Possible values are: ezcArchive::TAR, ezcArchive::TAR_V7, ezcArchive::TAR_USTAR, ezcArchive::TAR_PAX, ezcArchive::TAR_GNU. TAR will use the TAR_USTAR algorithm by default.
$type  

getZipInstance

ezcArchive getZipInstance( $charFile )
Open a zip instance. This method is made public for testing purposes, and should not be used.

Parameters

Name Type Description
$charFile ezcArchiveCharacterFile The character file which contains the archive.

isEmpty

bool isEmpty( )
Returns true if the current archive is empty, otherwise false.

isWritable

bool isWritable( )
Returns true if it is possible to write to the archive, otherwise false.
This method returns false if the archive is read-only, the algorithm didn't implement any write methods, or both.

See also:

ezcArchive::algorithmCanWrite().


key

int key( )
Returns the current key, entry number, if it is valid, otherwise false is returned.

next

ezcArchiveEntry next( )
Forwards the iterator to the next entry.
If there is no next entry all iterator methods except for rewind() will return false.

See also:

ezcArchive::rewind().


open

ezcArchive open( string $archiveName, [int $forceType = null] )
Returns a new ezcArchive instance.
This method returns a new instance according to the mime-type or the given $forceType.
  • If $forceType is set to null, this method will try to determine the archive format via the file data. Therefore the $forceType can only be null when the archive contains data.
  • If $forceType is set, it will use the specified algorithm. Even when the given archive is from another type than specified.

Parameters

Name Type Description
$archiveName string Absolute or relative path to the archive.
$forceType int Open the archive with the $forceType algorithm. Possible values are: ezcArchive::ZIP, ezcArchive::TAR, ezcArchive::TAR_V7, ezcArchive::TAR_USTAR, ezcArchive::TAR_PAX, ezcArchive::TAR_GNU. TAR will use the TAR_USTAR algorithm by default.

Throws

ClassDescription
ezcArchiveUnknownTypeException if the type of the archive cannot be determined.

rewind

void rewind( )
Rewinds the iterator to the first entry.

seek

bool seek( int $offset, [int $whence = SEEK_SET] )
Search for the entry number.
The two parameters here are the same as the PHP fseek() method. The internal iterator position will be set by $offset added to $whence iterations forward. Where $whence is:
  • SEEK_SET, Set the position equal to $offset.
  • SEEK_CUR, Set the current position plus $offset.
  • SEEK_END, Set the last file in archive position plus $offset.
This method returns true if the new position is valid, otherwise false.

Parameters

Name Type Description
$offset int  
$whence int  

truncate

void truncate( [ $fileNumber = 0], int $fileNumber )
Truncate the archive to $fileNumber of files.
The $fileNumber parameter specifies the amount of files that should remain. If the default value, zero, is used then the entire archive file is cleared.

Parameters

Name Type Description
$fileNumber int  
$fileNumber  

Redefined in descendants as

Method Description
ezcArchiveV7Tar::truncate()  
ezcArchiveZip::truncate()  

valid

bool valid( )
Returns true if the iterator points to a valid entry, otherwise false.

writeCurrentDataToFile

void writeCurrentDataToFile( string $targetPath )
Writes the file data from the current entry to the given file.

Parameters

Name Type Description
$targetPath string The absolute or relative path of the target file.

Redefined in descendants as

Method Description
ezcArchiveV7Tar::writeCurrentDataToFile()  
ezcArchiveZip::writeCurrentDataToFile()  

__get

mixed __get( string $name )
Returns the property $name.
Because there are no properties available, this method will always throw an ezcBasePropertyNotFoundException.

Parameters

Name Type Description
$name string  

Throws

ClassDescription
ezcBasePropertyNotFoundException if the property does not exist.

__set

void __set( string $name, mixed $value )
Sets the property $name to $value.
Because there are no properties available, this method will always throw an ezcBasePropertyNotFoundException.

Parameters

Name Type Description
$name string  
$value mixed  

Throws

ClassDescription
ezcBasePropertyNotFoundException if the property does not exist.

__toString

string __toString( )
Returns a string which represents all the entries from the archive.

Last updated: Fri, 02 Nov 2007