Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 使用ZipArchive提取文件夹内容_Php_Ziparchive - Fatal编程技术网

Php 使用ZipArchive提取文件夹内容

Php 使用ZipArchive提取文件夹内容,php,ziparchive,Php,Ziparchive,我在一个具有此结构的站点上有压缩的\u file.zip: 我想将version_1.x文件夹中的所有内容提取到我的根文件夹: 我该怎么做?无需递归就可以实现吗?查看文档以了解更多信息。示例1.这是可能的,但您必须使用以下方法自己读取和写入文件: $source='version_1.x'; $target='/path/to/target'; $zip=新的ZipArchive; $zip->open('myzip.zip'); 对于($i=0;$inumFiles;$i++){ $nam

我在一个具有此结构的站点上有压缩的\u file.zip

我想将version_1.x文件夹中的所有内容提取到我的根文件夹:


我该怎么做?无需递归就可以实现吗?

查看文档以了解更多信息。示例1.

这是可能的,但您必须使用以下方法自己读取和写入文件:

$source='version_1.x';
$target='/path/to/target';
$zip=新的ZipArchive;
$zip->open('myzip.zip');
对于($i=0;$inumFiles;$i++){
$name=$zip->getNameIndex($i);
//跳过不在$source中的文件
如果(strpos($name,“{$source}/”)==0)继续;
//确定输出文件名(删除$source前缀)
$file=$target.'/'.substr($name,strlen($source)+1);
//如有必要,创建目录
$dir=dirname($file);
如果(!is_dir($dir))mkdir($dir,0777,true);
//从Zip读取并写入磁盘
$fpr=$zip->getStream($name);
$fpw=fopen($file,'w');
而($data=fread($fpr,1024)){
fwrite($fpw,$data);
}
fclose($fpr);
fclose($fpw);
}

在使用@netcoder的解决方案时,我遇到了与@quantme类似的错误。我对该解决方案进行了更改,它可以正常工作,没有任何错误

$source = 'version_1.x';
$target = '/path/to/target';

$zip = new ZipArchive;
if($zip->open('myzip.zip') === TRUE) {
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);

        // Skip files not in $source
        if (strpos($name, "{$source}/") !== 0) continue;

        // Determine output filename (removing the $source prefix)
        $file = $target.'/'.substr($name, strlen($source)+1);

        // Create the directories if necessary
        $dir = dirname($file);
        if (!is_dir($dir)) mkdir($dir, 0777, true);

        // Read from Zip and write to disk
        if($dir != $target) {
            $fpr = $zip->getStream($name);
            $fpw = fopen($file, 'w');
            while ($data = fread($fpr, 1024)) {
                fwrite($fpw, $data);
            }
            fclose($fpr);
            fclose($fpw);
        }
    }
    $zip->close();
}
$source='version_1.x';
$target='/path/to/target';
$zip=新的ZipArchive;
如果($zip->open('myzip.zip')==TRUE){
对于($i=0;$i<$zip->numFiles;$i++){
$name=$zip->getNameIndex($i);
//跳过不在$source中的文件
如果(strpos($name,“{$source}/”)==0)继续;
//确定输出文件名(删除$source前缀)
$file=$target.'/'.substr($name,strlen($source)+1);
//如有必要,创建目录
$dir=dirname($file);
如果(!is_dir($dir))mkdir($dir,0777,true);
//从Zip读取并写入磁盘
如果($dir!=$target){
$fpr=$zip->getStream($name);
$fpw=fopen($file,'w');
而($data=fread($fpr,1024)){
fwrite($fpw,$data);
}
fclose($fpr);
fclose($fpw);
}
}
$zip->close();
}

看起来可以工作,但返回以下错误:
警告:fopen(//)[function.fopen]:无法打开流:第21行/extract_zip.php中的目录警告:fclose():第26行/extract_zip.php中提供的参数不是有效的流资源警告:fopen(//application/)[function.fopen]:无法打开stream:21行/extract_zip.php中的目录警告:mkdir()[function.mkdir]:17行/extract_zip.php中没有这样的文件或目录;文件有200+个items@quantme:以上代码对我来说很好。你可能忘了什么。对于这个问题,您可能想问另一个问题。这个代码对我来说非常有效。我和quantme有同样的问题。我认为唯一缺少的是$zip->close和if($zip->open('myzip.zip')==TRUE){太糟糕了,我不能将这个问题标记为您的答案。我已经修改了您的答案,添加了我认为必要的部分,并改进了跳过文件部分,因为您的代码可能会写出版本1.1_x文件夹。您应该将@netcoder answer标记为正确。它对我有效。这不是一个好答案,因为无论怎样,我都会以各种方式提取版本1.1_x文件夹。我已经尝试了很多次,只有@netcoder建议的ZipArchive::getStream有效。
$source = 'version_1.x';
$target = '/path/to/target';

$zip = new ZipArchive;
if($zip->open('myzip.zip') === TRUE) {
    for($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);

        // Skip files not in $source
        if (strpos($name, "{$source}/") !== 0) continue;

        // Determine output filename (removing the $source prefix)
        $file = $target.'/'.substr($name, strlen($source)+1);

        // Create the directories if necessary
        $dir = dirname($file);
        if (!is_dir($dir)) mkdir($dir, 0777, true);

        // Read from Zip and write to disk
        if($dir != $target) {
            $fpr = $zip->getStream($name);
            $fpw = fopen($file, 'w');
            while ($data = fread($fpr, 1024)) {
                fwrite($fpw, $data);
            }
            fclose($fpr);
            fclose($fpw);
        }
    }
    $zip->close();
}