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');
$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.

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

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 *