簡體   English   中英

使用php解壓縮文件(將ZIP文件折疊到一個文件夾中)

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

    $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...

我該如何解決...

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

改正為

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

完整路徑需要使用zip://流

PHP的文檔

$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';
}

鄧諾,為什么要使用流。

參見手冊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

我已經使用非常簡單的方法來做到這一點

system('unzip assets_04_02_2015.zip'); 

我要做的第一件事是:

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

看看你得到什么

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM