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:

  • remove ( &$pred , $URL )

    Removes all the files that follow a given predicate from file $URL. Note that the URL is the same as in the File_Archive::read() function. You can use nested archives.

  • removeFromSource ( &$pred , &$source , $URL = null )

    Same as remove, but use $source instead of the default file system reader. If no URL is specified, $source must be an archive, and the files will be removed from here.

  • removeDuplicates ( $URL )

    This function will remove all the doublons from the archive at $URL; only the most recent file will be kept.

    If the modification date is not specified for a file, it will be considered infinitely old.

    If two files have the same modification date, the one that has highest position in the archive (usually the one that was added to the archive at last) will be kept.

  • removeDuplicatesFromSource ( &$source , $URL = null )

    Same as removeDuplicate(), but use $source instead of the default file system reader. If no URL is specified, the duplicates will be removed from $source itself.

Note that those functions will not recursively uncompress archives.

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'
);
?>

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:

  • appender ( $URL , $unique = null , $type = null , $stat = array() )

    Allows to append to the archive specified by the URL.

    If unique is set to true, the eventual duplicates created by the insertion of new files will be automatically removed. If set to null, the default value (that you can change using File_Archive::setOption('appendRemoveDuplicates', true/false)) is used (false by default).

    If the archive does not exist, it will be created using the type specified in parameter (or looking at the extension in the URL if the type is not specified), and the stat array.

  • appenderFromSource ( &$source , $URL = null , $unique = null , $type = null , $stat = array() )

    Same as appender, but using $source instead of the default file system reader.

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

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:

Add a folder to an archive

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

File_Archive::extract(
    
$src 'folder',
    
$dest'archive.zip'
);
?>
    Поддержать сайт на родительском проекте КГБ