Archive modification

Archive modification -- Remove and append files

Introduction

File_Archive version 1.3 introduces some new functions to edit existing archives. These functions will allow you to remove or append files to an existing archive.

Since for File_Archive, the file system is just another reader / writer, those modifications can be done on "real" archives (real files), or on nested archives (an archive inside another archive).

Remove files from an existing archive

To remove files from an archive, you'll use one of the following functions from File_Archive class:

Note that those functions will not recursively uncompress archives.

Example 43-1. Remove Jpg, gif and Bmp from a zip archive


<?php
require_once "File/Archive.php";

File_Archive::remove(
    File_Archive::predExtension(
        array('jpg''jpeg''bmp''gif')
    ),
    'archive.zip'
);
?>

Example 43-2. Remove image files from a nested archive


<?php
require_once 'File/Archive.php';

File_Archive::remove(
    File_Archive::predMIME(
        array('image/*')
    ),
    'archive.zip/data.tgz'
);
?>

Appending files to an archive

To append files to an archive, you'll use one of the following functions from File_Archive class:

Both of these functions return a writer that you can use as in the previous examples, using the extract function

Example 43-3. Add a folder to an archive


<?php
require_once "File/Archive.php";

File_Archive::extract(
    File_Archive::read('folder'),
    File_Archive::appender('archive.zip')
);
?>

Note that if you use a string instead of a writer in a function, the string will be converted using File_Archive::appender(). Thus the previous example could be rewritten to:

Example 43-4. Add a folder to an archive


<?php
require_once "File/Archive.php";

File_Archive::extract(
    $src 'folder',
    $dest'archive.zip'
);
?>