File_Archive

HomeFAQTutorialExamplesCompareDownloadsForum

File_Archive v1.4 released
The new version has been released with a lot of good stuff. (wildcards, cache for zip writer, simplified syntax...)

A forum has been created
You can discuss about File_Archive, ask some questions about it... in the newly created forum

File_Archive 1.3 is out
With File_Archive 1.3, you can now edit existing archives (add or remove files)

File_Archive 1.2 is out
File_Archive 1.2 now supports reading and writing to the following formats:
tar, tgz, tbz, bz2, gzip, ar, deb

File_Archive is now a PEAR package
pOOcl has been renamed File_Archive and is now part of PEAR.
See the PEAR page of File_Archive.


Valid XHTML 1.0! Valid CSS!

Dynamic generation of archive files for a gallery

One possible use of File_Archive is to dynamically generate archives that contain pictures or videos from a gallery.
The choice of the file format is important if you want an efficient generation. Let's see what are the possibilities:
  • Tar
    Pros: generation very efficient, constant memory usage, no need to cache
    Cons: no compression (but anyway images or video can hardly be compressed), not as widely used as Zip
  • Tgz, Tbz
    Pros: very high compression ratio, constant memory usage
    Cons: can't be cached, needs a lot of CPU at each generation
  • Zip
    Pros: intermediate result can be cached, compressed, you can choose the compression level, widely used
    Cons: compression ratio lower than for Tgz/Tbz

We will focus on Tar and Zip generation, Tgz and Tbz are too CPU expensive for an "on the fly" archive generation

Tar generation

On the fly creation of a TAR archive <?php
require_once "File/Archive.php";

// $files is an array of path to the files that must be added to the archive

File_Archive::extract(
    
$files,
    
File_Archive::toArchive("myGallery.tar"File_Archive::toOutput())
);

?>

Zip generation

The main advantages of the Zip generation is that it is not very expensive (due to the ability to cache the result), and widely used. I think 2 viable options are to generate uncompressed Zip archives (since you don't reduce a lot the size of picture and video files by compressing them) or to generate compressed Zip archive using a cache system.
On the fly creation of an uncompressed ZIP archive <?php
require_once "File/Archive.php";

File_Archive::setOption("zipCompressionLevel"0);

// $files is an array of path to the files that must be added to the archive

File_Archive::extract(
    
$files,
    
File_Archive::toArchive("myGallery.zip"File_Archive::toOutput())
);

?>

On the fly creation of a compressed ZIP archive with a cache <?php
require_once "File/Archive.php";
require_once 
"Cache/Lite.php";

// See the documentation of cache lite for the meaning of the $options array
// fileNameProtection must be left to the default true value
// automaticSerialization is not required and should be left to false
$options = array("cacheDir" => "tmp");

File_Archive::setOption("cache", new Cache_Lite($options));
File_Archive::setOption("zipCompressionLevel"9);

// $files is an array of path to the files that must be added to the archive

File_Archive::extract(
    
$files,
    
File_Archive::toArchive("myGallery.zip"File_Archive::toOutput())
);

?>

Putting it all together

Since generating a zip or a tar archive is pretty much the same code, you can write a simple code that lets the user choose what format he wants. The following code is taken from a code I really use in my gallery
Custom archive <?php
$allowedFormats 
= array("tar""zip");

if(!
in_array($_GET["type"], $allowedFormats))
    die(
"Type ".$_GET["type"]." is either unknown or not allowed");

require_once 
"File/Archive.php";

//Ask File_Archive to use the cache object we just created
File_Archive::setOption("zipCompressionLevel"0);

/**
 * I skipped the generation of the $files array since it really depends on you gallery
 * and what files the user requires
 */

File_Archive::extract(
    
$files,
    
File_Archive::toArchive(
        
"myGallery.".$_GET["type"],
        
File_Archive::toOutput()
    )
);

}
?>