如何使用php从zip存档复制文件内容和目录数据

如何使用php从zip存档复制文件内容和目录数据

问题描述:

I am using php to open a zip file and read its content and then copy this content to some other location. I am stuck at one point that how to copy the file content(file is a json) and also have some images in other directories which I want to access.

$zip = zip_open(Input::file('file'));
$rzip = zip_read($zip);
$entr_open_zip = zip_entry_open($zip, $rzip, '');
if ($entr_open_zip) {
    return "hello";
    die;
} else {
    return "hi";
    die;
}

I have opened the file but don't know how to copy content or even how to open the directory and access files in that directory. Any help is appreciated. I will explain more if needed.Ignore the typos.

I have seen some questions related to it but not getting satisfactory solution.

我使用php打开一个zip文件并阅读其内容,然后将此内容复制到其他位置。 我有一点困惑,如何复制文件内容(文件是一个json),并在我想访问的其他目录中也有一些图像。 p>

  $ zip = zip_open(输入::文件('文件')); 
 $ rzip = zip_read($ zip); 
 $ entr_open_zip = zip_entry_open($ zip  ,$ rzip,''); 
if($ entr_open_zip){
 return“hello”; 
 die; 
} else {
 return“hi”; 
 die; 
} 
  code  >  pre> 
 
 

我已打开文件,但不知道如何复制内容,甚至不知道如何打开目录并访问该目录中的文件。 任何帮助表示赞赏。 如果需要,我会解释更多。忽略错别字。 p>

我看到了一些与之相关的问题,但没有得到满意的解决方案。 p> div>

ZipArchive in PHP have functions to handle .zip files. In your case to extract all contents to specific directory, extractTo function can make help.

Sample Code:

<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->extractTo('/my/destination/dir/');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
?>