使用php解压缩文件(将ZIP文件折叠到一个文件夹中)

时间:2011-03-10 07:11:12

标签: php zip

    $file_name = 'New Folder.zip'
    $zip = new ZipArchive;
    $result = $zip->open($target_path.$file_name);
    if ($result === TRUE) {
        for($i = 0; $i < $zip->numFiles; $i++) {
        $filename = $zip->getNameIndex($i);
        $fileinfo = pathinfo($filename);
        copy("zip://".$file_name."#".$filename, $target_path.$fileinfo['basename']);
        }
    }

当我运行此代码时,我收到此错误Warning: copy(zip://New Folder.zip#New Folder/icon_android.png) [function.copy]: failed to open stream: operation failed in...

我该如何解决这个问题......

5 个答案:

答案 0 :(得分:2)

来自PHP's doc

$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo($your_desired_dir);
    $zip->close();
    foreach (glob($your_desired_dir . DIRECTORY_SEPARATOR . 'New Folder') as $file) {
        $finfo = pathinfo($file);
        rename($file, $your_desired_dir . DIRECTORY_SEPARATOR . $finfo['basename']);
    }
    unlink($your_desired_dir . DIRECTORY_SEPARATOR . 'New Folder');
    echo 'ok';
} else {
    echo 'failed';
}

Dunno你为什么要使用流媒体。

答案 1 :(得分:2)

copy("zip://".$file_name."#".$filename, $target_path.$fileinfo['basename']);}

正确

copy("zip://".dirname(__FILE__).'/'.$file_name."#".$filename, $target_path.$fileinfo['basename']);}

完整路径需要使用zip:// stream

答案 2 :(得分:1)

请参阅手册http://php.net/manual/en/book.zip.php

unzip.php (sample code)

// the first argument is the zip file
$in_file = $_SERVER['argv'][1];

// any other arguments are specific files in the archive to unzip
if ($_SERVER['argc'] > 2) {
    $all_files = 0;
    for ($i = 2; $i < $_SERVER['argc']; $i++) {
        $out_files[$_SERVER['argv'][$i]] = true;
    }
} else {
    // if no other files are specified, unzip all files
    $all_files = true;
}

$z = zip_open($in_file) or die("can't open $in_file: $php_errormsg");
while ($entry = zip_read($z)) {

    $entry_name = zip_entry_name($entry);

    // check if all files should be unzipped, or the name of
    // this file is on the list of specific files to unzip
    if ($all_files || $out_files[$entry_name]) {

        // only proceed if the file is not 0 bytes long
        if (zip_entry_filesize($entry)) {
            $dir = dirname($entry_name);

            // make all necessary directories in the file's path
            if (! is_dir($dir)) { pc_mkdir_parents($dir); }

            $file = basename($entry_name);

            if (zip_entry_open($z,$entry)) {
                if ($fh = fopen($dir.'/'.$file,'w')) {
                    // write the entire file
                    fwrite($fh,
                           zip_entry_read($entry,zip_entry_filesize($entry)))
                        or error_log("can't write: $php_errormsg");
                    fclose($fh) or error_log("can't close: $php_errormsg");
                } else {
                    error_log("can't open $dir/$file: $php_errormsg");
                }
                zip_entry_close($entry);
            } else {
                error_log("can't open entry $entry_name: $php_errormsg");
            }
        }
    }
}

来自http://www.java-samples.com/showtutorial.php?tutorialid=985

答案 3 :(得分:0)

我要做的第一件事是:

echo "FROM - zip://".$file_name."#".$filename;
echo "<BR>TO - " .  $target_path.$fileinfo['basename'];

看看你得到了什么

答案 4 :(得分:0)

我用非常简单的方法来做到这一点

system('unzip assets_04_02_2015.zip');