13

I am building a system for people to upload .tar (and .tar.gz, .tar.bz2, .zip, etc) files in PHP. Uploading the files is fine, but I would like to list files contained in the archive after it has been uploaded.

Can someone recommend a good PHP library that can read file archives?

I found File_Archive on Pear but it hasn't been updated in a few years. ZipArchive works great for .zip files, but I need something that can handle more file types.

update I'm running on RHEL6, PHP 5.2, and Apache 2.2.

Michael
  • 8,446
  • 4
  • 26
  • 36

5 Answers5

31

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.

Sammitch
  • 30,782
  • 7
  • 50
  • 77
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • seems to be a problem with certain types of tar http://stackoverflow.com/questions/37189687/phardata-class-fails-when-tar-contents-has-relative-paths – artfulrobot Jun 13 '16 at 11:26
5

Don't try to build this yourself. Use an existing class like http://pear.php.net/package/Archive_Tar to handle that for you.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Does Archive_Tar handle compressed files? And the documentation doesn't say it can give a list of the files in a .tar file. I don't want to extract the files, I just want to list their contents. – Michael Feb 02 '11 at 21:20
  • 3
    @Michael: Yes, it supports both .gz and .bz2 compression. And there is a `->listContent` method among others. – mario Feb 02 '11 at 21:36
4

The below code reads a file inside a .gz zip file

    <?php
    $z = gzopen('zipfile.gz','r') or die("can't open: $php_errormsg");
    $string = '';

    while ($line = gzgets($z,1024)) {
        $string .= $line;
    }

    echo $string;

    gzclose($z) or die("can't close: $php_errormsg");
    ?>

Note that you need to have the zip extension of php enabled for this code to work.

shasi kanth
  • 6,987
  • 24
  • 106
  • 158
3

I don't think the first answer works. Or it only doesn't work for me. You could not read file content when you foreach it. I give my working code below.

$fh = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('phar:///dir/file.tar.gz'),
    RecursiveIteratorIterator::CHILD_FIRST
);

foreach ($fh as $splFileInfo) {
    echo file_get_contents($splFileInfo->getPathname());
}

This works for gz, zip, tar and bz files.

GiamPy
  • 3,543
  • 3
  • 30
  • 51
Jerry
  • 31
  • 1
2

Use the zlib extension

Mark Baker
  • 209,507
  • 32
  • 346
  • 385