PHP - 从字符串添加文件的PCLZIP


PHP - PCLZIP adding file from string

PCLZIP是一个很棒的库,但不幸的是,它的文档记录很差。我使用它是为了支持禁用 ZipArchive(或不支持 php 版本)的服务器

我有一个功能可以将上传的文件(一个接一个)添加到ZIP存档中。如果存档不存在,它会创建一个,如果存档存在,它只会添加新文件。

我遇到的问题是添加一个 TXT 文件的函数,该文件基于存档中的注释。(该函数读取之前准备的注释,并应从字符串创建 TXT 文件并插入到存档中。

我似乎找不到从字符串中覆盖文件的函数(或者我不知道如何使用它)。

我可以用PCLZIP_ATT_FILE_NAME创建它,但不知何故,当我运行该函数时,每次将文件添加到存档时,它都会创建一个新的.txt文件(具有相同的文件名!我尝试使用PCLZIP_ATT_FILE_NEW_FULL_NAME - 但我找不到在哪里给它参数到需要覆盖哪个文件..

函数在这里:(对不起,如果它很长。

    $archive = new PclZip($zipname);
        if (!file_exists($zipname)){  //The Archive already exists - let´s just ADD new files.

            $comment = $comment_head . $comment_add ;
            $string_content = $comment;
            $v_list = $archive->create($file,
                                        PCLZIP_OPT_ADD_PATH, $sitename,
                                        PCLZIP_OPT_COMMENT, $comment,
                                        PCLZIP_OPT_REMOVE_ALL_PATH);
                                        $prop = $archive->properties();
                                        $prop = $prop['comment'];
                                        if (!$prop) {$prop = $comment;}
            $list = $archive->add(array(
                                   array( 
                                   PCLZIP_ATT_FILE_NAME => $string_file,
                                   PCLZIP_ATT_FILE_CONTENT => $prop,
                                   PCLZIP_ATT_FILE_NEW_FULL_NAME => $string_file
                                         )
                                   )
                                   );

                      if ($v_list == 0) {
                        die("Error : ".$archive->errorInfo(true));
                      }

        } else { 
// No Archive already exists - Create with new file .
        $comment_add =  $meta['file'] . PHP_EOL . PHP_EOL ;/*.$comment_foot*/ ;
        $b_list = $archive->add($file,
                                    PCLZIP_OPT_ADD_PATH, $sitename,
                                    PCLZIP_OPT_ADD_COMMENT, $comment_add,
                                    PCLZIP_OPT_REMOVE_ALL_PATH);
                                $prop = $archive->properties();
                                        $prop = $prop['comment'];
                                        if (!$prop) {$prop = $comment;}
            $list_6 = $archive->add(array(
                                   array( PCLZIP_ATT_FILE_NAME => $string_file,
                                        PCLZIP_ATT_FILE_CONTENT => $prop
                                         )
                                   )
                                   );
                  if ($b_list == 0) {
                    die("Error : ".$archive->errorInfo(true));
                  }
      }

所以 - 任何人都知道如何使用 PCLzip 从字符串(而不是从文件..)覆盖文件?

$archive = new PclZip("archive.zip");
$v_filename = "new_file.txt";
$v_content = "This is the content of file one'nHello second line";
$list = $archive->create(array(
                               array( PCLZIP_ATT_FILE_NAME => $v_filename,
                                      PCLZIP_ATT_FILE_CONTENT => $v_content
                                     )
                               )
                         );
if ($list == 0) {
  die("ERROR : '".$archive->errorInfo(true)."'");
}  

http://www.phpconcept.net/pclzip/news/3-pclzip-26