Predicates
Introduction
File_Archive introduces the concept of filters to be able to select the files from a source. A filter is a particular reader that you can create with the File_Archive::filter() function. This function requires you to give a predicate. You can build this predicate using the File_Archive::pred* functions.
The standard logic predicates are:
-
predTrue(): always evaluates to true
-
predFalse(): always evaluates to false
-
predAnd($p1, $p2, ...): evaluates to
$p1 && $p2 && ...
-
predOr($p1, $p2, ...): evaluates to
$p1 || $p2 || ...
-
predNot($p): evaluates to
!$p
Some other predicats will help you filtering the files:
-
predMinSize($size): keep only the files which size is
>= $size
(in bytes). -
predMinTime($time): keep only the files that have been modified after $time (unix timestamp).
-
predMaxDepth($depth): keep only the files that have a public name with less than
$depth
directories. -
predExtension($list): keep only the files with a given extension.
$list
is an array or a comma separated string of allowed extensions. -
predEreg($ereg): keep only the files that have a public name that matches the given regular expression.
-
predEregi($ereg): same as predEreg(), but the test is case insensitive.
-
predMIME($mimes): Select files with a certain MIME type. You can pass an array of types or a string, and use wildcards.
Filter examples
<?php
//Extract all the files that contain an 'a' in their path
// or filename from a tar archive
File_Archive::extract(
File_Archive::filter(
File_Archive::predEreg('a'),
File_Archive::read(
'archive.tar/',
'folder'
)
),
File_Archive::toFiles()
);
//Compress a directory to a zip file, including only the files
//smaller than 1MB that have changed since last hour
File_Archive::extract(
File_Archive::filter(
File_Archive::predAnd(
File_Archive::predNot(
File_Archive::predMinSize(1024 * 1024)
),
File_Archive::predMinTime(time()-3600)
),
File_Archive::read('directory')
),
File_Archive::toArchive(
'directory.zip',
File_Archive::toFiles()
)
);
?>