How to extract tar.gz archives in php

By | March 12, 2013

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

// decompress from gz
$p = new PharData('files.tar.gz');
$p->decompress(); // creates files.tar

// unarchive from the tar
$phar = new PharData('files.tar');

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.

Last Updated On : 12th March 2013

One thought on “How to extract tar.gz archives in php

Leave a Reply

Your email address will not be published. Required fields are marked *