Perhaps you have a directory that contains many files, or different types of files, as well as subdirectories. There may well be situations in which you need to create a download of the whole directory that preserves its original structure. The typical command line approach to achieving this on Unix-based systems is first to create an “archive” file such as a .tar file (.tar files are “Tape Archives” and were originally conceived to help back up a file system onto tape), and then compress that file with gzip or bzip2. If you had a directory called mywork that you wanted to store as a single compressed file, you might enter the following into the command line:
This gives you a file called mywork.tar.gz. To unzip and extract the file you
could type:
This recreates the mywork directory below your current working directory. Note that you can perform the above examples in a single line on a typical Linux distribution, if you use the ’z’ flag to compress tar zcvf mywork.tar.gz ./mywork and extract tar zxvf mywork.tar.gz.
Using PHP’s system function, you could execute these commands from a PHP script, assuming your Web server has permissions to use the tar and gzip/bzip2 executables (which it probably won’t). How would you create archives from data stored in your database, or from nodes in an XML document for that matter?
Thanks to PEAR::Archive_Tar[10], it’s all possible. The basic functionality allows you to build .tar files; provided you have the zlib or bz2 extensions installed, you can also compress the file.
tar cvf mywork.tar ./mywork
gzip mywork.tar
gzip mywork.tar
could type:
gunzip mywork.tar.gz
tar xvf mywork.tar
tar xvf mywork.tar
This recreates the mywork directory below your current working directory. Note that you can perform the above examples in a single line on a typical Linux distribution, if you use the ’z’ flag to compress tar zcvf mywork.tar.gz ./mywork and extract tar zxvf mywork.tar.gz.
Using PHP’s system function, you could execute these commands from a PHP script, assuming your Web server has permissions to use the tar and gzip/bzip2 executables (which it probably won’t). How would you create archives from data stored in your database, or from nodes in an XML document for that matter?
Thanks to PEAR::Archive_Tar[10], it’s all possible. The basic functionality allows you to build .tar files; provided you have the zlib or bz2 extensions installed, you can also compress the file.
<?php
// Include PEAR::Archive_Tar
require_once 'Archive/Tar.php' ;
// Instantiate Archive_Tar
$tar = new Archive_Tar('demo/demo.tar.gz', 'gz');
// An array of files to archive
$files = array(
'demo/example.ini',
'demo/writeSecureScripts.html'
);
// Create the archive file
$tar->create($files);
echo 'Archive created';
?>
// Include PEAR::Archive_Tar
require_once 'Archive/Tar.php' ;
// Instantiate Archive_Tar
$tar = new Archive_Tar('demo/demo.tar.gz', 'gz');
// An array of files to archive
$files = array(
'demo/example.ini',
'demo/writeSecureScripts.html'
);
// Create the archive file
$tar->create($files);
echo 'Archive created';
?>