用于创建zip文件的递归函数

时间:2014-06-16 10:03:07

标签: php zip

您好我使用此函数创建一个zip文件(包含子文件和子目录)

<?php 


  function folderToZip($folder, &$zipFile, $exclusiveLength) { 
    $handle = opendir($folder); 
    while (false !== $f = readdir($handle)) { 
      if ($f != '.' && $f != '..') { 
        $filePath = "$folder/$f"; 
        // Remove prefix from file path before add to zip. 
        $localPath = substr($filePath, $exclusiveLength); 
        if (is_file($filePath)) { 
          $zipFile->addFile($filePath, $localPath); 
        } elseif (is_dir($filePath)) { 
          // Add sub-directory. 
          $zipFile->addEmptyDir($localPath); 
          folderToZip($filePath, $zipFile, $exclusiveLength); 
        } 
      } 
    } 
    closedir($handle); 

  } 


  function zipDir($sourcePath, $outZipPath) 
  { 
    $pathInfo = pathInfo($sourcePath); 
    $parentPath = $pathInfo['dirname']; 
    $dirName = $pathInfo['basename']; 

    $z = new ZipArchive(); 
    $z->open($outZipPath, ZIPARCHIVE::CREATE); 
    //$z->addEmptyDir($dirName); 
    folderToZip($sourcePath, $z, strlen("$parentPath/")); 
    $z->close(); 
  } 



   zipDir('mydirectory', 'copy_'.time().'.zip'); 

?>

该功能完美无缺,但只有当创建.zip文件的文件位于“ mydirectory ”外部时

- mydirectory
- createzip.php

但是我希望创建.zip文件的文件位于“ mydirectory / update /

- mydirectory
- mydirectory/update/createzip.php

并执行此操作:zipDir('../../mydirectory', 'copy_'.time().'.zip');,但这不起作用

UPDATE :使用此功能,问题仍然存在,我无法通过运行脚本从要复制的文件夹中复制该文件夹的副本

function Zip($source, $destination)
      {
          if (!extension_loaded('zip') || !file_exists($source)) {
              return false;
          }

          $zip = new ZipArchive();
          if (!$zip->open($destination, ZIPARCHIVE::CREATE)) {
              return false;
          }

          $source = str_replace('\\', '/', realpath($source));

          if (is_dir($source) === true)
          {
              $files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST);

              foreach ($files as $file)
              {
                  $file = str_replace('\\', '/', $file);

                  // Ignore "." and ".." folders
                  if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) )
                      continue;

                  $file = realpath($file);

                  if (is_dir($file) === true)
                  {
                      $zip->addEmptyDir(str_replace($source . '/', '', $file . '/'));
                  }
                  else if (is_file($file) === true)
                  {
                      $zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file));
                  }
              }
          }
          else if (is_file($source) === true)
          {
              $zip->addFromString(basename($source), file_get_contents($source));
          }

          return $zip->close();
      }

1 个答案:

答案 0 :(得分:-1)

替换

zipDir('mydirectory', 'copy_'.time().'.zip'); 

$time = time();
zipDir('mydirectory', 'copy_'.$time.'.zip'); 
rename('copy_'.$time.'.zip', 'mydirectory/update/copy_'.$time.'.zip');

这将创建zip,然后将其移至路径mydirectory/update/createzip.php 希望这就是你的意思。