试试这个:
$zip = new ZipArchive;
$archiveName = 'test.zip';
$destination = $path . '/includes/';
$pattern = '#^files/includes/(.)+#';
$patternReplace = '#^files/includes/#';
function makeStructure($entry, $destination, $patternReplace)
{
$entry = preg_replace($patternReplace, '', $entry);
$parts = explode(DIRECTORY_SEPARATOR, $entry);
$dirArray = array_slice($parts, 0, sizeof($parts) - 1);
$dir = $destination . join(DIRECTORY_SEPARATOR, $dirArray);
if (!file_exists($dir)) {
mkdir($dir, 0777, true);
}
if ($dir !== $destination) {
$dir .= DIRECTORY_SEPARATOR;
}
$fileExtension = pathinfo($entry, PATHINFO_EXTENSION);
if (!empty($fileExtension)) {
$fileName = $dir . pathinfo($entry, PATHINFO_BASENAME);
return $fileName;
}
return null;
}
if ($zip->open($archiveName) === true) {
for ($i = 0; $i < $zip->numFiles; $i++) {
$entry = $zip->getNameIndex($i);
if (preg_match($pattern, $entry)) {
$file = makeStructure($entry, $destination, $patternReplace);
if ($file === null) {
continue;
}
copy('zip://' . $archiveName . '#' . $entry, $file);
}
}
$zip->close();
}