Archive
[ ]
[ ]
[ ]
[ ]
[ ]
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:
- 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.
The following examples demonstrate how to use the Archive component.
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:
- <?php
-
- require_once 'tutorial_autoload.php';
- date_default_timezone_set( "UTC" );
-
- $archive = ezcArchive::open( "/tmp/my_archive.zip" );
- $archive->truncate();
-
- $filesToAppend[] = "/tmp/file1.txt";
- $filesToAppend[] = "/tmp/file2.txt";
-
- // The second parameter specifies prefix. The prefix is normally not included
- // in the archive.
- $archive->appendToCurrent( $filesToAppend, "/tmp/" );
-
- ?>
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:
- <?php
-
- require_once 'tutorial_autoload.php';
- date_default_timezone_set( "UTC" );
-
- $archive = ezcArchive::open( "/tmp/my_archive.zip" );
- $archive->truncate();
-
- $filesToAppend[] = "/tmp/directory/";
- $filesToAppend[] = "/tmp/file.txt";
-
- // The second parameter specifies prefix. The prefix is normally not included
- // in the archive.
- $archive->appendToCurrent( $filesToAppend, "/tmp/" );
-
- ?>
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:
- <?php
-
- require_once 'tutorial_autoload.php';
- date_default_timezone_set( "UTC" );
-
- class ArchiveContext extends ezcBaseFileFindContext
- {
- public $archive;
- public $prefix;
- }
-
- function findRecursiveCallback( ezcBaseFileFindContext $context, $sourceDir, $fileName, $fileInfo )
- {
- $path = "{$sourceDir}/{$fileName}";
- if ( is_dir( $path ) )
- {
- $path .= '/';
- }
- $context->archive->append( array( $path ), $context->prefix );
- }
-
- function appendRecursive( $archive, $sourceDir, $prefix )
- {
- $context = new ArchiveContext();
- $context->archive = $archive;
- $context->prefix = $prefix;
- ezcBaseFile::walkRecursive( $sourceDir, array(), array(), 'findRecursiveCallback', $context );
- }
-
- $archive = ezcArchive::open( "my_archive.zip", ezcArchive::ZIP );
- $archive->truncate();
-
- // the 2nd parameter is the directory, the 3rd parameter is the prefix
- appendRecursive( $archive, '/tmp/directory/', '/tmp/directory/' );
-
- ?>
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.
Last updated: Mon, 11 May 2009