One possible use case 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:
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
Pros: Very high compression ratio, constant memory usage
Cons: Can't be cached, needs a lot of CPU at each generation
We will focus on Tar and Zip generation, Tgz and Tbz are too CPU expensive for an "on the fly" archive generation.
<?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() ) ); ?> |
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.
Example 43-1. On the fly creation of an uncompressed ZIP archive
|
Example 43-2. On the fly creation of a compressed ZIP archive with a cache
|
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.
Example 43-3. Custom archive
|