2009-01-13 88 views
2

是否有一個用於在php中創建/解壓zip文件的庫?從zip檔案創建和解壓縮文件

的ZipArchive類的工作不正常,這是對php.net提到: (爲我檢查每一個功能)

ZipArchive :: addEmptyDir (沒有可用的版本信息,可能只在CVS)

回答

0

好吧,我檢查http://pear.php.net/package/Archive_Zip張貼由Irmantas,
但它說,這:
「這個包是不再維持,並已取代軟件包已經轉移到渠道pecl.php.net,包壓縮。」 然後我搜索了pear.php.net並且偶然發現了: http://pear.php.net/package/File_Archive

File_Archive雖然沒有一套非常直觀的方法。 但我想要製作一個tar文件的簡單功能,並且需要從tar文件中提取文件。

下面演示實現這一目標:從tar文件 提取文件 - >

<?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文件 - >

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

?> 
0

same module which includes ZipArchive還包括許多功能,它允許對zip文件的程序的訪問。該功能在PHP 4(自4.0.7)和PHP 5(自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); 
0

您可以從PHP調出到.NET程序集。 如果你不介意的話,那麼你可以使用DotNetZip - 它是可靠的,完全沒有不穩定的。

DotNetZip on CodePlex

示例代碼:

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

?>