How to Read a .Tar.Gz File with PHP

How can I read a .tar.gz file with PHP?

You can do this with the PharData class:

// Example: list files
$archive = new PharData('/some/file.tar.gz');
foreach($archive as $file) {
echo "$file\n";
}

This even works with the phar:// stream wrapper:

$list = scandir('phar:///some/file.tar.gz');
$fd = fopen('phar:///some/file.tar.gz/some/file/in/the/archive', 'r');
$contents = file_get_contents('phar:///some/file.tar.gz/some/file/in/the/archive');

If you don't have Phar, check the PHP-only implementation, or the pecl extension.

PHP Untar-gz without exec()?

Since PHP 5.3.0 you do not need to use Archive_Tar.

There is new class to work on tar archive: The PharData class.

To extract an archive (using PharData::extractTo() which work like the ZipArchive::extractTo()):

try {
$phar = new PharData('myphar.tar');
$phar->extractTo('/full/path'); // extract all files
} catch (Exception $e) {
// handle errors
}

And if you have a tar.gz archive, just decompress it before extract (using PharData::decompress()):

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

// unarchive from the tar
$phar = new PharData('/path/to/my.tar');
$phar->extractTo('/full/path');

How to extract all nested tar.gz and zip to a directory in PHP?

I solved it after doing a bit of research. This solves the problem.

There are 3 functions:

  • recursiveScanProtected(): It extracts all the Compressed files
  • scanJSON(): It will scan for JSON files and move them to the processing folder.
  • delete_files(): This function removes everything except the processing folder where have the JSON files, and index.php in the root directory.
<?php

// Root directory
$path = './';

// Directory where I want to extract the JSON files
$path_json = $path.'processing/';

// Function to extract all the compressed files
function recursiveScanProtected($dir, $conn) {
if($dir != '') {
$tree = glob(rtrim($dir, '/') . '/*');
if (is_array($tree)) {
for ($i = 0; $i < count($tree); $i++) {
$file = $tree[$i];
if (is_dir($file)) {
recursiveScanProtected($file, $conn); // Recursive call if directory
} elseif (is_file($file)) {

$item = $file;
$fileExt = explode('.', $item);
// Getting the extension of the file
$fileActualExt = strtolower(end($fileExt));
// Check if the file is a zip or a tar.gz
if(($fileActualExt == 'gz') or ($fileActualExt == 'zip')){

// Moving the file - Overwriting true
$phar->extractTo($dir.$i."/", null, true);

// Del the compressed file
unlink($item);

recursiveScanProtected($dir.$i, $conn); // Recursive call
}

}
}
}
}
}
recursiveScanProtected($path, $conn);

// Move the JSON files to processing
function scanJSON($dir, $path_json) {
if($dir != '') {
$tree = glob(rtrim($dir, '/') . '/*');
if (is_array($tree)) {
foreach($tree as $file) {
if (is_dir($file)) {
// Do not scan processing recursively, but all other directories should be scanned
if($file != './processing'){
scanJSON($file, $path_json);
}
} elseif (is_file($file)) {

$ext = pathinfo($file);

if(strtolower($ext['extension']) == 'json'){
// Move the JSON files to processing
rename($file, $path_json.$ext['basename']);
}
}
}
}
}
}

scanJSON($path, $path_json);

/*
* php delete function that deals with directories recursively
* It deletes everything except ./dataset/processing and index.php
*/
function delete_files($target) {

if(is_dir($target)){
$files = glob( $target . '*', GLOB_MARK ); //GLOB_MARK adds a slash to directories returned
foreach( $files as $file ){
if($file == './processing/' || $file == './index.php'){
continue;
} else{
delete_files( $file );
}
}
if($target != './'){
rmdir( $target );
}
} elseif(is_file($target)) {
unlink( $target );
}
}

delete_files($path);
?>

Create .tar.gz file using PHP

Use the parent of "parent-folder" as the base for Phar::buildFromDirectory() and use its second parameter to limit the results only to "parent-folder", e.g.:

$parent = dirname("parent-folder");
$pd->buildFromDirectory($parent, '#^'.preg_quote("$parent/parent-folder/", "#").'#');
$pd->compress(Phar::GZ);

How do I extract a .tar file in pure PHP on Windows?

It's too hard. Try something like this: http://www.phpclasses.org/package/945-PHP-Create-tar-gzip-bzip2-zip-extract-tar-gzip-bzip2-.html

How to get content of a single text or a single json file inside a .tar.gz file in PHP?

A .tar.gz file cannot be accessed randomly. All of the files up to the one you want must be decompressed.

You can try using a .zip file instead, which will permit rapid random access. However the compression of many small files could be much worse, since zip entries are compressed independently and not able to use repeated information in the previous files to aid in the compression of subsequent files.

Get total number of files inside a tar.gz file using php

Try PharData:

// Example: list files
$archive = new PharData('/some/file.tar.gz');
foreach($archive as $file) {
echo "$file\n";
}


Related Topics



Leave a reply



Submit