我有一个包含一些文件和文件夹的zip文件,我想从zip文件中将文件夹“/ files”的内容提取到指定的路径(我的应用程序的根路径)。
如果存在不存在的文件夹,则应该创建它。
因此,例如,如果zip中的路径是:“/ files /includes / test.class.php”,则应将其提取到
$path . "/includes/test.class.php"
我该怎么做?
我发现在zip文件中切换的唯一功能应该是
http://www.php.net/manual/en/ziparchive.getstream.php
但我实际上不知道如何用这个功能做到这一点。
答案 0 :(得分:0)
我认为您需要zziplib扩展才能使用
$zip = new ZipArchive;
if ($zip->open('your zip file') === TRUE) {
//create folder if does not exist
if (!is_dir('path/to/directory')) {
mkdir('path/to/directory');
}
//then extract the zip
$zip->extractTo('destination to which zip is to be extracted');
$zip->close();
echo 'Zip successfully extracted.';
} else {
echo 'An error occured while extracting.';
}
请阅读此链接以获取更多信息http://www.php.net/manual/en/ziparchive.extractto.php
希望这会有所帮助:)
答案 1 :(得分:0)
试试这个:
$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();
}