开发者

Unzip the file using php (collapses the ZIP file into one Folder)

开发者 https://www.devze.com 2023-02-15 18:57 出处:网络
$file_name = \'New Folder.zip\' $zip = new ZipArchive; $result = $zip->open($target_path.$file_name);
    $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']);
        }
    }

When i run this code i get this error Warning: copy(zip://New Folder.zip#New Folder/icon_android.png) [function.copy]: failed to open stream: operat开发者_开发百科ion failed in...

How can I solve this...


From 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 why are you using stream.


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

correct to

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

Full path need to use zip:// stream


see manual 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");
            }
        }
    }
}

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


First thing I would do is this:

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

and see what you get


I have used very simple method to do this

system('unzip assets_04_02_2015.zip'); 
0

精彩评论

暂无评论...
验证码 换一张
取 消