file_get_contents("zip:///a/b/c.zip")
renvoie NULL
. Comment puis-je lire le contenu décompressé d'un fichier zip en PHP 5+?La meilleure façon de lire le fichier zip en PHP
Répondre
Utilisez les fonctions zip_open
et zip_read
pour ce faire. Documentation à ce que vous pouvez trouver à http://pl2.php.net/manual/en/function.zip-read.php
<?php
/**
* This method unzips a directory within a zip-archive
*
* @author Florian 'x!sign.dll' Wolf
* @license LGPL v2 or later
* @link http://www.xsigndll.de
* @link http://www.clansuite.com
*/
function extractZip($zipFile = '', $dirFromZip = '')
{
define(DIRECTORY_SEPARATOR, '/');
$zipDir = getcwd() . DIRECTORY_SEPARATOR;
$zip = zip_open($zipDir.$zipFile);
if ($zip)
{
while ($zip_entry = zip_read($zip))
{
$completePath = $zipDir . dirname(zip_entry_name($zip_entry));
$completeName = $zipDir . zip_entry_name($zip_entry);
// Walk through path to create non existing directories
// This won't apply to empty directories ! They are created further below
if(!file_exists($completePath) && preg_match('#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry))))
{
$tmp = '';
foreach(explode('/',$completePath) AS $k)
{
$tmp .= $k.'/';
if(!file_exists($tmp))
{
@mkdir($tmp, 0777);
}
}
}
if (zip_entry_open($zip, $zip_entry, "r"))
{
if(preg_match('#^' . $dirFromZip .'.*#', dirname(zip_entry_name($zip_entry))))
{
if ($fd = @fopen($completeName, 'w+'))
{
fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
fclose($fd);
}
else
{
// We think this was an empty directory
mkdir($completeName, 0777);
}
zip_entry_close($zip_entry);
}
}
}
zip_close($zip);
}
return true;
}
// The call to exctract a path within the zip file
extractZip('clansuite.zip', 'core/filters');
?>
regard sur la construction dans les fonctions zip: http://php.net/manual/en/book.zip.php
J'aime la classe "ZipArchive". –
Le protocole zip://
est fourni par le ZIP extension de PHP. Vérifiez dans votre sortie phpinfo()
si l'extension a été installée ou non.
de phpinfo(): Zip => activé Version Extension => $ Id: php_zip.c, v 1.1.2.38 06/08/2007 22:02:32 yannick Exp $ version Zip => 2.0. 0 Libzip version => 0.7.1 Flux PHP enregistrés => zip, php, fichier, données, http, ftp, compress.bzip2, compress.zlib, https, ftps – Sam
Avez-vous vérifié si le fichier que vous essayez de open est en fait un fichier '.zip' valide? – joschi
%> fichier test.zip test.zip: Données d'archive zip, au moins v2.0 pour extraire – Sam
utilisation ZipArchive
classe
$zip = new ZipArchive;
$zip->open('test.zip');
echo $zip->getFromName('filename.txt');
$zip->close();
'if ($ zip)' ne fonctionne pas, vous devez utiliser 'si (is_resource ($ zip))'. Voir ['zip_open'] (http://php.net/manual/fr/function.zip-open.php). De plus, le lien fourni dans la réponse n'est plus disponible. – kjaquier