I arrived back from Toronto yesterday, although I was too tired to do anything despite getting home at about 10am! But I’m now ready to continue posting regularly.
I am getting ready to release a major new version (1.2) of one of my products. One of the features of this allows you to automatically generate zip, tar, gzip and bzip files of your product so they can be distributed to your customers. You upload the files, and the application generates the archive.
When I was writing this section of code, I spent quite a long time looking for classes to help with the archive generation. I am a strong believer in reusing code that is made available in libraries rather than writing it myself. It saves a lot of time! I looked in a lot of places and tried several classes – they worked, but not fully. For example, there were issues with corrupt zip files, or directories not being maintained. I eventually settled on a PEAR class called File_Archive, which is extremely good!
File_Archive: File_Archive will let you manipulate easily the tar, gz, tgz, bz2, tbz, zip, ar (or deb) files
Not only does it do everything you might need to do with archives, but it is actively developed. I have contributed several bug reports and they are always responded to and fixed quickly – often within hours of reporting. When I started using it, the docs available weren’t that great, but they have now been greatly improved and updated.
Before actually doing any code writing, it is important to decide what kind of archive you are going to generate. The documentation provides a very good description of what to look out for:
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
- Tgz, Tbz
Pros: very high compression ratio, constant memory usage
Cons: can’t be cached, needs a lot of CPU at each generation
Pros: intermediate result can be cached, compressed, you can choose the compression level, widely used
Cons: compression ratio lower than for Tgz/Tbz
File_Archive includes several features which will be useful for handling files and their archives (hence the name!). These examples have been nabbed from the File_Archive docs to illustrate the library.
The readers allow you to read files, directories and also archives of files/directories. The generated list is held within an object so it can be used later when archiving or serving to users. For example, if you wanted to read an archive, and hence uncompress it:
$source = File_Archive::read('path/to/dir/archive.tar');
Writers will do the actual writing of archives to disk or to memory. There is also a function which can send a created archive as an attachment in an e-mail. You have to call a reader before the writer so you can get the contents to create the archive with. For example, this example will send all the files in the current directory by e-mail:
File_Archive::read('path/to/dir', '', 0, 0),
File_Archive::toMail('email@example.com', array('Subject' => 'path/to/dir directory', 'From' => 'firstname.lastname@example.org'), 'body'));
File_Archive 1.4 introduced the possibility to use a cache to store intermediate result of a zip compression. It uses the Cache_Lite PEAR package to do so.
A zip file is made of compressed files, one after the others. So if you generate an archive that contains files A, B and C and then another archive that contains A and C, you will compress twice the files A and C. The use of the cache will allow to save the compressed version of files A, B and C on the first compression, to use them again in the second compression.
On my machine (a thinkpad T42P with default factory equipment), generating a 200MB zip archive takes around 30s of CPU without the cache, 32s of CPU with an empty cache and 2s of CPU if all the files to compress are already in cach
Here is a function which I used in my product to quickly generate any kind of supported archive from an existing directory (and all of its contents):
This function takes 2 arguments: the type of archive you want to create – [font=Courier New]string $type[/font], and the name of the directory you want to archive – [font=Courier New]string $directory[/font]. The archive is then created with the same name as the directory. The options to pass to [font=Courier New]$type[/font] can be one of tgz, tbz, tar, zip, gz, gzip, bz2, bzip2 or any composition of them (for example tar.gz or tar.bz2). The case of this parameter is not important.
function archive($type, $directory)
$archive = File_Archive::read($directory, $directory);
trigger_error('Directory '.$location.' does not exist', E_USER_ERROR);
So hopefully you’ll find this useful in your own programs.