1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
| function unzip($file, $path='', $effacer_zip=false){
if(!file_exists($file)){
echo "Le fichier ".$file." n'existe pas <br />";
exit;
}
// Cas d'un serveur Windows = on transforme notre chemin de fichier
if(isset($_SERVER['SystemRoot'])){
$file_array=explode("/",$file);
// on vérifie si on utilise la règle de nommage windows ou pas (si on a un array en faisant un explode de /
if(count($file_array)>1){
$dossier_en_cours=getcwd();
$dossier_en_cours_array=explode('\\',$dossier_en_cours);
$compteur_montant=0;
for($i=0; $i<count($file_array); $i++){
if($file_array[$i]==".."){
// on doit remonter de $compteur_montant niveaux
$compteur_montant++;
}else{
$file_array_final[]=$file_array[$i];
}
}
$file="";
for($i=0; $i<count($dossier_en_cours_array)-$compteur_montant; $i++){
$file.=$dossier_en_cours_array[$i]."\\";
}
for($i=0; $i<count($file_array_final); $i++){
$file.=$file_array_final[$i];
// on ajoute le \ si et seulement si on est encore dans les dossiers
if($i<count($file_array_final)-1){
$file.="\\";
}
}
}
}
$tab_liste_fichiers = array(); //Initialisation
if(!is_file($file)){
echo "le fichier zip n'est pas un fichier valide ou n'existe pas<br />".$file;
return false;
}else{
$zip = zip_open($file);
if ($zip){
//Pour chaque fichier contenu dans le fichier zip
while ($zip_entry = zip_read($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 = strtr($nom_fichier,"ÀÁÂÃÄÅàáâãäåÒÓÔÕÖØòóôõöøÈÉÊËèéêëÇçÌÍÎÏìíîïÙÚÛÜùúûüÿÑñ","AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn");
//$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');
print "Taille du fichier $complete_name : ".zip_entry_filesize($zip_entry)."<br>\n";
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;
}// Fin fichier présent ok
}// fin de la fonction unzip |
Partager