[PHP] Unzip a file | Dézipper un fichier [PHP] Unzip a file | Dézipper un fichier | Scripts | Codes

Scripts | Codes

All languages in three languages :-)


Dézipper un fichier avec PHP
Ce code crée un fonction unzip qui prend comme argument le fichier à décompresser

Unzip a file with PHP
This code creates an unzip() function which take the name of the zipped file as argument

 فتح ملف زيب بإستعمال PHP

هذا السكريبت ينشئ وظيفة unzip تأخذ اسم الملف كمدخل 

Open in a new window
<?php
//###################################################
// find more codes on scripts-n-codes.blogspot.com
//###################################################

function unzip($file, $path='', $effacer_zip=false)
{
$tab_liste_fichiers = array(); //Initialisation

$zip = zip_open($file);

if ($zip)
{
while ($zip_entry = zip_read($zip)) //Pour chaque fichier contenu dans le fichier zip
{
if (zip_entry_filesize($zip_entry) > 0)
{
$complete_path = $path.dirname(zip_entry_name($zip_entry));
   
/*On supprime les éventuels caractères spéciaux et majuscules*/
$nom_fichier = zip_entry_name($zip_entry);

$nom_fichier = strtolower($nom_fichier);
$nom_fichier = ereg_replace('[^a-zA-Z0-9.]','-',$nom_fichier);

/*On ajoute le nom du fichier dans le tableau*/
array_push($tab_liste_fichiers,$nom_fichier);
 
$complete_name = $path.$nom_fichier; //Nom et chemin de destination
 
if(!file_exists($complete_path))
{
$tmp = '';
foreach(explode('/',$complete_path) AS $k)
{
$tmp .= $k.'/';
 
if(!file_exists($tmp))
{ mkdir($tmp, 0755); }
}
}
 
/*On extrait le fichier*/
if (zip_entry_open($zip, $zip_entry, "r"))
{
$fd = fopen($complete_name, 'w');
 
fwrite($fd, zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)));
 
fclose($fd);
zip_entry_close($zip_entry);
}
}
}

zip_close($zip);

/*On efface éventuellement le fichier zip d'origine*/
if ($effacer_zip === true)
unlink($file);
}

return $tab_liste_fichiers;
}

/**********************************************************/

$liste = array();

$liste = unzip('phpMyAdmin-3.2.0-english.zip','pma/');

echo 'Le fichier zip contenait '.count($liste).' fichier(s) :<br />';

foreach ($liste as $nom_fichier)
{
echo $nom_fichier.'<br />';
}

?>


2 commentaires

  1. Anonymous  

    Merci pour cette information interessante

  2. Anonymous  

    Erreur du preg_match qui empêche le support des dossiers et sous-dossiers, il faut remplacer - par /

    Attention, c'est juste une correction rapide, pas un patch efficace ;)

Post a Comment

Subscribe to: Post Comments (Atom)
attendez....