Zip arşiv dosyaları oluşturma ve ayıklanması

4 Cevap php

Php zip dosyalarını ayıklama / kutulama için bir kütüphane var mı?

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)

4 Cevap

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)
);
?>

Tar dosyası dosya ekleme ->

<?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()
    )
);

?>

O yardımcı olabilir ARMUT Archive_Zip Giriş Eğer http://pear.php.net/package/Archive_Zip/docs/latest/Archive_Zip/Archive_Zip.html

same module which includes ZipArchive ayrıca zip dosyalarına usul erişime izin fonksiyonları bir dizi içerir. Bu işlevsellik (4.0.7 beri) PHP 4 mevcuttur ve PHP 5 (5.2.0 'dan beri).

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

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

Örnek kod:

<?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>';
}

?>