...

View Full Version : Extracting a specific folder from a Zip on the server



Gremlyn1
12-03-2009, 12:33 AM
<?php
function extractDir($zipfile, $path) {
if (file_exists($zipfile)) {
$files = array();
$zip = new ZipArchive;
if ($zip->open($zipfile) === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
//Use strpos() to check if the entry name contains the directory we want to extract
if (strpos($entry, "/MyFolder/")) {
//Add the entry to our array if it is in our desired directory
$files[] = $entry;
}
}
//Feed $files array to extractTo() to get only the files we want
if ($zip->extractTo($path, $files) === TRUE) {
return TRUE;
} else {
return FALSE;
}
$zip->close();
} else {
return FALSE;
}
} else {
return FALSE;
}
}

//Run the function
if (extractDir($zipfile, $path)) {
$extracted = "YES! :-D";
} else {
$extracted = "NO! :*(";
}

echo $extracted;
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum