1

I use the following for zipping

//http://ninad.pundaliks.in/blog/2011/05/recursively-zip-a-directory-with-php/

class FlxZipArchive extends ZipArchive {

    public function addDir($location, $name) {
        $this->addEmptyDir($name);     
        $this->addDirDo($location, $name);
     } // EO addDir

    private function addDirDo($location, $name) {
        $name .= '/';
        $location .= '/';

        // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;

            // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } // EO addDirDo();
}


function zipIt($source, $target){
  $za = new FlxZipArchive;

  $res = $za->open($target, ZipArchive::CREATE);

  if($res === TRUE) {
      $za->addDir($source, basename($source));
      $za->close();
  }
  else
      echo 'Could not create a zip archive';

}

$the_folder = './Sales report';
$zip_file_name = './Sales report.docx';
//Don't forget to remove the trailing slash in folder
zipIt($the_folder,$zip_file_name,false);

The problem is that it includes the current directory,

Sales report/
    file1.html
    file2.html
    Sub_Dir/
        file19.html

But I just want

    file1.html
    file2.html
    Sub_Dir/
        file19.html

How to do that?

4

2 回答 2

1

这是我使用的:

/**
 * Add a directory and its contents to the archive
 *
 * @param string $dir       The local filesystem path to the directory
 * @param string $localName The archive filesystem path to the directory
 *
 * @throws \RuntimeException When adding an object to the archive fails
 */
public function addDir($dirPath, $localName = NULL)
{
    if ($localName === NULL) {
        $localName = basename($dirPath);
    }
    if (!$this->addEmptyDir($localName)) {
        throw new \RuntimeException('Error adding directory '.$dirPath.' to archive');
    }

    $this->addDirContents($dirPath, $localName);
}

/**
 * Add the contents of a directory to the archive
 *
 * @param string $dir       The local filesystem path to the directory
 * @param string $localName The archive filesystem path to the directory
 *
 * @throws \RuntimeException When adding an object to the archive fails
 */
public function addDirContents($dirPath, $localName = '')
{
    $base = ltrim($localName.'/', '/');

    foreach (glob("$dirPath/*") as $file) {
        if (is_dir($file)) {
            $this->addDir($file, $base.basename($file));
        } else {
            if (!$this->addFile($file, $base.basename($file))) {
                throw new \RuntimeException('Error adding file '.$file.' to archive');
            }
        }
    }
}

像您的代码一样,这些方法属于扩展类\ZipArchiveaddDir()添加一个目录及其内容,addDirContents()只需添加内容而不在存档中创建父目录(因此可以执行您想要的操作)。

我通常不喜欢不解释就给出免费的工作代码,但碰巧我已经在我的编辑器中打开了你需要的东西。

于 2013-03-20T10:35:07.797 回答
0

最后,我使用此代码来提供选择是否包含当前目录的选项:

<?php
//http://ninad.pundaliks.in/blog/2011/05/recursively-zip-a-directory-with-php/



class FlxZipArchive extends ZipArchive {

    public function addDir1($location, $name, $includeCurrDir) {
      if($includeCurrDir)
        $this->addEmptyDir($name);     
        $this->addDirDo1($location, $name, $includeCurrDir);
     } // EO addDir

    private function addDirDo1($location, $name, $includeCurrDir) {
      if($includeCurrDir)
        $name .= '/';
      else
        $name = '';

      $location .= '/';

        // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;

            // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } // EO addDirDo();

    public function addDir($location, $name) {
        $this->addEmptyDir($name);     
        $this->addDirDo($location, $name);
     } // EO addDir

    private function addDirDo($location, $name) {
        $name .= '/';
        $location .= '/';

        // Read all Files in Dir
        $dir = opendir ($location);
        while ($file = readdir($dir))
        {
            if ($file == '.' || $file == '..') continue;

            // Rekursiv, If dir: FlxZipArchive::addDir(), else ::File();
            $do = (filetype( $location . $file) == 'dir') ? 'addDir' : 'addFile';
            $this->$do($location . $file, $name . $file);
        }
    } // EO addDirDo();
}


function zipIt($source, $target, $includeCurrDir){
  $za = new FlxZipArchive;

  $res = $za->open($target, ZipArchive::CREATE);

  if($res === TRUE) {
      $za->addDir1($source, basename($source),$includeCurrDir);
      $za->close();
  }
  else
      echo 'Could not create a zip archive';

}
$the_folder = 'Sales report';
$zip_file_name = 'Sales report.docx';
//Don't forget to remove the trailing slash in folder
zipIt($the_folder,$zip_file_name,false);


?>
于 2013-03-20T11:18:58.103 回答