Unzip functie
Auteur: lucasvdh - 06 januari 2010 - 19:36 - Gekeurd door: Koen - Hits: 3082 - Aantal punten: (0 stemmen)
Even een simpele unzip functie gemaakt..
voorbeeldje van een implementatie staat er bij ;)
Plz let me know als er nog wat bugs in zitten..
lucas@hb-webdesign.nl
|
Code: |
<?php
/*
#################################
# AUTHOR: Lucas vd have #
# VERSION: v1.0 #
# DATE: 06-01-10 #
# WEBSITE: www.hb-webdesign.nl #
#################################
*/
// VOORBEELD ALS JE WIL UNZIPPEN AAN DE HAND VAN $_GET
if(isset($_GET['unzip'])){
$info = unzip($_GET['unzip'],"destinationFolder/");
if(is_array($info)) {
echo "Extracted successfully<br /><br />";
echo "Folders:<br />";
for($i=0; $i<count($info["folders"]); $i++){
echo $info["folders"][$i]."<br />";
}
echo "<br />Files extracted:<br />";
for($i=0; $i<count($info["files"]); $i++){
echo $info["files"][$i]." - ".$info["size"][$i]." bytes<br />";
}
}
else {
echo "<font color='red'>Error : ".$info."</font><br />";
}
}
// VOORBEELD
/*
Function unzip
Unzip a *.zip file and save files to server.
@param $file path to *.zip file
@param $destination destination folder (not required)
@return array() [files] //all files extracted
[size] //size of extracted file
[compressionMethod] //compressionmethod of file
[folders] //all folders extracted
*/
function unzip($file,$destination = NULL)
{
if(!is_dir($destination) && $destination!=NULL) // if the destination does not exist and is not NULL, return error
return "Destination does not exist";
if(!is_file($file)) // if the file does not exist return error
return "File does not exist";
$filetype = mime_content_type($file);
if ($filetype=="application/zip") {
$zip = zip_open($file); // open zip
// initialize arrays for optional data
$fileArray = array();
$folderArray = array();
$sizeArray = array();
$compressionMethodArray = array();
while ($zip_entry = zip_read($zip)) { // read entire zip
// initialize variables
$name = zip_entry_name($zip_entry);
$size = zip_entry_filesize($zip_entry);
$compressed_size = zip_entry_compressedsize($zip_entry);
$compressionMethod = zip_entry_compressionmethod($zip_entry);
if (zip_entry_open($zip, $zip_entry, "r")) {
$content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
$lastchar = $name[strlen($name)-1];
if($lastchar=='/'){ // if $name is a dir, create a dir with permissions 0777
if(@mkdir($destination.$name, 0777))
array_push($folderArray,$name);
else
return "Unable to create directory"; // an error occurred
}
else { // if $name is NOT a dir, create file
$f = fopen($destination.$name,'w'); // create file
if(fwrite($f,$content)) { // write content to file
array_push($fileArray,$name); // push filename to array
array_push($sizeArray,$size); // push filesize to array
array_push($compressionMethodArray,$compressionMethod); // push compressionMethod to array
}
else {
return "Unable to write file"; // an error occurred
}
fclose($f); // close file
zip_entry_close($zip_entry);
}
}
}
zip_close($zip); // close zip
for($i=0;$i<count($folderArray); $i++){
chmod($folderArray[$i],0755); // set chmod back to 0755 so it can't be edited by everyone.
}
// everything is completed, return the array with optional data
return array("files" => $fileArray, "folders" => $folderArray, "size" => $sizeArray, "compressionMethod" => $compressionMethodArray);
}
else {
return "Unable to extract file (not a *.zip file)."; // an error occurred
}
}
?>
<?php /* ################################# # AUTHOR: Lucas vd have # # VERSION: v1.0 # # DATE: 06-01-10 # # WEBSITE: www.hb-webdesign.nl # ################################# */ // VOORBEELD ALS JE WIL UNZIPPEN AAN DE HAND VAN $_GET if(isset($_GET['unzip'])){ $info = unzip($_GET['unzip'],"destinationFolder/"); echo "Extracted successfully<br /><br />"; for($i=0; $i<count($info["folders"]); $i++){ echo $info["folders"][$i]."<br />"; } echo "<br />Files extracted:<br />"; for($i=0; $i<count($info["files"]); $i++){ echo $info["files"][$i]." - ".$info["size"][$i]." bytes<br />"; } } else { echo "<font color='red'>Error : ".$info."</font><br />"; } } // VOORBEELD /* Function unzip Unzip a *.zip file and save files to server. @param $file path to *.zip file @param $destination destination folder (not required) @return array() [files] //all files extracted [size] //size of extracted file [compressionMethod] //compressionmethod of file [folders] //all folders extracted */ function unzip($file,$destination = NULL) { if(!is_dir($destination) && $destination!=NULL) // if the destination does not exist and is not NULL, return error return "Destination does not exist"; if(!is_file($file)) // if the file does not exist return error return "File does not exist"; $filetype = mime_content_type($file); if ($filetype=="application/zip") { $zip = zip_open($file); // open zip // initialize arrays for optional data $compressionMethodArray = array(); while ($zip_entry = zip_read($zip)) { // read entire zip // initialize variables $name = zip_entry_name($zip_entry); $size = zip_entry_filesize($zip_entry); $compressed_size = zip_entry_compressedsize($zip_entry); $compressionMethod = zip_entry_compressionmethod($zip_entry); if (zip_entry_open($zip, $zip_entry, "r")) { $content = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); $lastchar = $name[strlen($name)-1]; if($lastchar=='/'){ // if $name is a dir, create a dir with permissions 0777 if(@mkdir($destination.$name, 0777)) else return "Unable to create directory"; // an error occurred } else { // if $name is NOT a dir, create file $f = fopen($destination.$name,'w'); // create file if(fwrite($f,$content)) { // write content to file array_push($fileArray,$name); // push filename to array array_push($sizeArray,$size); // push filesize to array array_push($compressionMethodArray,$compressionMethod); // push compressionMethod to array } else { return "Unable to write file"; // an error occurred } zip_entry_close($zip_entry); } } } zip_close($zip); // close zip for($i=0;$i<count($folderArray); $i++){ chmod($folderArray[$i],0755); // set chmod back to 0755 so it can't be edited by everyone. } // everything is completed, return the array with optional data return array("files" => $fileArray, "folders" => $folderArray, "size" => $sizeArray, "compressionMethod" => $compressionMethodArray); } else { return "Unable to extract file (not a *.zip file)."; // an error occurred } } ?>
Download code (.txt)
|
|
Stemmen |
Niet ingelogd. |
|