Monday, 24 September 2018

How to extract tar.gz archives in php


In a previous article we learned how to create compressed tar archives in php. Now lets do the reverse, that is extract tar.gz archives and get the files out.

The code to extract a tar.gz archive is very simple and uses PharData class. Here is an example
1
2
3
4
5
6
7
// decompress from gz
$p = new PharData('files.tar.gz');
$p->decompress(); // creates files.tar
// unarchive from the tar
$phar = new PharData('files.tar');
$phar->extractTo('new_dir');
The first step is to uncompress the tar.gz and convert into a .tar file. Next the .tar file is unpacked and file are copied inside a directory.

0 comments:

Post a Comment