Tar is a common archive format used on linux. Alone it is just an archiving format, that is it can only pack multiple files together but not compress them. When combined with gzip or bzip2 files are compressed as well. Then it becomes a .tar.gz which is actually a gzip compressed tar file.
Php has a class called PharData that can be used to create tar archives and tar.gz compressed files as well. It works from php 5.3 onwards. Usage is quite simple. Lets take a look
//First pack all files in a tar archive
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
echo "Files added to archive.tar";
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
The above code creates a tar archive named "archive.tar". Files are added using the addFile method.
Compress
Next thing to do is compress the tar archive to a tar.gz. There are 2 ways to do this. Either call the compress method of PharData object.
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
$a->compress(Phar::GZ);
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
Or use the gzencode function
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
//Now compress to tar.gz
file_put_contents('archive.tar.gz'...
Read full post here
How to create tar archives in php
Php has a class called PharData that can be used to create tar archives and tar.gz compressed files as well. It works from php 5.3 onwards. Usage is quite simple. Lets take a look
//First pack all files in a tar archive
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
echo "Files added to archive.tar";
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
The above code creates a tar archive named "archive.tar". Files are added using the addFile method.
Compress
Next thing to do is compress the tar archive to a tar.gz. There are 2 ways to do this. Either call the compress method of PharData object.
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
$a->compress(Phar::GZ);
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
Or use the gzencode function
try
{
$a = new PharData('archive.tar');
$a->addFile('data.xls');
$a->addFile('index.php');
}
catch (Exception $e)
{
echo "Exception : " . $e;
}
//Now compress to tar.gz
file_put_contents('archive.tar.gz'...
Read full post here
How to create tar archives in php