Skip to the content.


Reading and extraction

  1. Open the file with automatic detection of a format by name or content. In case of successful, an UnifiedArchive object will be returned. In case of failure - null will be returned.

     require 'vendor/autoload.php';
     use \wapmorgan\UnifiedArchive\UnifiedArchive;
     $archive = UnifiedArchive::open('filename.rar');
     // or
     $archive = UnifiedArchive::open('', null, 'password');
     // or
     $archive = UnifiedArchive::open('filename.tar', [
     // or
     $archive = UnifiedArchive::open('filename.7z', null, 'password');
  2. Read the list of files of archive or check that file is in archive.

    foreach($archive->getFiles() as $filename) { // ['file', 'file2', 'file3', ...]
         // ...
    foreach($archive->getFiles('*.txt') as $filename) { // ['README.txt', 'doc.txt', ...]
         // ...
    foreach ($archive as $filename) {
         // ...
    if ($archive->hasFile('')) {
        // some operations
  3. To get common information about concrete file use getFileData() method. This method returns an ArchiveEntry instance. To get raw file contents use getFileContent() method, to get stream for file use getFileStream() method.

    // file meta
    $file_data = $archive->getFileData('')); // ArchiveEntry with file information
    echo 'Original size is ' . $file_data->uncompressedSize.PHP_EOL;
    echo 'Modification datetime is ' . date('r', $file_data->modificationTime).PHP_EOL;
    // raw file content
    $file_content = $archive->getFileContent('')); // string
    // pass stream to standard output
  4. Unpack all archive or specific files on a disk - extract().

     // to unpack all contents of archive to "output" folder
     // to unpack specific files ( and composer.json) from archive to "output" folder
     $archive->extract(__DIR__.'/output', ['', 'composer.json']);
     // to unpack the "src" catalog with all content from archive into the "sources" catalog on a disk
     $archive->extract(__DIR__.'/output', 'src/', true);

Archive modification

Only few archive formats support modification: (zip, 7z, tar) - it depends on low-level driver - see Formats page for details.

  1. Delete files from archive

     // Delete a single file
     // Delete multiple files
     $archive->delete(['', 'MANIFEST.MF']);
     // Delete directory with full content
     $archive->delete('src/', true);

    In case of success the number of successfully deleted files will be returned.

  2. Add files to archive

     // Add a catalog with all contents with full paths
     // To add one file (will be stored as one file "syslog")
     // To add some files or catalogs (all catalogs structure in paths will be kept)
     $archive->add([$directory, $file, $file2, ...]);


Only few archive formats support modification: zip, 7z, tar (with restrictions).

To pack completely the catalog with all attached files and subdirectories in new archive:

# archive all folder content
UnifiedArchive::archive('/var/log', '');

# archive one file
UnifiedArchive::archive('/var/log/syslog', '');

# archive few files / folders
UnifiedArchive::archive([$directory, $file, $file2, ...], '');

Also, there is extended syntax for add() and archive():

          'abc.log' => '/var/www/site/abc.log',   // stored as 'abc.log'
          '/var/www/site/abc.log',                // stored as '/var/www/site/abc.log'
          'logs' => '/var/www/site/runtime/logs', // directory content stored in 'logs' dir
          '/var/www/site/runtime/logs',           // stored as '/var/www/site/runtime/logs'
          '' => ['/home/user1/docs', '/home/user2/docs'], // user1 and user2 docs stored in archive root
    ], '');