PHP 8.5.0 Alpha 4 available for testing

Voting

: one plus eight?
(Example: nine)

The Note You're Voting On

stanislav dot eckert at vizson dot de
10 years ago
The extractTo() method does not offer any parameter to allow extracting files and folders recursively from another (parent) folder inside the ZIP archive. With the following method it is possible:

<?php
class my_ZipArchive extends ZipArchive
{
public function
extractSubdirTo($destination, $subdir)
{
$errors = array();

// Prepare dirs
$destination = str_replace(array("/", "\\"), DIRECTORY_SEPARATOR, $destination);
$subdir = str_replace(array("/", "\\"), "/", $subdir);

if (
substr($destination, mb_strlen(DIRECTORY_SEPARATOR, "UTF-8") * -1) != DIRECTORY_SEPARATOR)
$destination .= DIRECTORY_SEPARATOR;

if (
substr($subdir, -1) != "/")
$subdir .= "/";

// Extract files
for ($i = 0; $i < $this->numFiles; $i++)
{
$filename = $this->getNameIndex($i);

if (
substr($filename, 0, mb_strlen($subdir, "UTF-8")) == $subdir)
{
$relativePath = substr($filename, mb_strlen($subdir, "UTF-8"));
$relativePath = str_replace(array("/", "\\"), DIRECTORY_SEPARATOR, $relativePath);

if (
mb_strlen($relativePath, "UTF-8") > 0)
{
if (
substr($filename, -1) == "/") // Directory
{
// New dir
if (!is_dir($destination . $relativePath))
if (!@
mkdir($destination . $relativePath, 0755, true))
$errors[$i] = $filename;
}
else
{
if (
dirname($relativePath) != ".")
{
if (!
is_dir($destination . dirname($relativePath)))
{
// New dir (for file)
@mkdir($destination . dirname($relativePath), 0755, true);
}
}

// New file
if (@file_put_contents($destination . $relativePath, $this->getFromIndex($i)) === false)
$errors[$i] = $filename;
}
}
}
}

return
$errors;
}
}
?>

Example:
<?php
echo "<pre>";

$zip = new my_ZipArchive();
if (
$zip->open("test.zip") === TRUE)
{
$errors = $zip->extractSubdirTo("C:/output", "folder/subfolder/");
$zip->close();

echo
'ok, errors: ' . count($errors);
}
else
{
echo
'failed';
}
?>

<< Back to user notes page

To Top