tags:

views:

788

answers:

5

Is there a library for crating/extracting zip files in php?

The ZipArchive class works erratically, and this is mentioned on php.net : (for every function I checked)

ZipArchive::addEmptyDir (No version information available, might be only in CVS)

+2  A: 

Check PEAR Archive_Zip it might help you http://pear.php.net/package/Archive_Zip/docs/latest/Archive_Zip/Archive_Zip.html

Irmantas
+1  A: 

PHP has native Zip support via GZip (may not be enabled by default) http://us2.php.net/zlib

You can also use this class (Drupal is not part of this implementation) http://drupal.org/node/83253

TravisO
A: 

The same module which includes ZipArchive also includes a number of functions which allow procedural access to zip files. This functionality is available in PHP 4 (since 4.0.7) and PHP 5 (since 5.2.0).

$zip = zip_open("foo.zip");
$files = [];

while ($entry = zip_read($zip)) {
    zip_entry_open($zip, $entry);
    $files[zip_entry_name($entry)] = zip_entry_read($entry, zip_entry_filesize($entry));
    zip_entry_close($entry);
}

zip_close($zip);
Ben Blank
A: 

Ok, I checked http://pear.php.net/package/Archive_Zip as posted by Irmantas,
but it says this :
"This package is not maintained anymore and has been superseded. Package has moved to channel pecl.php.net, package zip." Then I searched pear.php.net and stumbled upon : http://pear.php.net/package/File_Archive

File_Archive doesn't have a very intuitive set of methods though. But I wanted simple functionality of making a tar file, and extracting files from a tar file.

Following snippets achieve that : Extracting files from a tar file ->

<?php
require_once "File/Archive.php";
$tmp = 'output';
$t1 = 'check.tar';
File_Archive::setOption('tmpDirectory','tmp');
$r = File_Archive::extract(
    File_Archive::read($t1.'/'),
    File_Archive::toFiles($tmp)
);
?>

Adding files to a tar file ->

<?php
require_once "Archive.php";
$dir = "../../mysql/data/blackStone/";
$files[0] = "";
$i = 0;
          if ($dh = opendir($dir)) {
              while (($file = readdir($dh)) !== false ) {
           if( $file != "." && $file != ".." )
             $files[$i++] = $dir.$file;
          }
      }

File_Archive::extract(
    $files,
    File_Archive::toArchive(
     'check.tar',
     File_Archive::toOutput()
    )
);

?>
dta
A: 

You can call out from PHP into .NET assemblies. If you don't mind that, then you could use DotNetZip - it's reliable and totally non-erratic.

DotNetZip on CodePlex

Sample code:

<?php
try
{
  $fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
  $zipOutput = "c:\\temp\\" . $fname;
  # Use COM interop to get to Ionic Zip. (Windows only)
  $zip = new COM("Ionic.Zip.ZipFile");
  $zip->Name = $zipOutput;
  $dirToZip= "c:\\temp\\psh";
  # Encryption:  3=AES256, 2=AES128, 1=PKZIP, 0=None.
  $zip->Encryption = 3;
  $zip->Password = "AES-Encryption-Is-Secure";
  $zip->AddDirectory($dirToZip);
  $zip->Save();
  $zip->Dispose();

  if (file_exists($zipOutput))
  {
    header('Cache-Control: no-cache, must-revalidate');
    header('Content-Type: application/x-zip'); 
    header('Content-Disposition: attachment; filename=' . $fname);
    header('Content-Length: ' . filesize($zipOutput));
    readfile($zipOutput);
    unlink($zipOutput);
  }
  else 
  {
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '</body>';
    echo '</html>';
  } 
}
catch (Exception $e) 
{
    echo '<html>';
    echo '  <head>';
    echo '  <title>Calling DotNetZip from PHP through COM</title>';
    echo '  <link rel="stylesheet" href="basic.css"/>';
    echo '  </head>';
    echo '<body>';
    echo '<h2>Whoops!</h2>' . "<br/>\n";
    echo '<p>The file was not successfully generated.</p>';
    echo '<p>Caught exception: ',  $e->getMessage(), '</p>', "\n";
    echo '<pre>';
    echo $e->getTraceAsString(), "\n";
    echo '</pre>';
    echo '</body>';
    echo '</html>';
}

?>
Cheeso